LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-08-2009, 07:26 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
get fields using awk


I have a shell local variable like this:
Quote:
value=18:46:52
How can I assign the fields in the variable to different variables like a, b, c which will look like as:
Quote:
a=18 b=46 c=52
I tried it as :
Quote:
echo $value|awk `{BEGIN{FS=:}{a=$1;b=$2;c=$3}`
But it's not working. Can you help me?

Thanks,
 
Old 12-08-2009, 07:46 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Does AWK have the ability to assign fields? (The man page will tell you)

This:
Quote:
a=$1;b=$2;c=$3
--if it were to do anything, it would assign the 3 field values to the variables a,b,c
But--awk returns field values with--eg-- print $1, so I'm not sure what the quoted syntax does.

How about this:
Code:
a=18
b=46
c=52
value="$a:$b:$c"
 
Old 12-08-2009, 07:47 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
c:~$ value=18:46:52
c:~$ IFS=':' array=( $value )
c:~$ unset IFS    # Effectively set back to default
c:~$ a=${array[1]}
c:~$ echo $a
46
 
Old 12-08-2009, 08:10 AM   #4
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Thanks alot. It's working....!
 
Old 12-08-2009, 08:15 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ashok.g View Post
I have a shell local variable like this:

How can I assign the fields in the variable to different variables like a, b, c which will look like as:

I tried it as :

But it's not working. Can you help me?

Thanks,
you got your syntax wrong..
Code:
echo $value | awk 'BEGIN{FS=":"}{a=$1;b=$2;c=$3}'
please read the awk manual again.
 
Old 12-08-2009, 10:03 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If shell variables are to be set and awk is preferred then awk can be used to write shell variable assignments for the shell to execute using eval
Code:
#!/bin/bash

value='18:46:52'

eval $( echo "$value" | /usr/bin/awk -F ':' '
    { print "a=" $1 " b=" $2 " c=" $3}
' )

echo "DEBUG: a is $a, b is $b and c is $c"
The technique of embedding awk in shell scripts can be very useful, for example when a lot of string data has to be handled (awk runs a lot faster than shell script). For debugging the first and last lines can be changed so the generated assignment statements can be viewed
Code:
#eval $( echo "$value" | /usr/bin/awk -F ':' '
echo "$value" | /usr/bin/awk -F ':' '
    { print "a=" $1 " b=" $2 " c=" $3}
'
#' )
 
Old 12-08-2009, 12:17 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by catkin View Post
Code:
c:~$ unset IFS    # Effectively set back to default
Unsetting IFS is not exactly the same as setting it back to default. The differences are still a bit unclear to me, but it could lead to problems in specific circumstances. I think it mostly affects how the shell treats null separators.

Better to store the original IFS away, then restore it when you're finished.
Code:
oldIFS="$IFS"

<your actions>

IFS="$oldIFS"
 
Old 12-08-2009, 03:29 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by David the H. View Post
Unsetting IFS is not exactly the same as setting it back to default. The differences are still a bit unclear to me, but it could lead to problems in specific circumstances.
It is not exactly the same because IFS does not exist any more but it is functionally (effectively) the same as documented in the links in this LQ post.
 
Old 12-08-2009, 04:13 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Hmm. I must've missed your last response on that thread somehow. I know it was that thread I was thinking about that prompted me to post here. So I guess it's safe. But it would still be nice to confirm it 100%.
 
Old 12-09-2009, 01:21 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by David the H. View Post
But it would still be nice to confirm it 100%.
Truly. It is unfortunate that the documentation is not conclusive and only implies it. We are left having to confirm it by usage -- until anyone reports an exception -- unless someone analyses the source code.
 
  


Reply

Tags
awk, fields



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 question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
AWK won't separate fields slinx Programming 2 03-10-2009 04:11 PM
modify all fields in awk tostay2003 Programming 16 08-09-2008 01:41 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 11:06 AM

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

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