LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-22-2010, 05:06 AM   #1
quanba
Member
 
Registered: Jul 2009
Posts: 41

Rep: Reputation: 15
Thumbs up awk: how can I assign value to a shell variable inside awk?


Hi there,

I have a problem

a="abc:100:200"
I just want to split out the variable "a" into 3 variables:

e.g
echo $a | awk -F: '{
shell_val1=$1;
shell_val2=$2;
shell_val3=$3;
}'

I just don't want to call echo and awk 3 times:
e.g: shell_val1=`echo $a | awk -F: 'print $1'`

Is it possible to call echo and awk just one time (for better performance).

Thanks in advance.
 
Old 03-22-2010, 06:26 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
#!/bin/bash
string="abc:100:200";
IFS=":";
bar=( $string );
for (( i=0; i<${#bar[@]}; i++ )); do
  echo "["$i"] "${bar[$i]};
done;
fits so?

http://www.unix.com/shell-programmin...nto-array.html

Last edited by troop; 03-22-2010 at 06:40 AM.
 
Old 03-22-2010, 06:33 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
For performance do it all in-shell
Code:
c@CW8:~$ a="abc:100:200"
c@CW8:~$ IFS=':'
c@CW8:~$ for x in $a
> do
> echo $x
> done
abc
100
200
c@CW8:~$ unset IFS
Alternatively
Code:
c@CW8:~$ a="abc:100:200"
c@CW8:~$ x=${a%%:*}
c@CW8:~$ echo $x
c@CW8:~$ a=${a#$x:}
c@CW8:~$ echo $a
100:200
c@CW8:~$ y=${a%%:*}
c@CW8:~$ echo $y
100
 
Old 03-22-2010, 07:41 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Hey just in case you still want to use awk:

Code:
a="abc:100:200"

vars=($(awk 'BEGIN{var = "'$a'";gsub(/:/,"\n",var); print var}'))

for x in ${vars[@]}
do
    echo $x
done
 
Old 03-23-2010, 12:08 AM   #5
quanba
Member
 
Registered: Jul 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks you all, your solutions help me alot, not just about the current problem, but the way I write scripts.
 
Old 03-23-2010, 12:09 AM   #6
quanba
Member
 
Registered: Jul 2009
Posts: 41

Original Poster
Rep: Reputation: 15
thanks catkin, your 2nd solution is best fit
 
Old 03-23-2010, 03:18 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
quanba ... if finished with thread, please mark as SOLVED
 
  


Reply


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
Shell variable in awk script rohini_sharma Programming 3 02-22-2010 09:32 AM
awk and shell variable markux Linux - Server 6 01-19-2009 04:54 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 06:19 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM


All times are GMT -5. The time now is 04:46 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration