LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk help (https://www.linuxquestions.org/questions/linux-newbie-8/awk-help-630197/)

i.you 03-24-2008 05:54 AM

awk help
 
Hi

I'd like to split a string and I used awk :

Quote:

#!/bin/sh

string="sun,mon,tue,wed"
awk 'BEGIN { split{string, array, ",") }'
for i in 0 1 2 3
do
echo ${array[i]}
done
Is it wrong?
It doesn't work;...

Any help?

livetoday 03-24-2008 05:57 AM

Is this what you want ??

echo "sun,mon,tue" | sed 's/,/\n/g'

suniL

Quote:

Originally Posted by i.you (Post 3098567)
Hi

I'd like to split a string and I used awk :



Is it wrong?
It doesn't work;...

Any help?


jschiwal 03-24-2008 06:28 AM

echo $string | awk 'BEGIN {FS=","; OFS="\n"}{$1=$1;print $0}'

Inside the awk command, string is an awk variable and not a bash variable.
You could have a bash variable evaluated in the command line. But that isn't the same thing as manipulating a bash variable:
echo | awk 'BEGIN {string='\"$string\"'}{print "the input: " string}'


All times are GMT -5. The time now is 02:21 AM.