LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2006, 09:08 PM   #1
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Rep: Reputation: 15
How to use awk command to parse fields in a line


Hi All,

First of all I just started to learn shell scripting...So, dont be surprised...I have one quick question with regards to parsing the separate fields in a given line(text line). I was able to parse each field for a given line of text regardless of the number of fileds, using the function called function_until(). Now, the for loop basically reads each line of the textfile and passes the line to function function_until. Somebody mentioned to me that I could also use the awk command to parse the fields of each line.

I know a bit of awk command but as far as I know the, I'm only able to use the awk command to parse/extract each field for a given line if only I know exactly how many fields there are in given line. But in a text file, it's a bit unpredictable since we don't know exactly how many fields there are in a given line. Could somebdy please give me suggestion on how to use awk in parsing fields...Thank you in advance...

#!/bin/bash

function_until()
{
until [ -z "$1" ]
do
echo -n "$1"
echo -n " "
echo
shift
done

return 0

}


echo "************Extracting Each Line*************"

text=/home/johnsanty/scripts/textfile



{
for ((a=0 ; a<=2 ; a++))
do
read line1
function_until $line1
done
} < $text

Last edited by johnsanty; 05-22-2006 at 09:53 PM.
 
Old 05-23-2006, 02:05 AM   #2
imagineers7
Member
 
Registered: Mar 2006
Distribution: BackTrack, RHEL, FC, CentOS, IPCop, Ubuntu, 64Studio, Elive, Dream Linux, Trix Box
Posts: 310

Rep: Reputation: 30
Hi johnsanty,

Are you lookin for this:-

ls -l | awk '{ print $3 }'

Which prints the third fields of the following
-rw-rw-r-- 1 aniruddha aniruddh 21903360 Feb 20 16:40 firefox-1.5.0.1.tar
-rw-rw-r-- 1 aniruddha aniruddh 1312768 Feb 21 10:00 abcd.pdf-rw-rw-r-- 1 aniruddha aniruddh 431149 Feb 21 09:53 bds.zip


ie. the owner of the files:-
aniruddha
aniruddha


Have fun
 
Old 05-23-2006, 09:32 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Imagineers7 is right.

However, although I do not quite understand what you might try to achieve, it could be more simple to use awk anyway.

If you want to read a line from a file and echo it, you could do

Code:
awk '{print $0}' yourfile.txt
Now this is a dull example, because you could use 'cat' as well. However, in your awk script you can process any single line you have read. You can even extract the number of fields in each line.

I learned awk from this manual. It took me a few hours to grab the concept, but then it was easy.

jlinkels
 
Old 05-23-2006, 11:56 PM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
awk has got a "spilt" function that returns an array based on a delimiter which could be your field separator ...

HTH

END
 
Old 05-24-2006, 10:33 PM   #5
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by imagineers7
Hi johnsanty,

Are you lookin for this:-

ls -l | awk '{ print $3 }'

Which prints the third fields of the following
-rw-rw-r-- 1 aniruddha aniruddh 21903360 Feb 20 16:40 firefox-1.5.0.1.tar
-rw-rw-r-- 1 aniruddha aniruddh 1312768 Feb 21 10:00 abcd.pdf-rw-rw-r-- 1 aniruddha aniruddh 431149 Feb 21 09:53 bds.zip


ie. the owner of the files:-
aniruddha
aniruddha


Have fun
Hi imagineers,

Thank you so much for replying.

Here's what I did.

#####################################
awk '{ print $1 }' number_lines
echo "The number of line is $1"

#####################################

Basically, the number_lines file contains the following:
~~~~~~~~~~~~~~~~~~~~
3 textfile
~~~~~~~~~~~~~~~~~~~~

The first line of my code works perfectly but I want to extract the value of the $1, which is 3, and use it in my for loop. I tried different things like the following:

Try1:
##########################################
awk '{ temp=$1 }' number_lines
echo "The number of line is $temp"
#########################################

Try2:
##########################################
awk '{ let temp=$1 }' number_lines
echo "The number of line is $temp"
#########################################

Try3:
##########################################
awk '{ $((temp=$1)) }' number_lines
echo "The number of line is $temp"
#########################################


They all failed. Could you give some hints on how to extract that value of $1. thank you in advance...

Last edited by johnsanty; 05-24-2006 at 10:35 PM.
 
Old 05-24-2006, 11:03 PM   #6
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlinkels
Imagineers7 is right.

However, although I do not quite understand what you might try to achieve, it could be more simple to use awk anyway.

If you want to read a line from a file and echo it, you could do

Code:
awk '{print $0}' yourfile.txt
Now this is a dull example, because you could use 'cat' as well. However, in your awk script you can process any single line you have read. You can even extract the number of fields in each line.

I learned awk from this manual. It took me a few hours to grab the concept, but then it was easy.

jlinkels

Hi jlinkels,

Thanks o much..i'll read the manual..are you sure it took you only few hours...


Thanks again...
 
Old 05-25-2006, 12:06 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Grab value like this:
var=`awk '{ print $1 }' number_lines`
echo $var
 
Old 05-25-2006, 08:30 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
To assign a variable the output of a command, you have to use single backquotes (next to the "1" key).

So:
Code:
temp=`awk '{print $1}'  number_lines`
Something tells me you have been using "wc" to count the number of lines in your file, and your piped the output in the file number_lines.

However, if you'd do:

Code:
wc -l < myfile > number_lines
number_lines would only contain the number "3".

Alternatively, you could skip awk and say:
Code:
temp=`wc -l < myfile`
Have you already studied the Advance Bash Scripting Guide?

It'll take you a few hours to grab the concept. A few years before you master it

jlinkels
 
Old 05-25-2006, 09:51 PM   #9
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01
Grab value like this:
var=`awk '{ print $1 }' number_lines`
echo $var

Hi Chrism01,

Thanks a lot..it worked... Thanks to all who helped me...
 
Old 05-25-2006, 09:56 PM   #10
johnsanty
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Hi Jlinkels,

I have to admit...I'm very impressed. Evrything that you said is true. I read the number of lines in a file and put the information in to another file then I tried to extract the number of lines from that file.

BTW, the Sha-Bang that's what I'm currently reading...Thanks again!!! Thanks to all!!!
 
  


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
Awk - get a parameter from the command line benjalien Programming 1 01-24-2006 09:06 AM
Awk command-line arguments lowpro2k3 Programming 1 03-28-2005 09:09 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 11:06 AM
I need to parse a word: awk or sed? mehesque Programming 5 07-27-2004 04:23 PM
search for specific text in fields using awk Helene Programming 2 04-23-2004 12:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:51 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