Register
It is currently Thu Apr 17, 2014 6:29 am

Awk/sed loop causing me grief


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Nov 22, 2013 9:44 am   

Joined: Fri Nov 22, 2013 9:29 am
Posts: 2
Hi

I know this is really basic, but it is making me crazy.

I have a file of users, with the contents in the following format:

Firstuser Lastname,,user1@email.com,Author
Seconduser Lastname,,user2@email.com,Author
Thirduser Lastname,,user3@email.com,Author

What I want to do is to take the first part of the email address, user1, for example, and place between the two empty set of commas.

If I use
Code:
awk -F'@|,' '{print $3}' user.txt
the result is what I want:

user1
user2
user3

And if I use
Code:
for i in `awk -F'@|,' '{print $3}' user.txt`; do echo $1 ; done


The results are again what I'm looking for:

user1
user2
user3

But if use
Code:
for i in `awk -F'@|,' '{print $3}' user.txt`; do sed "s/,,/,$i,/" user.txt; done


Instead of getting

Firstuser Lastname,user1,user1@email.com,Author
Seconduser Lastname,user2,user2@email.com,Author
Thirduser Lastname,user3,user3@email.com,Author

I get

Firstuser Lastname,user1,user1@email.com,Author
Seconduser Lastname,user1,user2@email.com,Author
Thirduser Lastname,user1,user3@email.com,Author

Firstuser Lastname,user2,user1@email.com,Author
Firstname Lastname,user2,user2@email.com,Author
Thirduser Lastname,user2,user3@email.com,Author

Firstuser Lastname,user3,user1@email.com,Author
Seconduser Lastname,user3,user2@email.com,Author
Thirduser Lastname,user3,user3@email.com,Author

Any help in pointing out to me my mistake would be greatly appreciated.

Thanks.


Top
 Profile  
 PostPosted: Fri Nov 22, 2013 10:43 am   

Joined: Fri Nov 22, 2013 9:29 am
Posts: 2
Apparently I'm just making it too hard for myself:

Code:
awk -F'@|,' '{print $1 "," $3 "," $3"@"$4 ","$5}' user.txt


Top
 Profile  
 PostPosted: Fri Nov 22, 2013 10:56 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
for is not meant to be used this way.
You use it for iterating arguments, not command outputs.

Your line prints the file three times because you're using sed(1) for each line,
which in your case prints the whole file after editing the stream -- while you
want all the changes made to the stream to be applied to the file itself.


With awk(1) (only to show it's feasible):
Code:
awk -F, -vOFS=, '!$2 { sub(/@.+$/, "", $3); $2 = $3 } 1' user.txt > user_new.txt # &&
# mv user_new.txt user.txt


With a text editor, ex(1):
Code:
ex -sc '%s/\v(\w+\s+\w+),,([^@]+)(.*)/\1,\2,\2\3/|x' user.txt

The field will be replaced only if it's empty, preserving the possible correct lines present.
You're not handling that alternative with your last solution, though you may not want to.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP