how to use the sed w option to redirect pattern match to file
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
how to use sed to redirect only pattern match to file (not entire line)
Hi, i'm trying to figure out how to use sed and am having some problems. I have this file: tcxmlmeldinger/innfraergo/TC-20060413121638410.xml
the whole xml file is on 1 line so i can't grep out the text i want (grrr). When i try to use the w option in sed:
Code:
sed -e '/Varsling_[0-9]+/w tmp' tcxmlmeldinger/innfraergo/TC-20060413121638410.xml
Even that writes the whole matching line (in this case, the entire file) to tmp. What i'm most interested in is just getting the regexp match (will look something like: Varsling_902394039023) so i can save it in a bash variable. I guess i could rephrase this whole thing:
how do i extract a substring from a line of text in a file using bash?
The search string is made up of the following 3 parts:
.* => everything in front of Varsling_[0-9]*
\(Varsling_[0-9]*\) => What we are looking for. The \( and \) are special. All in between can be represented as \1 in the replacement part.
.* => everything after Varsling_[0-9]*
The -n make sed suppress the normal output, the p on the end prints only the replacement pattern.
druuna, thank you thank you thank you! i had thought about the whole \1 thing, but wasn't sure how to write it syntactically correct. you da man! worked like a charm =)
Is the input still 1 line, or is the example in post #4 a new, multiple line, inputfile?
If the infile is multiple lines, you could do something like this (only first 2 lines are shown):
Code:
sed -n -e 's%<Year>\([0-9]*\)</Year>%\1%p' -n -e 's%<Month>\([0-9]*\)</Month>%\1%p' infile
Some chars are special and need to be escaped, but you can also change the separator that sed uses (changed it from / to % in the above example. Now you do not need to escape the / (in </zzzzz> constructs).
Also the -e option is new. This makes it possible to join multiple sed commands.
If it is one line, please post the line so I can have a look.
\s Matches a whitespace char (space, tab, newline...)
since the file is multi-line i thought that would take care of matching the newline and eventual tabs and/or whitespaces after and before the xml tags...
i added the .* before and after to take away whitespace, but the problem is that the output on the screen looks like this:
Code:
TIMESTAMP from SENT file test20060413134349.xml: 2006
04
13
13
43
i need it to look like this:
Quote:
TIMESTAMP from SENT file test20060413115712.xml: 200604131157
it seems like the 'p' option acts like a println, but i need it to act like a print (no newline). druuna thanks for the cool tip about switching the separator =)
PS. so it isn't possible to use multiple backward references like in my previous post?
The newline after each print (from sed) causes this behavior. Besides writing a complete sed script I don't know how to solve this.
So I would not use sed to solve your problem. It could probably be done by sed, but awk (to name just one) can do it more elegant and simpler (my opinion):
Mind the difference between print and printf (printf omits the newline, print does not).
Quote:
PS. so it isn't possible to use multiple backward references like in my previous post?
It is 'not possible' the way you set it up, including the fact that the inputfile is multiple lines. Not possible is between quotes, it is probably possible by writing a complete sed script, but I don't believe that is what you want (assumed by me......).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.