You are not logged in.

#1 2006-11-03 21:58:13

nadman10
Member
From: USA
Registered: 2005-10-15
Posts: 202

Need help with shell script

I'm new to scripting and was wondering if anyone had some pointers for me. I have a text file in the following format:

"user1@email.com" TAB "password1"
"user2@email.com" TAB "password2"

That "TAB" is an actual tab of course. I need the file to output like this:

user1@email.com,user1@email.com,password1
user2@email.com,user2@email.com,password2

Any suggestions?

Offline

#2 2006-11-03 22:20:49

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Need help with shell script

cat myfile | awk '{ print $1 "," $1 "," $2 }'

Go, awk, go! (I love how abused awk is...)

Offline

#3 2006-11-03 22:25:07

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Need help with shell script

Here's another option, to be verbose:

fmt () { echo "$1,$1,$2"; }
while read line; do
   fmt $line
done < my_text_file;

Offline

#4 2006-11-04 20:22:27

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: Need help with shell script

sed -r 's|(.*)t(.*)|1,1,2|'

1000

Offline

#5 2006-11-06 13:09:46

nadman10
Member
From: USA
Registered: 2005-10-15
Posts: 202

Re: Need help with shell script

Thanks guys. The first one worked for me. awk is pretty sweet

Offline

#6 2006-11-06 13:44:41

nadman10
Member
From: USA
Registered: 2005-10-15
Posts: 202

Re: Need help with shell script

Ok. How about this:

drpepper@email.com,drpepper@email.com,password,64.227.239.141

Into this:

drpepper@email.com,drpepper@email.com,password,64.227.239.141,drpepper

Any ideas? I'm trying to learn this for an Exchange migration script if anyone was wondering.

Offline

#7 2006-11-06 16:02:44

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Need help with shell script

I would use sed for that one, similar to what byte did... I don't want to give away too much because you said you wanted to learn it.
The idea is that sed maintaines regex matches with () (or () is -r is not specified), which are used in the second expression.  You can also nest these parenthesis.

Here, I'll give you an example to work with:

echo "drpepper@email.com" | sed -r "s|(([^@]*).*)|1,2|"

Offline

#8 2006-11-06 16:07:52

nadman10
Member
From: USA
Registered: 2005-10-15
Posts: 202

Re: Need help with shell script

Thanks phrakture. I'll have to play with this. A coworker came up with this:

cat email.txt | awk -F@ '{print $1"@"$2"@"$3","$1}' > emaildone.txt

Awk seems a little easier with beginners. I would also like to learn a little about sed too.

Offline

#9 2006-11-06 16:33:49

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: Need help with shell script

nadman10 wrote:

I would also like to learn a little about sed too.

http://www.cornerstonemag.com/sed/ should have links enough and you're welcome to bug me ('jra') on irc://chat.freenode.net/#archlinux for hints.


1000

Offline

#10 2006-11-06 16:43:06

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Need help with shell script

nadman10 wrote:

Thanks phrakture. I'll have to play with this. A coworker came up with this:

cat email.txt | awk -F@ '{print $1"@"$2"@"$3","$1}' > emaildone.txt

Awk seems a little easier with beginners. I would also like to learn a little about sed too.

Heh, that's rather interesting, splitting on the @ sign.  I like it.

Awk is easier to get the basics of, but awk itself can get real complicated real fast.  Awk, is, in fact, a full programming language.

Offline

#11 2006-11-06 18:16:16

nadman10
Member
From: USA
Registered: 2005-10-15
Posts: 202

Re: Need help with shell script

I have to give credit to Dale the package maintainer for it. It's his work of art. Thanks to you too byte.

Offline

Board footer

Powered by FluxBB