Solved

Linux string manipulation.

Posted on 2010-08-24
7
516 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After running Ubuntu some time, you will be asked to download updates for fixing bugs and security updates. All the packages you download replace the previous ones, except for the kernel, also called "linux-image". This is due to the fact that w…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…

809 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