Re: shell script (350 Views)
Reply
Trusted Contributor
Jairo Campana
Posts: 551
Registered: ‎07-26-2001
Message 1 of 10 (350 Views)

shell script

Hello, I have two file
cat file1
13344 domain0.com
44555 domain1.com
.
.
I need filed 2 domains:
for i in `cat file1|awk '{print $2}'
do
......
done

My output , obtain wiht field 1:

13344 domain0.com
44555 domain1.com

since it could do this
legionx
Honored Contributor
Marvin Strong
Posts: 492
Registered: ‎03-01-2004
Message 2 of 10 (350 Views)

Re: shell script

you seem to be missing the ending `

for i in `cat file1 file2 | awk '{print $2}'`
> do
> echo $i
> done

domain0.com
domain1.com

assuming you mean want the second field from both files.


Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 3 of 10 (350 Views)

Re: shell script

Why complicate things with awk and a for loop?

cat file1 file2 | cut -d" " -f2

live free or die
harry d brown jr
Live Free or Die
Trusted Contributor
Marlou Everson
Posts: 198
Registered: ‎12-12-1996
Message 4 of 10 (350 Views)

Re: shell script

The cat is not necessary.

awk '{print $2}' file1 file2

Marlou
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 5 of 10 (350 Views)

Re: shell script

True,

cut -d" " -f2 file1 file2 ...

cut is at least twice as fast as awk. You can dig a ditch with a shovel, but a backhoe is a real tool.

live free or die
harry d brown jr
Live Free or Die
Honored Contributor
TwoProc
Posts: 2,202
Registered: ‎01-11-1999
Message 6 of 10 (350 Views)

Re: shell script

I like the cut command (Harry's post) best
But just cause you can do it a million ways,
here's another way.

cat file1 | sed -e "s/^.* //"
"[That boy] is about as sharp as a sack of wet mice" - Foghorn Leghorn
Respected Contributor
D Block 2
Posts: 569
Registered: ‎10-05-1999
Message 7 of 10 (350 Views)

Re: shell script

I would suggest, never use a 'cat |' with a filter program.. if you avoid it..

awk '{ print $2 }' file1
Golf is a Good Walk Spoiled, Mark Twain.
Esteemed Contributor
Cem Tugrul
Posts: 964
Registered: ‎12-21-2003
Message 8 of 10 (350 Views)

Re: shell script

Marwin is right you seem missed ` end
and i also prefer to use as Tom's suggestion
awk '{ print $2 }' file1
Goo Luck,
Our greatest duty in this life is to help others. And please, if you can't
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 9 of 10 (350 Views)

Re: shell script

You have not told about file2 and what you are expecting from both files?

To get second field from a file you can use awk or cut as,

awk '{ print $2 }' file1
cut -d" " -f2 file1

hth.
Easy to suggest when don't know about the problem!
Honored Contributor
Nguyen Anh Tien
Posts: 923
Registered: ‎05-20-2004
Message 10 of 10 (350 Views)

Re: shell script

I still can get field 2 on my server
#cat temp
44555 domain1.com
44555 domain1.com
44555 domain1.com
44555 domain1.com
44555 domain1.com
# cat temp|awk '{print $2}'
domain1.com
domain1.com
domain1.com
domain1.com
domain1.com
YOU CAN SET DELIMETER BY -F LIKE THIS
#cat temp|awk -F ' ' '{print $2}'
domain1.com
domain1.com
domain1.com
domain1.com
domain1.com
#cat temp|awk -F '.' '{print $2}'
com
com
com
com
HTH
tienna
HP is simple
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.