sed with pattern using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed with pattern using variable
# 8  
Old 01-11-2018
Quote:
Originally Posted by MadeInGermany
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
# 9  
Old 01-12-2018
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.
# 10  
Old 01-14-2018
Quote:
Originally Posted by MadeInGermany
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
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 01-14-2018
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.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

6. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

7. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

8. Shell Programming and Scripting

sed with complicated variable pattern

Hi, Below is the content of the file how it looks: # EMAIL #export BMS_EMAIL_ENABLED=true export BMS_EMAIL_ENABLED=false #export BMS_EMAIL_SERVER=esasmtp01.kohls.com export BMS_EMAIL_SERVER=esasmtp01.kohls.com.SMTP_SERVICE export BMS_EMAIL_FROM_ADDRESS=ec_notify@kohlsectest.com export... (4 Replies)
Discussion started by: pravintse
4 Replies

9. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

10. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies
Login or Register to Ask a Question