1748238 Members
3686 Online
108759 Solutions
New Discussion юеВ

Re: shell script

 
Jairo Campana
Trusted Contributor

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
9 REPLIES 9
Marvin Strong
Honored Contributor

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.


harry d brown jr
Honored Contributor

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
Marlou Everson
Trusted Contributor

Re: shell script

The cat is not necessary.

awk '{print $2}' file1 file2

Marlou
harry d brown jr
Honored Contributor

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
TwoProc
Honored Contributor

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/^.* //"
We are the people our parents warned us about --Jimmy Buffett
D Block 2
Respected Contributor

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.
Cem Tugrul
Esteemed Contributor

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
Muthukumar_5
Honored Contributor

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!
Nguyen Anh Tien
Honored Contributor

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