Home > other >  How to multiple a number by 2 (double)present in a particular line number of a file, in Linux?
How to multiple a number by 2 (double)present in a particular line number of a file, in Linux?

Time:08-02

File_A

Name: John Smith
Grade: 8
Institute: Baldwin
Number of entries: 125
State: Texas

File_B

Name: David Buck
Grade: 9
Institute: High Prime
Number of entries: 123
State: California

There are many such similar files in which the Number of entries (present at line number 4 in all files) has to doubled.

For File_A it should be 250 and for File_B 246.

How to do this for all files in Linux?(using sed or awk or any other commands)

Tried commands:

sed -i '4s/$2/$2*2/g' *.txt               (nothing happening from this)
awk "FNR==4 {sub($2,$2*2)}" *.txt         (getting syntax error)

CodePudding user response:

With your shown samples please try following awk code. Simple explanation would be look/search for string /Number of entries: and then multiply 2 into value of last field and save it within itself, then print lines by mentioning 1.

awk '/Number of entries:/{$NF = ($NF * 2)} 1' File_A File_B

Run above command it will print output on screen, once you are Happy with output and want to save output into Input_file itself then you can try awk's -inplace option(available in GNU awk 4.1 version etc).

Also if your files extension is .txt then pass it to above awk program itself, awk can read multiple files itself.

CodePudding user response:

I want to explain why what you have tried failed, firstly

sed -i '4s/$2/$2*2/g' *.txt

$ has not special meaning for GNU sed, that it is literal dollar sign, also GNU sed does not support arithmetic, so above command is: at 4th line replace dollar sign folowed by 2 using dollar sign followed by 2 followed by asterix followed by 2 and do so globally. You do not have literal $2 at 4th line of line which is firstly rammed so nothing happens.

awk "FNR==4 {sub($2,$2*2)}" *.txt 

You should not use " for enclosing awk command unless you want to summon mind-boggling bugs. You should use ' in which case syntax error will be gone, however behavior will be not as desired. In order to do that your code might be reworked to

awk 'BEGIN{FS=OFS=": "}FNR==4{$2*=2}{print}' *.txt

Observe that I specify FS and OFS to inform GNU AWK that field are separated and should be separated by : rather than one-or-more whitespace characters (default) and do not use sub function (which is for working with regular expression), but rather simply increase 2 times operator (*=2) and I also print line, as without it output would be empty. If you want to know more about FS or OFS read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

  • Related