Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-17-2008, 05:48 PM   #1
LQ Newbie
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
Printing a column in a file.. slight confusion with my code

The Text File is cd.txt contains in an order till.cdt.

-r--r--r-- 3 admin admin 10485558 Jun 13 15:26 /data01/cur/000000000000000A.cdt
-r--r--r-- 3 admin admin 10485250 Jun 13 15:30 /data01/cur/0000000000000011.cdt
-r--r--r-- 3 admin admin 10484680 Jun 13 15:33 /data01/cur/0000000000000020.cdt
-r--r--r-- 3 admin admin 10485640 Jun 13 15:36 /data01/cur/000000000000002F.cdt
-r--r--r-- 3 admin admin 10485372 Jun 13 15:41 /data01/cur/000000000000003E.cdt

i want to print each line and then assign 5th column to a variable.
i tried echoing the line and getting the 5th column ... it doesnt work giving the below output.
code i used:
while read line
t=$(echo "$line" | awk '{print $5}')
echo t
done < <(~/hlw/fcdt.txt)

output :
/home/admin/hlw/fcdt.txt: line 1: -r--r--r--: command not found
/home/admin/hlw/fcdt.txt: line 2: -r--r--r--: command not found
/home/admin/hlw/fcdt.txt: line 3: -r--r--r--: command not found

how to get the task done? please let me know .
Old 06-17-2008, 06:00 PM   #2
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99

while read line ; do
    t=$(echo "$line" |awk '{print $5}')
    echo $t
done < ~hlw/fcdt.txt

Old 06-17-2008, 06:03 PM   #3
LQ Newbie
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Forrestt, it worked.. Thanks a lot!!
Old 06-17-2008, 07:19 PM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
You could dispense with the read loop since awk will do that anyway. Simply include the filename after the awk command. If it is the output of "ls -l" then pipe the output through the awk command.

awk '{print $5}' ~hlw/fcdt.txt

t=( $(awk '{print $5}' ~hlw/fcdt.txt) )

The second examples fills an array variable with the values.
The third contains all the values in one variable separated by a space.

Your description asks about assigning the output to a variable, but there are several outputs, so what you want to do with the variable(s) isn't clear. You can also do things inside the awk program such as calculating the total, displaying the minimum value, maximum value, average value, etc.

If you see the read command used in a script, it probably can be rewritten using common filter commands such as sed and awk.

Last edited by jschiwal; 06-17-2008 at 07:28 PM.


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
awk column printing schneidz Programming 7 09-29-2005 07:14 AM
slight confusion with agpgart and DRI xushi Slackware 9 02-04-2005 02:43 AM
132 column printing sachin_keluskar Linux - Software 0 07-17-2004 03:29 AM
major printing confusion shellcode Linux - General 3 04-02-2004 09:29 PM
printing confusion!! figadiablo Slackware 4 04-21-2003 11:03 PM > Forums > Linux Forums > Linux - Newbie

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

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