Solved

Linux string manipulation.

Posted on 2010-08-24
7
513 Views
Last Modified: 2013-12-21

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.
0
Comment
Question by:AbdellahT
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:mhenry20
ID: 33514986
grep "no.reply+" filename | cut -d "+" -f 3
0
 
LVL 4

Expert Comment

by:mhenry20
ID: 33515016
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.
0
 

Author Comment

by:AbdellahT
ID: 33515225
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Accepted Solution

by:
mhenry20 earned 500 total points
ID: 33515371
grep "no.reply+" filename | cut -d "+" -f 3|cut -d "@" -f 1

That will cut again at the @ sign and return the first field. thisstring.
0
 

Author Comment

by:AbdellahT
ID: 33516261
Groovy.
0
 
LVL 3

Expert Comment

by:T1750
ID: 33925128
grep "no.reply+" under some systems might behave as egrep and match no.replyyyyyyyyyy

you should fgrep for exact string matching.
0
 
LVL 3

Expert Comment

by:T1750
ID: 33925135
even if it doesn't matter due to the input, it makes your intention clearer in the code.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video discusses moving either the default database or any database to a new volume.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now