LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2017, 11:59 PM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
cut username only from a file with sed


I have a large file with this format: e-mail:hash:ID

e.g.:

Quote:
user1@yahoo.com:2fb6c56e97b68ea6d4286349062cc457:ID1
user2@gmail.com:5249b8cddf50e28cbe0e2c351afa37d8:ID2
user3@cox.net:fa9797f0463ebd4e3e90bf5e908f40d8:ID3
I would like to cut the username from the mail field.

Desired output:
user1
user2
user3

How could I do it with sed?

My bad command:
Code:
sed -np '/^*@/p' file.txt
I can do it with the cut command:
Quote:
cut -d@ -f 1 file
Thank you very much.

Last edited by freeroute; 08-13-2017 at 12:14 AM.
 
Old 08-13-2017, 12:24 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You have "cut", so this is self education on sed ?.
You need to use the "s" command of sed - well documented. In this case I might be inclined to delete the @ and everything after it, but the choice is yours.
 
1 members found this post helpful.
Old 08-13-2017, 12:26 AM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
You have "cut", so this is self education on sed ?.
You need to use the "s" command of sed - well documented. In this case I might be inclined to delete the @ and everything after it, but the choice is yours.
Yes, self education only.
I am learning sed...

Thank you.
 
Old 08-13-2017, 12:35 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Start here - go down to docs and get the info file; handy to have on your machine.
Also have a look at the 1liners file for ideas of how to do things with sed.
 
Old 08-13-2017, 12:36 AM   #5
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
You would use:
Code:
$ sed 's/@.*//g' file1 > file2
where file1 is the input file and file2 is the output file.
 
1 members found this post helpful.
Old 08-13-2017, 12:51 AM   #6
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
You would use:
Code:
$ sed 's/@.*//g' file1 > file2
where file1 is the input file and file2 is the output file.
Thank you. It works.

I understand that s means substitute, g means global.
Your command means: Substitute characters in lines by lines with nothing which contains @ character, followed any character (.*), and than print matchings?
 
Old 08-13-2017, 03:05 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
sed will process file line by line (in your case it is a substitution) and also will print the result. This is the default behavior, no need to match anything.
 
1 members found this post helpful.
Old 08-13-2017, 05:25 AM   #8
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
The substitution expression means substitute nothing in place @ followed by any number of single characters.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
CUT or SED rozz.alex Linux - Newbie 5 12-18-2012 06:11 AM
how can i use sed to cut out all the text up until the pattern? daweefolk Linux - Newbie 4 02-15-2011 09:17 AM
using SED or AWK to cut data from a file, between certain characters hugh86 Linux - Newbie 18 10-28-2010 04:17 AM
Get data from multi lined text file using awk, sed or perl - grep & cut not upto par cam34 Programming 4 07-02-2010 03:10 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:47 AM.

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