Link to home
Start Free TrialLog in
Avatar of AbdellahT
AbdellahT

asked on

Linux string manipulation.


I am looking for a Linux command or script that retrieves a particular string from a file and write it to another file.
 The file to be searched is a log file that may contain e-mail addresses in the following format “no.reply+something+thisstring@domain.com”
I need to retrieve "thisstring" wherever found and write it to another file.

My approach was to search for “no.reply+” substring using grep command and then use another command that retrieves the string between the second + sign and the @ sign, but I need help writhing the complete command.

Any help is appreciating it.

Thanks in advance.
Avatar of mhenry20
mhenry20
Flag of United States of America image

grep "no.reply+" filename | cut -d "+" -f 3
More information cut:

-d = delimeter in this case the + sign.
-f = field to return in this case the third field.

So -f 1 = no.reply
-f 2 = something
and
-f 3 = thisstring@domain.com

in case you need to adjust fields.
Avatar of AbdellahT
AbdellahT

ASKER

I ran the command but the result are lines start with thisstring@domain.com ..........
How can I make the command to return only "thisstring"?
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of mhenry20
mhenry20
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Groovy.
grep "no.reply+" under some systems might behave as egrep and match no.replyyyyyyyyyy

you should fgrep for exact string matching.
even if it doesn't matter due to the input, it makes your intention clearer in the code.