LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-13-2017, 12:59 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 66

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 01:14 AM.
 
Old 08-13-2017, 01:24 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,959

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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, 01:26 AM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 66

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, 01:35 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,959

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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, 01:36 AM   #5
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 3,136

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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, 01:51 AM   #6
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 66

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, 04:05 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,797

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
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, 06:25 AM   #8
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 3,136

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


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
CUT or SED rozz.alex Linux - Newbie 5 12-18-2012 07:11 AM
how can i use sed to cut out all the text up until the pattern? daweefolk Linux - Newbie 4 02-15-2011 10:17 AM
using SED or AWK to cut data from a file, between certain characters hugh86 Linux - Newbie 18 10-28-2010 05: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 04:10 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM

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

All times are GMT -5. The time now is 10:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration