1

what would the syntax look like to pass in zsh a variable line number and string to awk and having awk insert the string at the given line number?

#!/bin/zsh

freq_pols=('10 10' '10 11c' '10 11s' '10 20' '10 21c' '10 21s' '10 22c' '10 22s' '11c 11c' '11c 11s' '11c 20' '11c 21c' '11c 21s' '11c 22c' '11c 22s' '11s 11s' '11s 20' '11s 21c' '11s 21s' '11s 22c' '11s 22s' '20 20' '20 21c' '20 21s' '20 22c' '20 22s' '21c 21c' '21c 21s' '21c 22c' '21c 22s' '21s 21s' '21s 22c' '21s 22s' '22c 22c' '22c 22s' '22s 22s')

line_number_O=('8' '11' '14' '17' '20' '23' '26' '29' '32' '35' '38' '41' '44' '47' '50' '53' '56' '59' '62' '65' '68' '71' '74' '77' '80' '83' '86' '89' '92' '95' '98' '101' '104' '107' '110' '113')
line_number_H1=('118' '121' '124' '127' '130' '133')
line_number_H2=('138' '141' '144' '147' '150' '153')

zeros="     0.00000000  0.00000000  0.00000000  0.00000000  0.00000000"


for ((job=1;job<=2.0;job++));
do
output=~/interpolate/grid_1_11_15/aiff/m$job.freq_pol

    for ((i=1;i<=36;i++));
    do
    holder=$line_number_O[i]
    tmp=$(sed -n "${holder}p" $output)

        if [[ $tmp != $freq_pols[i] ]];then
        #echo $tmp
        awk -v n=$holder -v s=$zeros 'NR == n {print s} {print}' $output > test
        awk -v n=$holder -v s=$freq_pols[i] 'NR == n {print s} {print}' $output > test
        fi

    done


done

sample input:

Title monomer ... monomer
Frequencies   0.5    10
Skip  0
Print nonzero
Molecule  monomer
Site  O1  type  O1
10 10
 7.16240000      7.13140000      6.95110000      6.37710000      5.16380000
 3.41180000      1.74320000      0.64940000      0.12840000      0.00480000
10 11s
-0.78000000     -0.77400000     -0.74000000     -0.64100000     -0.45000000
-0.22200000     -0.09000000     -0.03200000     -0.00500000      0.00000000
.....

Sample output:

Title monomer ... monomer
Frequencies   0.5    10
Skip  0
Print nonzero
Molecule  monomer
Site  O1  type  O1
10 10
 7.16240000      7.13140000      6.95110000      6.37710000      5.16380000
 3.41180000      1.74320000      0.64940000      0.12840000      0.00480000
**10 11c**
**0.0000000      0.00000000     0.00000000     0.00000000     0.0000000**
**0.0000000      0.00000000     0.00000000     0.00000000     0.0000000**
10 11s
-0.78000000     -0.77400000     -0.74000000     -0.64100000     -0.45000000
-0.22200000     -0.09000000     -0.03200000     -0.00500000      0.00000000
.....

Where the text in bold is what I wish to add. (bold text now surrounded by ** chars : Shellter)

shellter
  • 36,525
  • 7
  • 83
  • 90
  • 1
    A good start at a question. Please edit to include the sample output (we don't need to see 36 lines ;-) . Also not that each time you use `> test`, you're overwriting the previous contents. Have one of the first lines be `>test` (to zero file out), then use `awk ... >> test`. Also, don't use `test` as a name, its a reserved word in the shell. Why not `mytest`. Good luck. – shellter Mar 27 '15 at 00:17
  • 2
    Good show, thanks for the data. This can be simplified a lot, probably just as 1 awk program. Would that work for you? But if not, you should go thru and surround all variable names in dbl-quotes, like `awk -v n="$holder" -v s="$zeros " 'NR == n {print s} {print}' $output >> mytest` ;[-) Sorry, I have an early morning tomorrow and have to goto bed:-( . Good luck. – shellter Mar 27 '15 at 01:55
  • 1
    can you use a mostly `awk` (maybe all) solution? I don't want to spend time writing it if you must stick with your current design. Good luck. – shellter Mar 28 '15 at 16:25

1 Answers1

0

I would go with sed. Here you can see an example that could work for you. Given that when you append to a file the line number changes probably you should append in reverse line number.

Community
  • 1
  • 1
Stanislav
  • 2,629
  • 1
  • 29
  • 38