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


Tags
pattern, sed, solved, variable

Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago   -   Original Discussion by mystition
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 January 2018, 4:46 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,678
Thanks: 112
Thanked 1,633 Times in 1,198 Posts
Quote:
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+.

bakunin
Sponsored Links
    #9  
Old Unix and Linux 1 Week Ago   -   Original Discussion by mystition
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 January 2018, 3:00 AM EST
Location: Simplicity
Posts: 3,947
Thanks: 335
Thanked 1,322 Times in 1,192 Posts


Code:
man sed
...
d

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
    #10  
Old Unix and Linux 1 Week Ago   -   Original Discussion by mystition
mystition's Unix or Linux Image
mystition mystition is offline
Registered User
 
Join Date: May 2009
Last Activity: 22 January 2018, 1:15 AM EST
Posts: 42
Thanks: 21
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by MadeInGermany View Post
Also doable with sed, continuing on your attempt


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


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

Quote:
Originally Posted by RudiC View Post
Try also


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



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

BR//
    #11  
Old Unix and Linux 1 Week Ago   -   Original Discussion by mystition
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 January 2018, 3:28 AM EST
Location: Aachen, Germany
Posts: 11,966
Thanks: 354
Thanked 3,688 Times in 3,386 Posts
awk works in

Code:
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 (3 Days Ago)
Sponsored Links
Reply

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 05:00 PM
Regex in sed to find specific pattern and assign to variable radioactive9 Shell Programming and Scripting 5 12-31-2013 04:51 AM
Pattern match exclusive return pattern/variable gatekeeper258 Shell Programming and Scripting 1 06-08-2013 05:27 AM
sed with complicated variable pattern pravintse Shell Programming and Scripting 4 06-10-2011 04: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 06:40 AM



All times are GMT -4. The time now is 04:29 AM.