0

I am really new to shell scripting, but here is the situation: I have a text file that contains the following :

int1, operationName1
int2, operationName2
int3, operationName3

In a nutshell, the file contains some operations (operationNameX) and the number of days remaining to execute each operation (intX).

I want to write a script that will be executed once per day and removes 1 from each int. for example if "test.txt" contains:

2, operation1
4, operation2
3, operation3

after the execution of the script, it will contain:

1, operation1
3, operation2
2, operation3

any idea on what should I do??

1 Answer 1

1

You could try Awk. It's incredibly powerful and perfect for this kind of thing:

awk -F, '//{VAL=$1; print (VAL-1)", "$2}' test.txt

This sets the field separator to a comma (whitespace, by default), assigns the first element (the integer) to a variable named VAL and then prints out VAL-1 followed by a comma, a space and the original second column.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, but can I overwrite the file content by the output of awk??
You could but it's generally a bad idea to overwrite your source file atomically. You could cat test.txt | awk ... > test.txt. I'd recommend doing it in two steps, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.