LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 04: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, 05:26 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
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 05:40 AM.
 
Old 03-22-2010, 05:33 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
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, 06:41 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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-22-2010, 11:08 PM   #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-22-2010, 11:09 PM   #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, 02:18 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

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

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

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