LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Any grep, sed or awk gurus with regex familiarity? I need some help. (https://www.linuxquestions.org/questions/linux-newbie-8/any-grep-sed-or-awk-gurus-with-regex-familiarity-i-need-some-help-857258/)

bcrawl 01-19-2011 09:23 AM

hey guys. every one of the above commands worked. Thanks a lot. I have instantly got the awk example from druuna/schenidz but working on sed ones from other guys. But they all work magically. My terminal shows 5 different commands which operate on the same file and give the same output. pretty awesome.

bcrawl 01-19-2011 09:38 AM

Quote:

Originally Posted by grail (Post 4230163)
Based on sycamorex's sed:
Code:

sed -rn 's@</?mfr_id>@@gp' file

could someone please explain the command. How is it extracting it? Is it stripping <mfr_id> and </mfr_id> and leaving the digits or is it extracting the digits...and how does it differ from this one. This will help me understand the process a little bit.

Code:

sed -nr '/mfr_id/ s:[^[:digit:]]*([[:digit:]]+).*:\1:p'
Thanks.

EricTRA 01-19-2011 09:47 AM

Quote:

Originally Posted by bcrawl (Post 4230208)
hey guys. every one of the above commands worked. Thanks a lot. I have instantly got the awk example from druuna/schenidz but working on sed ones from other guys. But they all work magically. My terminal shows 5 different commands which operate on the same file and give the same output. pretty awesome.

Hi,

As usual in Linux, there's always more then one way to skin a cat :D I have learned lots from this thread thanks to the gurus here at LQ. Thanks guys.

Kind regards,

Eric

syg00 01-19-2011 04:14 PM

The first sed example strips out (specifically) what is known to be not wanted - it substitutes "null" for it.
The latter case ensures only non-digits start the record, and looks for a string of digits - followed by anything (which could include digits). The bit in parentheses - the string of digits in this case - is retained by the back-reference "\1".

Obvious ain't it .... :D. You have to be real careful constructing regex so you don't get unintended results. We've all been there.

schneidz 01-19-2011 07:52 PM

@ sycamorex , druuna
Code:

grep mfr_id <input-file> | awk -F "[><]" '{print $3}'
it was really not meant as a full solution but rather a code snippet to point the original poster in the right direction...

and the nit-picky award goes to...


All times are GMT -5. The time now is 09:17 AM.