2

I'm developing a script, and it perform calculations that need to include in a text file already filled. The format of the file lines is:

1 20/10/16 12:00 take car in the garage joe's garage

Inside the script I get a number from calculations, and that number will replace the first number of the row. For example, the "calculate" returns the value 15, then the script must access the file in question and change the row in question should look like this:

15 20/10/16 12:00 take car in the garage joe's garage

The point is that I do not know how to accomplish this change. It can be in bash, awk, sed, or any available resource. Thank you.

8
  • how this 15 is calculated ? 15 is calculated based on the 1st line number values ?
    – Kamaraj
    Oct 14, 2016 at 5:07
  • Yes. I use the difference between the date line and the current date. And the value is contained in a variable.
    – D_Alves
    Oct 14, 2016 at 5:14
  • tell me, how you calculated 15 from this line ? 20/10/16 12:00 take car in the garage joe's garage
    – Kamaraj
    Oct 14, 2016 at 5:20
  • This date is in format DD/MM/YY. I verify the ramining days to the date of line and i increase the first number of line (works like a priority level) according to proximity.
    – D_Alves
    Oct 14, 2016 at 5:22
  • The number that i need to edit is ever in the first column. I need to replace this number with a value stored in a variable.
    – D_Alves
    Oct 14, 2016 at 5:27

3 Answers 3

2

How about:

awk -v old=$oldNum -v new=$newNum \
'{if ($1==old) {$1=new; print} else {print}}' input.txt > output.txt

Tried it like this:

$ cat input.txt
2 19/10/16 15:30 some other line
1 20/10/16 12:00 take car in the garage joe's garage
3 17/10/16 5:30 another one

$ oldNum=2
$ newNum=15

Run the awk command, then:

$ cat output.txt
15 19/10/16 15:30 some other line
1 20/10/16 12:00 take car in the garage joe's garage
3 17/10/16 5:30 another one

Is this what you wanted ? If you want your result to appear in the original file, just mv the output file with new name input.txt, that should overwrite the input file (use an anti-slash \mv to avoid aliasing of the mv command).

NOTE: you can probably obtain the same result with shorter awk instructions, but this syntax makes it more readable. sed can probably do it in an even more concise way.


EDIT: I realise this works if you want to change only 1 number within your file. If I understand your problem correctly, you want to re-calculate the number for every line and create a file with those new numbers. The optimal way of doing that would be to include your calculation of new line number inside the awk script, that way you don't have to create a shell loop, which is generally a bad idea since calling tools like awk, echo, sed... repeatedly ends up being very costly. You could do something like this:

if [ -f $PWD/output.txt ]; then 
    # Remove old output if present in current directory
    \rm -f $PWD/output.txt
fi

awk '{ ###calculation here, result store in newNum###; $1=newNum; print}' input.txt > output.txt

For example, if I want to trivially just increment by one every line number:

awk '{$1++; print}' input.txt > output.txt

If you cannot (or dare not) include your calculation inside awk, you can do a loop on the file, but that's pretty clumsy from what I understand of bash scripting:

if [ -f $PWD/output.txt ]; then 
    # Remove old output if present in current directory
    \rm -f $PWD/output.txt
fi

while read line
do
    newNum=###Calculation here for current line###
    echo $line | awk -v new=$newNum '$1=new' >> output.txt
done <input.txt
1

try this..

awk -v num="$variable" '{$1=num}1' input.txt
7
  • I try, but this replace all line's first numbers.
    – D_Alves
    Oct 14, 2016 at 5:42
  • how i replace only the line in question?
    – D_Alves
    Oct 14, 2016 at 5:42
  • how i replace only the actual line?*
    – D_Alves
    Oct 14, 2016 at 5:43
  • is it always first line ?
    – Kamaraj
    Oct 14, 2016 at 5:44
  • No, the archive contains n lines, and i need to replace one by one.
    – D_Alves
    Oct 14, 2016 at 5:52
0

Here's a short python script that does what you want. Usage is simple: give it file, number that line starts with, and the number you wish to see instead

Demo:

    bash-4.3$ cat data.txt 
    2 19/10/16 15:30 some other line
    1 20/10/16 12:00 take car in the garage joe's garage
    3 17/10/16 5:30 another one
    bash-4.3$ python renumber.py data.txt 1 15
    ['renumber.py', 'data.txt', '1', '15']
    bash-4.3$ cat data.txt 
    2 19/10/16 15:30 some other line
    15 20/10/16 12:00 take car in the garage joe's garage
    3 17/10/16 5:30 another one

Code:

import sys
import os

print(sys.argv)

with open(sys.argv[1]) as inputfile:
    with open('/tmp/temp.text','w') as tempfile:
         for line in inputfile:
             old = line.strip().split()
             if old[0] == sys.argv[2]:
                old[0] = sys.argv[3] 
                line = ' '.join(old) + '\n' 
             tempfile.write(line)

os.rename('/tmp/temp.text',sys.argv[1])

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .