LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get fields using awk (https://www.linuxquestions.org/questions/programming-9/get-fields-using-awk-774215/)

ashok.g 12-08-2009 07:26 AM

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,

pixellany 12-08-2009 07:46 AM

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"


catkin 12-08-2009 07:47 AM

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


ashok.g 12-08-2009 08:10 AM

Thanks alot. It's working....!

ghostdog74 12-08-2009 08:15 AM

Quote:

Originally Posted by ashok.g (Post 3784013)
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.

catkin 12-08-2009 10:03 AM

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}
'
#' )


David the H. 12-08-2009 12:17 PM

Quote:

Originally Posted by catkin (Post 3784033)
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"


catkin 12-08-2009 03:29 PM

Quote:

Originally Posted by David the H. (Post 3784292)
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.

David the H. 12-08-2009 04:13 PM

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%.

catkin 12-09-2009 01:21 AM

Quote:

Originally Posted by David the H. (Post 3784545)
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.


All times are GMT -5. The time now is 08:07 PM.