LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   one more regex problem sorry (https://www.linuxquestions.org/questions/programming-9/one-more-regex-problem-sorry-873738/)

ted_chou12 04-08-2011 04:25 AM

one more regex problem sorry
 
Hi, I want to match any strings like:
Code:

...contents...
Wrote file /tmp/1-MMmxI7E

Red variable region

This is always at the start of the line, I have come up with a matching pattern, but this grabs the whole line instead of only "/tmp/1-MMmxI7E"
Code:

echo "$(cat "/var/mail/root/torrents/81A3A9.torrent" | sed -n '/^Wrote file/{ /.*/p}' )"
eg:"Wrote file /tmp/1-MMmxI7E" but I just want "/tmp/1-MMmxI7E" portion.
THanks,
Ted

grail 04-08-2011 04:52 AM

Yeah you still need a removal:
Code:

sed -n '/^Wrote file/s|[^/]*//p' file
Also, you should get out of the habit of using cat it serves no purpose here.

sycamorex 04-08-2011 04:52 AM

edit: Nevermind, grail provided a shorter and better solution:)

ted_chou12 04-08-2011 04:56 AM

thanks!

ted_chou12 04-08-2011 05:01 AM

Quote:

Originally Posted by grail (Post 4317882)
Yeah you still need a removal:
Code:

sed -n '/^Wrote file/s|[^/]*//p' file
Also, you should get out of the habit of using cat it serves no purpose here.

Hi, this returned empty string :/
the contents that I am tring to match is in the file "/var/mail/root/torrents/81A3A9.torrent"
so it is actually:
Code:

$ cat "/var/mail/root/torrents/81A3A9.torrent"
From: Chou Ted <ted_chou12@hotmail.com>
To: <daemon@tedchou12.cz.cc>
Subject: FW: test
Date: Thu, 7 Apr 2011 21:12:08 -0700


Cannot handle any part of multipart/alternative message


This message contains 'application/octet-stream`-format data.

which is being decoded and written to the file named "/tmp/1-MMrPxg6".
If you do not want this data, you probably should delete that file.
Wrote file /tmp/1-MMrPxg6

The part is the only part I want and it's variable.
Thanks!
Ted

colucix 04-08-2011 05:08 AM

Assuming the file name does not contain spaces:
Code:

sed -nr 's/Wrote file ([^[:space:]]+).*/\1/p' /var/mail/root/torrents/81A3A9.torrent

ted_chou12 04-08-2011 05:14 AM

Thank you!

grail 04-08-2011 05:44 AM

My bad ... still had old copy stored in paste :( ... Should have been:
Code:

sed -n '/^Wrote file/s|[^/]*||p' file

ted_chou12 04-08-2011 11:33 AM

Hi, np, it works perfect too! Thanks!!
Ted


All times are GMT -5. The time now is 08:03 PM.