LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Printing a column in a file.. slight confusion with my code (https://www.linuxquestions.org/questions/linux-newbie-8/printing-a-column-in-a-file-slight-confusion-with-my-code-649976/)

pdklinux79 06-17-2008 04:48 PM

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
do
t=$(echo "$line" | awk '{print $5}')
echo t
done < <(~/hlw/fcdt.txt)



output :
admin@hpdl365g1-2:~/hlw$./find_badhashes.sh
/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 .
thanks

forrestt 06-17-2008 05:00 PM

Try:

Code:

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

HTH

Forrest

pdklinux79 06-17-2008 05:03 PM

Forrestt, it worked.. Thanks a lot!!

jschiwal 06-17-2008 06:19 PM

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) )
t2=${t[*]}

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.


All times are GMT -5. The time now is 04:06 PM.