LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 01: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, 01:41 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

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

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

$ cut -d" " -f1 file.txt

Only first column should be printed out.
 
Old 10-11-2011, 08:50 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

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



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 02:20 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

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

All times are GMT -5. The time now is 03:56 AM.

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
Open Source Consulting | Domain Registration