sed command to print expression plus entire line after
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sed command to print expression plus entire line after
Hey guys
Using the following ced command with a lot of help from a couple of forum members which works great.
I cannot work out how to print the "said: xxx" and also include the rest of the line ie, description of the error, in bold below.
sed -rn 's/.*<([^>]*).*(said: 450*|said: 451*|said: 452*|said: 550*([- ]5\.1\.1)?).*/\1,\2/p' mail > out.txt
Eg, above ced urrently prints the 'Person@amailbox.com,said: 550' I'm guessing it's p ot 'p', which didn't work!
Jul 3 05:58:54 245ML postfix/smtp[15004]: 77FFC1665C0: to=<Person@amailbox.com>, relay=mail.amailbox.com[xxx.xxx.xxx.xxx]:25, delay=23, delays=0.82/0.13/21/0.26, dsn=5.0.0, status=bounced (host mail.amailbox.com[xxx.xxx.xxx.xxx] said: 550 <Person@amailbox.com>, Recipient unknown (in reply to RCPT TO command))
If you just want the whole line, you don't need sed, you could just use grep.
Here's sed
Code:
# just entire line
sed -rn 's/.*<([^>]*).*(said: 450*|said: 451*|said: 452*|said: 550*([- ]5\.1\.1)?).*/&/p'
# matches, followed by line
sed -rn 's/.*<([^>]*).*(said: 450*|said: 451*|said: 452*|said: 550*([- ]5\.1\.1)?).*/\1,\2,&/p'
Using the '# matches, followed by line'
Instead of printing, eg, Person@amailbox.com,said: said: 550 Person@amailbox.com, Recipient unknown (in reply to RCPT TO command)) Which would be perfect.
It actually prints:
Person@amailbox.com,said: 550,Jul 4 04:14:24 245ML postfix/smtp[15004]: 77FFC1665C0: to=<Person@amailbox.com>, relay=mail.amailbox.com[xxx.xxx.xxx.xxx]:25, delay=23, delays=0.82/0.13/21/0.26, dsn=5.0.0, status=bounced (host mail.amailbox.com[xxx.xxx.xxx.xxx] said: 550 <Person@amailbox.com>, Recipient unknown (in reply to RCPT TO command))
Seems to be adding another 'said:' and re positioning date and time
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.