LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-09-2017, 02:33 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
awk - cut usernames from mail addresses with gsub or regex


How can I cut usernames from a file with awk with gsub or regex?
mail.txt contains:
Quote:
freeroute@gmail.com
free34te@gmail.com
freetoyota@co.uk
Desired output:
Quote:
freeroute
free34te
freetoyota
I can do it with these commands:
Code:
awk -F@ '{print $1}' email.txt 
sed -E 's/([A-Za-z0-9]+).*/\1/' email.txt or
sed -E 's/@.*//' email.txt
But in this case I would like to use awk with gsub/regex like with sed.
Note: I learning awk, so I would like to do this task with awk, too.
 
Old 11-09-2017, 02:37 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,629

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
that is relatively simple:
gsub("@.*", "")
 
1 members found this post helpful.
Old 11-09-2017, 02:44 AM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thank you.
Are there any other solution using only awk with regex (without gsub)?
Like in this example:

Code:
sed -E 's/([A-Za-z0-9]+).*/\1/' email.txt or
sed -E 's/@.*//' email.txt
 
Old 11-09-2017, 02:51 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,629

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
yes, there are other possibilities too.
https://is.gd/60BpEm
 
1 members found this post helpful.
Old 11-09-2017, 03:06 AM   #5
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Okay. Thank you for your help.
 
  


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
[SOLVED] Using cut to extract list of usernames and shells from /etc/passwd.... BMan8577 Programming 7 02-22-2014 11:49 PM
[SOLVED] AWK: gsub ' or set ' as field separator cristalp Programming 8 11-10-2011 10:03 AM
[SOLVED] awk gsub with "(" rubber08 Linux - Newbie 4 04-30-2010 01:04 PM
awk gsub() command - string (column) manipulation - substitution casperdaghost Linux - Newbie 1 03-08-2010 02:12 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:46 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