I’m relatively new to bash scripting, having started out of the need to manage my simulations on supercomputers. I’m currently stuck on writing a script to change specific lines in my pbs files.
There’s 2 stages to my problem. First, I need to replace a number of lines in a text file (another script), and overwrite that file for my later use. The rough idea is:
Replace lines 27, 28 and 29 of
'filename005'with'text1=000','text2=005'and'text3=010'
Next, I’d like to do that recursively for a set of text files with numbered suffixes, and the numbering influences the replaced text.
My code so far is:
#!/bin/bash
for ((i = 1; i < 10; i++))
do
let NUM=i*5
let OLD=NUM-5
let NOW=NUM
let NEW=NUM+5
let FILE=$(printf "filename%03g" $NUM)
sed "27 c\text1=$OLD" $FILE
sed "28 c\text2=$NOW" $FILE
sed "29 c\text3=$NEW" $FILE
done
I know there are some errors in the last 4 lines of my code, and I’m still studying up on the proper way to implement sed. Appreciate any tips!
Thanks!
CS
Taking the first line of your specification:
That becomes:
Rinse and repeat. The backslashes indicate to
sedthat the change continues. It’s easier on yourself if your actual data lines do not need to end with backslashes.You can play with:
to see what happens without risking damage to precious files. Given the specification lines, you could write a
sedscript to generatesedscripts from the specification (though I’d be tempted to use Perl orawkinstead; indeed, I’d probably do the whole job in Perl).