Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

sed with pattern using variable

Shell Programming and Scripting

pattern, sed, solved, variable

Thread Tools Search this Thread Display Modes
Old Unix and Linux 01-11-2018   -   Original Discussion by mystition
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
Join Date: May 2005
Last Activity: 25 May 2018, 6:09 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,765
Thanks: 112
Thanked 1,686 Times in 1,239 Posts
Originally Posted by MadeInGermany View Post
Ah, of course, the d command jumps to the next input cycle, so the following commands are not run.
Actually this was not the case in my test and the script worked as it was shown here. For reference, i used Linux (Kernel 4.10.42) and GNU-sed 4.2.2, shell is Kornshell 93 u+.

Sponsored Links
Old Unix and Linux 01-12-2018   -   Original Discussion by mystition
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Join Date: May 2012
Last Activity: 25 May 2018, 6:53 PM EDT
Location: Simplicity
Posts: 4,084
Thanks: 351
Thanked 1,379 Times in 1,243 Posts

man sed

Delete pattern space. Start next cycle.

You must test with an input file that has a further <Description>xyz</Description> after (outside) the <OfferDefinition Id=...> ... </OfferDefinition> block.
Sponsored Links
Old Unix and Linux 01-14-2018   -   Original Discussion by mystition
mystition's Unix or Linux Image
mystition mystition is offline
Registered User
Join Date: May 2009
Last Activity: 25 April 2018, 5:33 AM EDT
Posts: 42
Thanks: 21
Thanked 0 Times in 0 Posts
Originally Posted by MadeInGermany View Post
Also doable with sed, continuing on your attempt

sed -e '/<OfferDefinition Id="/ {h; s/.*=" *//; s/ *">.*//; x; n;}' -e '/<Description>/ {H; x; s/\(.*\)\n\(.*<Description>\)/\2\1_/;}' file.txt

Better readable in two lines

sed '
  /<OfferDefinition Id="/ {h; s/.*=" *//; s/" *>.*//; x; n;}
  /<Description>/ {H; x; s/\(.*\)\n\(.*<Description>\)/\2\1_/;}
' file.txt

Of course digging out the saved value from the hold space is a bit of a hack (also in the previous post).
While the other codes using awk were giving slight errors (while printing the 'id' in 'description', they were replacing the existing text) which could be tweaked, your code worked perfectly. Many Thanks!

---------- Post updated at 05:17 PM ---------- Previous update was at 05:03 PM ----------

Originally Posted by RudiC View Post
Try also

awk -F\" '/OfferDefinition>/ {ID = $2} /Description>/ {sub (/>/, "&" ID "_")} 1' file
Thanks Rudi, you are always helpful. Worked with a little bit of tweak in separators:

 awk -F\" '/OfferDefinition>|="/ {ID = $2"_"} /Description>/ {sub (/>/, "&" ID )} 1' file

However, can you explain why you have used '1' at the end? I tried removing it to understand but the code doesn't work without it...

Old Unix and Linux 01-14-2018   -   Original Discussion by mystition
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 25 May 2018, 6:41 PM EDT
Location: Aachen, Germany
Posts: 12,751
Thanks: 415
Thanked 3,921 Times in 3,605 Posts
awk works in

pattern {action}

pairs. If pattern evaluates to TRUE, the respective action is executed. 1 is always TRUE, and for a missing actions the default, print, will be taken.
The Following User Says Thank You to RudiC For This Useful Post:
mystition (01-18-2018)
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern Klasform Shell Programming and Scripting 9 10-12-2015 04:00 PM
Regex in sed to find specific pattern and assign to variable radioactive9 Shell Programming and Scripting 5 12-31-2013 03:51 AM
Pattern match exclusive return pattern/variable gatekeeper258 Shell Programming and Scripting 1 06-08-2013 04:27 AM
sed with complicated variable pattern pravintse Shell Programming and Scripting 4 06-10-2011 03:56 AM
Need help in sed command ( Replacing a pattern inside a file with a variable value ) frozensmilz Shell Programming and Scripting 2 12-22-2008 05:40 AM

All times are GMT -4. The time now is 10:32 PM.