LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk help (https://www.linuxquestions.org/questions/linux-newbie-8/awk-help-907520/)

aggrishabh 10-11-2011 01:35 AM

Awk help
 
Hi All,

I have a file "host" with three columns in it like below and i tried below double FOR loop to print 1 column in one iteration. i use two/three ways but all fails with no error/output. please let me know where i am wrong. please suggest if there is any other way to do this.

file with 3 columns
a b c
a b c
a b c
a b c
a b c
.
.
.


method 1

for i in 1 2 3
do
for HOSTS in `awk -v var=$i '{print var}' host|grep -i VHL..WX|sed 's/^#//'`
do
echo $HOSTS
............
............
............
done
done

method 2

for i in 1 2 3
do
for HOSTS in `awk '{print var}' var=$i host|grep -i VHL..WX|sed 's/^#//'`
do
echo $HOSTS
............
.............
...............
done
done

chrism01 10-11-2011 01:41 AM

If file t.t contains data as above, then
Code:

awk '{print $1}' t.t
dumps first col only.

rng 10-11-2011 11:42 AM

Probably 'cut' command can also be used:

$ cut -d" " -f1 file.txt

Only first column should be printed out.

chrism01 10-11-2011 08:50 PM

Possibly, but cut requires exactly 1 char (in this case space) as the delimiter; awk uses any amt of contiguous whitespace, so a bit more flexible.
Without code tags we can't be sure of the data spacing ;)


All times are GMT -5. The time now is 01:12 AM.