larrl
February 24, 2014, 12:45am
1
I'm trying to change "F" to "G" in lines after the first one:
'FUE.SER' 5
1 1 F0501 F0401 F0502
2 1 F0301 E0501 F0201 E0502 F0302
3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504
4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403
5 1 F0505 E0505 E0303 E0204 E0304 E0506 F0506
6 1 F0303 E0507 F0204 E0508 F0304
7 1 F0507 F0404 F0508
0 0
This is the desired end state:
'FUE.SER' 5
1 1 G0501 G0401 G0502
2 1 G0301 E0501 G0201 E0502 G0302
3 1 G0503 E0503 E0301 E0201 E0302 E0504 G0504
4 1 G0402 G0202 E0202 G0101 E0203 G0203 G0403
5 1 G0505 E0505 E0303 E0204 E0304 E0506 G0506
6 1 G0303 E0507 G0204 E0508 G0304
7 1 G0507 G0404 G0508
0 0
Here's the script:
#!/bin/sh -xv
AAA=G
sed -e '/FUE\.SER/,/0 0/ {/FUE\.SER/b; s/F/'$AAA'/g;}'
Here's the output:
'FUE.SER' 5
0502 G G
0302 E0502 GG
0504 E0503 E0301 E0201 E0302 E0504 G
0403 G0203 G
0506 E0505 E0303 E0204 E0304 E0506 G
0304 E0508 GG
0508 G G
0 0
I've tried several different quote schemes but can't seem to get $AAA to substitute correctly. Anyone have any suggestions?
Thanks,
Larry
Like this?
AAA=G
sed "2,$ s/F/$AAA/g" infile
--ahamed
larrl
February 24, 2014, 1:10am
3
Hi ahamed,
Yes, like that. But the range is in the middle of a file of hundreds of lines. So, I need the range construction.
/FUE\.SER/,/0 0/
And, I don't want to modify the line
'FUE.SER' 5
Eventually, I want to increment the AAA
variable. So, I want to pass that variable to the sed substitution. But, I can't seem to get the behavior that I want.
Thanks,
Larry
I didn't quite understand about incrementing the AAA variable. May be you should provide more input with desired output.
This has the range in place.
AAA=G
sed "/'FUE.SER' 5/,/ 0 0/ {/FUE.SER/{n}; s/F/$AAA/g}" infile
--ahamed
larrl
February 24, 2014, 1:27am
5
when I try your suggestion, I get the result:
'FUE.SER' 5
0502 G G
0302 E0502 GG
0504 E0503 E0301 E0201 E0302 E0504 G
0403 G0203 G
0506 E0505 E0303 E0204 E0304 E0506 G
0304 E0508 GG
0508 G G
0 0
As for incrementing, the goal is to increment AAA
each cycle. Initially it is 'F'. Next I want to to increment it to 'G', next time to 'H',the 'I', etc. Does that help?
this is what I get
root@maximus:/tmp# AAA=G
root@maximus:/tmp# sed "/'FUE.SER' 5/,/ 0 0/ {/FUE.SER/{n}; s/F/$AAA/g}" infile
'FUE.SER' 5
1 1 G0501 G0401 G0502
2 1 G0301 E0501 G0201 E0502 G0302
3 1 G0503 E0503 E0301 E0201 E0302 E0504 G0504
4 1 G0402 G0202 E0202 G0101 E0203 G0203 G0403
5 1 G0505 E0505 E0303 E0204 E0304 E0506 G0506
6 1 G0303 E0507 G0204 E0508 G0304
7 1 G0507 G0404 G0508
0 0
--ahamed
---------- Post updated at 10:31 PM ---------- Previous update was at 10:29 PM ----------
Which is your OS? Paste the exact command you are using, copy paste it please.
--ahamed
larrl
February 24, 2014, 1:43am
7
The commands are in a file called 't3'. Here's the contents:
#!/bin/sh -xv
AAA=G
sed "/'FUE.SER' 5/,/ 0 0/ {/FUE.SER/{n}; s/F/$AAA/g}"
I invoke it like this:
./t3 <input
I'm running Cygwin64 in Windows 7. Could that be my problem?
Can you paste the output of the script you execute? I don't see any issue with that.
Try this from the command line and see if it works
awk '/.FUE.SER. 5/,/ 0 0/{if(!/FUE.SER/){gsub(/F/,AAA)} }1' AAA=G infile
--ahamed
larrl
February 24, 2014, 2:21am
9
That works perfectly. How do I incorporate that into a working script?
---------- Post updated at 02:21 AM ---------- Previous update was at 02:14 AM ----------
Here's the output of the script I ran:
$ ./t3.txt <inp.txt
#!/bin/sh -xv
AAA=G
+ AAA=$'G\r'
sed "/'FUE.SER' 5/,/ 0 0/ {/FUE.SER/{n}; s/F/$AAA/g}"
/g}'d '/'\''FUE.SER'\'' 5/,/ 0 0/ {/FUE.SER/{n}; s/F/G
'FUE.SER' 5
0502 G G
0302 E0502 GG
0504 E0503 E0301 E0201 E0302 E0504 G
0403 G0203 G
0506 E0505 E0303 E0204 E0304 E0506 G
0304 E0508 GG
0508 G G
0 0
There's your problem, the \r in AAA assignment. Delete the line from the script and type it in manually - do not copy paste or do anything of that sort.
BTW
You can just relace the sed statement with the awk in your script omitting the file name infile.
--ahamed
larrl
February 24, 2014, 8:37pm
11
Many thanks, ahamed! I appreciate your sticking with me to find the real problem. Your patience is apreciated..