LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-11-2011, 02:35 AM   #1
aggrishabh
Member
 
Registered: Nov 2010
Posts: 87

Rep: Reputation: 1
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
 
Old 10-11-2011, 02:41 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
If file t.t contains data as above, then
Code:
awk '{print $1}' t.t
dumps first col only.
 
Old 10-11-2011, 12:42 PM   #3
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,064

Rep: Reputation: 41
Probably 'cut' command can also be used:

$ cut -d" " -f1 file.txt

Only first column should be printed out.
 
Old 10-11-2011, 09:50 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 03:20 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 10:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 01:21 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration