sed variable expansion fails for substitution in range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed variable expansion fails for substitution in range
# 1  
Old 02-24-2014
sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one:
Code:
'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:
Code:
'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:

Code:
#!/bin/sh -xv
AAA=G
sed -e '/FUE\.SER/,/0  0/ {/FUE\.SER/b; s/F/'$AAA'/g;}'

Here's the output:

Code:
'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

Last edited by Don Cragun; 02-24-2014 at 01:57 AM.. Reason: Add CODE tags.
# 2  
Old 02-24-2014
Like this?
Code:
AAA=G
sed "2,$ s/F/$AAA/g" infile

--ahamed

Last edited by ahamed101; 02-24-2014 at 02:03 AM..
# 3  
Old 02-24-2014
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.
Code:
/FUE\.SER/,/0  0/

And, I don't want to modify the line
Code:
'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

Last edited by Don Cragun; 02-24-2014 at 03:05 AM.. Reason: Add CODE and ICODE tags.
# 4  
Old 02-24-2014
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.
Code:
AAA=G
sed "/'FUE.SER' 5/,/ 0  0/ {/FUE.SER/{n}; s/F/$AAA/g}" infile

--ahamed
# 5  
Old 02-24-2014
when I try your suggestion, I get the result:
Code:
'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?

Last edited by Don Cragun; 02-24-2014 at 03:06 AM.. Reason: Add CODE and ICODE tags.
# 6  
Old 02-24-2014
this is what I get
Code:
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
# 7  
Old 02-24-2014
The commands are in a file called 't3'. Here's the contents:
Code:
#!/bin/sh -xv
AAA=G
sed "/'FUE.SER' 5/,/ 0  0/ {/FUE.SER/{n}; s/F/$AAA/g}"

I invoke it like this:
Code:
./t3 <input

I'm running Cygwin64 in Windows 7. Could that be my problem?

Last edited by Don Cragun; 02-24-2014 at 03:08 AM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command. I tried three different nawk command as below but none of them substitute the value of entry variable. ls *.txt | while IFS='' read -r entry; do #nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)}... (9 Replies)
Discussion started by: mohtashims
9 Replies

2. Shell Programming and Scripting

Real-time scenarios where VARIABLE SUBSTITUTION/EXPANSION is useful

Hi, at academic level I am familiar with how variable substitution/expansion feature works. From your live experience, can you please tell what are real-time scenarios where ${variable:=} ${variable%pattern} ${variable:=word} sort of features can be used? How can we connect that dot that... (6 Replies)
Discussion started by: ab_2010
6 Replies

3. Shell Programming and Scripting

sed filtering lines by range fails 1-line-ranges

The following is part of a larger project and sed is (right now) a given. I am working on a recursive Korn shell function to "peel off" XML tags from a larger text. Just for context i will show the complete function (not working right now) here: function pGetXML { typeset chTag="$1" typeset... (5 Replies)
Discussion started by: bakunin
5 Replies

4. Shell Programming and Scripting

Sed - variable substitution

Problem with the code below is that the value of the variable is not getting substituted in the sed expression. #/bin/csh set UNIX_ID="rajibd" set X_ID="xrajibd" sed -n 's/$UNIX_ID/$X_ID/g' passwd When run , it is not giving expected output as shown below : ... (4 Replies)
Discussion started by: hiten.r.chauhan
4 Replies

5. Shell Programming and Scripting

Variable expansion in sed

The objective of the code below is to create sed script to be later executed. However, it bonks because $ARCHIVENAME expands to a directory specification so the forward slashes cause problems. I can think of a few solutions that would involve redesigning the process, but I'm hoping there might be... (4 Replies)
Discussion started by: tiggyboo
4 Replies

6. Shell Programming and Scripting

Sed variable substitution

Hello, I searched the forum and unable to find a solution for my particular problem. I have a text file I'm trying to insert some text using sed after finding a pattern.. File contains in one line Invoice date: xx/xx/xxxx Balance: $$$$ Name: xxxxxxxxxxxxxx Trying to insert Invoice "Number:... (3 Replies)
Discussion started by: mallu0987
3 Replies

7. Shell Programming and Scripting

[Solved] Command Substitution and Variable Expansion within a Case

Hello All, I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system). I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other... (1 Reply)
Discussion started by: jaimielives
1 Replies

8. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

9. Shell Programming and Scripting

SED variable substitution

Hi Chaps... I have a log file as below:- 01 Oct 2009 12:57:56 DEBUG : 01 Oct 2009 12:14:21 DEBUG :..... . . . .05 Oct 2009 14:31:56 DEBUG :.... . . .05 Oct 2009 12:57:56 DEBUG :.... 06 Oct 2009 01:23:11 DEBUG :.... . . .06 Oct 2009 12:53:46 DEBUG :.... (4 Replies)
Discussion started by: rpraharaj84
4 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question