pythonbeginer pythonbeginer - 6 months ago 20
Linux Question

changing a specific field as per file name

I have 20 files named as nxt_bas_01.txt, nxt_bas_02.txt ... nxt_bas_20.txt and at all the files at line nr.5 i have the same text

TITLE / nxt_bas_01
now i need to change only at line nr.5 as per my file name, i tried with sed -i unsuccessfully, please have a look at my simple code and share your suggestions,

num=01
till=20
while [ "$num" -lt `expr $till + 01 ` ]
do
sed -i '5s/.*/TITLE / nxt_bas_01/' nxt_bas_$num.txt
num=`expr $num + 1 `
done

Answer

Try this :

for num in {01..20}; do
   sed -i "5s/.*/TITLE \/ nxt_bas_$num/" nxt_bas_$num.txt
done

Not tested but it should do the trick.