LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-13-2010, 12:45 AM   #1
Hb_Kai
Member
 
Registered: Jan 2008
Distribution: Windows 8.1, Debian 7
Posts: 91

Rep: Reputation: 49
Searching .txt file for (specific) strings and printing them to new file


Hey, I know the title was too long but basically, what I'm trying to do is take a specific string from a text file and print the strings to a seperate file and on a new line. May seem confusing but here's an example.

Name: Bob, PhoneNumber: *****, MobileNumber: *****, Email: ***@**.com,

And I have a file with this type of formatting; if I was using the above example, I would like to take only the Emails from the file and print them to a completely seperate file by > seperate-file.txt but the problem is, I don't know how to go about that.

I tried using grep to search for a string of "***@***.com" but that would only print the entire line including Name, PhoneNumber, etcetera and after reading the man pages, found out that it's for whole lines.

I was wondering if anybody knew of a command that I could do this with? It's beginning to give me a bit of a headache.

Thanks for any help in advanced.
 
Old 02-13-2010, 12:54 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

This looks like homework to me, is it? You're not going to find a lot of help for homework on LQ, since it's against the rules. Anyway, if it is, you should look into awk and loops to obtain the result you want. If it's not homework post what you've already tried and we'll take it from there.

Kind regards,

Eric
 
Old 02-13-2010, 01:19 AM   #3
Hb_Kai
Member
 
Registered: Jan 2008
Distribution: Windows 8.1, Debian 7
Posts: 91

Original Poster
Rep: Reputation: 49
Hey. Thank you for your reply.

No, I don't go to school or college at the minute, it has nothing to do with any homework or course work of the sort. All it is, was I have been trying to teach myself a bit more about BASH and its commands by looking into some of the man pages and making practice text files and other stuff and try to manipulate the way they work and what they contain and stuff from the terminal. I worked up this list of random nobodies with some random numbers, addresses, etc and was just wondering if this is the type of thing can be done with the terminal but being pretty sure that it was, it began giving me a headache so I wanted to ask on a forum.

I've tried such things as grep but grep could only print the whole lines and that's about as near as I could. The latest commands I have been trying are

Code:
find [directory] -type f -exec grep -i "[search string]" {} \; | cat > [file location/name]
as well as the grep -i

Code:
grep -i *@*.* random.txt
I have already looked into awk but when I was reading through the man pages, it was explaining about (mawk? And) how it's used with programs, which I didn't really understand how I would use awk to process the .txt file like so.

That's pretty much all I've been able to think of at the minute though.
 
Old 02-13-2010, 01:32 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

Ok, first, you don't need to pipe output into cat and then redirect to a file, you can just directly redirect to a file like this:
Code:
find [directory] -type f -exec grep -i "[search string]" {} \; > [file location/name]
By using awk you can split a line into cells if you have a common delimiter like in this case a space. So using awk you could put something like this:
Code:
find [directory] -type f -exec awk '{ print $8 }' {} \; > [file location/name]
or you could use cut to do the same.
Code:
find [directory] -type f -exec cut -d " " -f8 {} \; > [file location/name]
That would give you an output with the trailing comma included, so you can use sed to 'clip' it off and replace it by a newline character to get all the email addresses in a list.

I'm sure there are even other ways, as other users more acquainted with Bash will surely point out.

Kind regards,

Eric
 
Old 02-13-2010, 08:49 AM   #5
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
grep has a --only-matching option
 
Old 02-14-2010, 05:15 AM   #6
Hipants
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Rep: Reputation: 0
Smile

Hi, I think i might have the solution to your problem. I have found that your best friend in the linux world for text problems like this is grep, cut, and awk.

Grep is used to search for a whole line in a text file
Cut is used a lot of times with grep to separate the line, this is the one i used here, and awk does this and a whole bunch more. Takes a bit of practice but is a life saver in the end.

Anyway enough rambling.

Name: Bob, PhoneNumber: *****, MobileNumber: *****, Email: ***@**.com,

The way would do this is either separating by the colon : or the , first

1. cut -d: -f4 textfile


The -d is the delimeter or separator. In this case it is a : but it could be anything.

The -f is the field separator or the number of : in. Name is one, Bob, Phonenumber is two, 12345,MobileNumber is three, 1939493,Email is four and ***@**.com, is five etc

This will return us ***@**.com,

But the problem is the emails still have a comma so we will use it again this time with the delimeter as a comma.

so

Final Answer

cut -d: -f5 inputfile | cut -d, -f1 > outfile

Hope this helps...
 
Old 02-14-2010, 05:59 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Hb_Kai View Post
I've tried such things as grep but grep could only print the whole lines and that's about as near as I could.
Not really true. Look at the -o option: it will print only the matching string. If more matches are on the same line, they will be printed on separate lines.

In a more general case, where the file format is strictly the one you've posted in the OP (that is a CSV format and lines made of key/value pairs) you can try something like this:
Code:
awk 'BEGIN{RS = ","}/Email/{print $NF}' file > output_file
In this way awk will consider commas as record separators and for records matching the key (Email) it will print the value.
 
Old 02-18-2010, 09:09 AM   #8
Hb_Kai
Member
 
Registered: Jan 2008
Distribution: Windows 8.1, Debian 7
Posts: 91

Original Poster
Rep: Reputation: 49
Hey. I'm sorry for the late reply. I have been away the last couple of days. That one done the job exactly how I wanted. Thank you very much colucix.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing specific lines of a file using script. barunparichha Linux - Software 6 05-20-2009 12:31 AM
if file *.txt then do x else if *.mp3 file then do xx (bash) Techno Guy Linux - Newbie 3 04-12-2009 08:52 PM
searching contents of .class file within jar (for strings) wakeboarder3780 Linux - Newbie 4 02-17-2009 01:49 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
How to convert a txt file to be a db file in Redhat linux 9? winnie Linux - Newbie 3 06-27-2003 08:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration