Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing specific lines in multiple files using bash

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

like image 233
theFunnyEngineer Avatar asked Feb 18 '26 03:02

theFunnyEngineer


1 Answers

Taking the first line of your specification:

Replace lines 27:29 of filename005, with text1=000; text2=005; text3=010

That becomes:

sed -e '27,29c\
text1=000\
text2=005\
text3=010' filename005

Rinse and repeat. The backslashes indicate to sed that the change continues. It's easier on yourself if your actual data lines do not need to end with backslashes.

You can play with:

seq 1 35 |
sed -e '27,29c\
text1=000\
text2=005\
text3=010'

to see what happens without risking damage to precious files. Given the specification lines, you could write a sed script to generate sed scripts from the specification (though I'd be tempted to use Perl or awk instead; indeed, I'd probably do the whole job in Perl).

like image 143
Jonathan Leffler Avatar answered Feb 19 '26 20:02

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!