LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2010, 11:37 AM   #1
Basse1
LQ Newbie
 
Registered: Jun 2010
Location: India
Posts: 14

Rep: Reputation: 1
Question How do I separate columns using sed


I have a file that contains a couple of email addresses and I want to extract the usernames ( Letters before @ symbol ). How can I do that using sed/awk.

I know cut will work, but the current environment doesn't allow me to use cut command. I can use either awk or sed.

Eg: in the email address something@example.com, I want to extract "something".

Suggestions?
 
Old 07-01-2010, 11:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi

It depends a bit on where the email address is located. If it is the first and leftmost part:

awk -F@ '{ print $1 }' infile
or
sed 's/@.*//' infile

If it is not the leftmost part: Please provide an example.

Hope this helps.
 
Old 07-01-2010, 11:51 AM   #3
Basse1
LQ Newbie
 
Registered: Jun 2010
Location: India
Posts: 14

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
Hi

It depends a bit on where the email address is located. If it is the first and leftmost part:

awk -F@ '{ print $1 }' infile
or
sed 's/@.*//' infile

If it is not the leftmost part: Please provide an example.

Hope this helps.
Ah.. Cool! Thank you!
 
Old 07-01-2010, 12:02 PM   #4
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Maybe you should split the file into separate lines for each word and then delete all the lines which don't contain @
It feels quite naff to me to pipe one sed into another. There must be a way to do it all with one sed
Code:
sed 's/ /\n/g' infile | sed '/@/!d;s/@.*//'
 
0 members found this post helpful.
Old 07-01-2010, 12:54 PM   #5
Basse1
LQ Newbie
 
Registered: Jun 2010
Location: India
Posts: 14

Original Poster
Rep: Reputation: 1
Hi Andrew,

It is nice indeed..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed : substitute without displacing columns billywayne Programming 13 05-27-2010 08:33 PM
calculate the average of cells in columns in separate txt files Mike_V Programming 16 05-16-2009 04:37 AM
Removing columns from a file using `sed` indiancosmonaut Programming 6 10-04-2007 12:42 AM
Moving columns with sed or awk? btm Linux - Newbie 4 09-27-2007 02:03 PM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 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