I have a file with the following line
parameter (nx = 277, ncmk = 377)
I want to write a script where I can replace the current values of of the variables nx and ncmk with new values defined by variables $nx and $ncmk.
Thanks.
I have a file with the following line
parameter (nx = 277, ncmk = 377)
I want to write a script where I can replace the current values of of the variables nx and ncmk with new values defined by variables $nx and $ncmk.
Thanks.
Try:
perl -i -pe "/parameter/&&s/nx = \d+/nx = $nx/&&s/ncmk = \d+/ncmk = $ncmk/" file
nx="whatyouwant"
ncmk="whateveryouwant"
sed -e "s/\(nx = \)/\1$nx/g" -e "s/\(ncmk = \)/\1$ncmk/g" inputFile > outputFile
--ahamed
---------- Post updated at 03:23 AM ---------- Previous update was at 03:14 AM ----------
Initially I understood your issue incorrectly :wall:. Have corrected the code.
This is what you want right?
#Input
parameter (nx = 277, ncmk = 377)
Ouput
parameter (nx = 123, ncmk = 456)
--ahamed
Many thanks bartus11, that worked like a charm!
Yes ahamed101 that's what I meant. The your bit of code just appends the new values next to the old ones, i.e. it doesn't replace the old values?
Try this
sed -e "s/\(parameter (nx = \).*\(, ncmk = \).*\()\)$/\1$nx\2$ncmk\3" inputFile > outputFile
--ahamed
Hi I have another problem of the similar type. This time the line where the value I want to change looks like
Open (unit=Lfile1, file='Nc_957.dat')
I want to replace the number 957 with a variable $nc. I don't know anything about regex to achieve this.
Also the other problem the above line occurs three times in my file, but I only want the change to be in the third occurrence or the 48th line. I guess it's not ideal if I define the line in case I add more lines of code.
Thanks
---------- Post updated at 03:52 PM ---------- Previous update was at 11:51 AM ----------
I've made some progress the following code does what I want
perl -i -pe "/Open/&&s/Nc_\d+/Nc_$nx/" test.f
But how can I do this on say the nth line rather than every line it occurs?
This will replace every 3rd occurrence of the Open line:
perl -i -pe "(++\$i%3==0)&&/Open/&&s/Nc_\d+/Nc_$nx/" test.f
root@bt:/tmp# cat f
1 Open (unit=Lfile1, file='Nc_957.dat')
2 Open (unit=Lfile1, file='Nc_957.dat')
3 Open (unit=Lfile1, file='Nc_957.dat')
4 Open (unit=Lfile1, file='Nc_957.dat')
5 Open (unit=Lfile1, file='Nc_957.dat')
6 Open (unit=Lfile1, file='Nc_957.dat')
7 Open (unit=Lfile1, file='Nc_957.dat')
8 Open (unit=Lfile1, file='Nc_957.dat')
9 Open (unit=Lfile1, file='Nc_957.dat')
10 Open (unit=Lfile1, file='Nc_957.dat')
root@bt:/tmp# awk -v nc="123" '/file=.Nc_/{++i} {if(i==3){i=0; gsub(/Nc_[0-9]*.dat/,"Nc_"nc".dat")}}1 ' f
1 Open (unit=Lfile1, file='Nc_957.dat')
2 Open (unit=Lfile1, file='Nc_957.dat')
3 Open (unit=Lfile1, file='Nc_123.dat')
4 Open (unit=Lfile1, file='Nc_957.dat')
5 Open (unit=Lfile1, file='Nc_957.dat')
6 Open (unit=Lfile1, file='Nc_123.dat')
7 Open (unit=Lfile1, file='Nc_957.dat')
8 Open (unit=Lfile1, file='Nc_957.dat')
9 Open (unit=Lfile1, file='Nc_123.dat')
10 Open (unit=Lfile1, file='Nc_957.dat')
--ahamed