LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-08-2010, 11:53 AM   #1
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Rep: Reputation: 15
awk: swap fields given a condition


Hi,

I have a file records as follows:

Jane pepe@pepe.biz
john@pepe.net John
Joe joe@willxyz.org

How can I get that always first field be given name and second one be the mail address?

I've tried

Code:
awk '$1 ~ /@/ { a=$1 ; $1=$2; $2=a; print   }'  file
But that doesn't worked

TIA
 
Old 10-08-2010, 12:19 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Not with awk but this little script would do it:

Code:
#!/bin/bash
while read first second
do if echo $first |grep \@ >/dev/null 2>&1
   then echo $second $first
   else echo $first $second
   fi
done
Just run the script with redirect in from file. So if you named the script "reorder.sh" you'd run it as:

Code:
reorder.sh <file
Where file is the name of the file with the original data. Should output:
Jane pepe@pepe.biz
John john@pepe.net
Joe joe@willxyz.org
 
1 members found this post helpful.
Old 10-08-2010, 01:09 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Here's a working fix-up of your awk code. I think the main problem with your original method was trying to assign values to fields, but you didn't tell us what exactly was happening when your code didn't work, so I'm guessing about that.

Anyhow, you don't need to assign anything to anything - just print the fields in the opposite order if the email address comes first; otherwise, print the record as it is:

Code:
awk '{ if ($1 ~ /@/){printf $2" "$1"\n"} else {print} }' filename
Best regards.
 
1 members found this post helpful.
Old 10-08-2010, 07:39 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
 awk '$1~/@/{t=$1;$1=$2;$2=t}1' file
 
Old 10-08-2010, 07:49 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Thanks ghostdog74,

so what I see is:

OP had the exact right idea, only should have had the print statement outside the braces (where you've used a 1 instead)?

And, we can reassign fields to different values. I didn't know that!

Thanks,
Sasha
 
Old 10-08-2010, 08:17 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by GrapefruiTgirl View Post
(where you've used a 1 instead)?
"1" is just a shortcut for print, i am sure you already know.

Quote:
And, we can reassign fields to different values. I didn't know that!
yes, but take note it will change the FS. Assign OFS if needed.
 
1 members found this post helpful.
  


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
[SOLVED] Update a line using AWK with condition NetRock Programming 11 06-21-2010 08:16 PM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
[SOLVED] get fields using awk ashok.g Programming 9 12-09-2009 01:21 AM
AWK won't separate fields slinx Programming 2 03-10-2009 04:11 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

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

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