Splitting the data and storing it into 2 variables

Hi,

I have a file in the given format:

|aaa |hhh |fat hat chat |make sure

I need to get store in a variable,say 'error' the value of the string 'fat hat chat'

In short,
echo $error should give the result
fat hat chat

Is this possible using awk?

Also, can this be split.For example, I have to store the value of the string (fat hat chat) in to two variables.Say, error1 and error2 (Given field width is predefined)
i.e
error1=fat ha
error2=h chat

Thanks in advance
Js

I have got the answer for my first question:

sed '1q' 1psql.out | awk -F"|" '{print $3}' ( I found this from the forum it self)

But can some one help me with teh second part???

Thanks in advance
Js

Dear jisha,

try this

to get the value of "error=hat fat chat", use this

awk -F'|' '{print $3}' filename

to split a value,
try this

split($3,a,<seperator of the value>)

gud luk

Regards,
Pankaj

Thanks for ur time Pankaj.
Can u please explain me what the 'a' and separator of the value stands for?
I searched in the man page .. But didnt understand any thing ..

Regards,
Js

Can any one tell me the solution for the same question when the given file has fields not separated by the | ???

Thanks in advance
Js

Dear Jisha,

split($3,a,<seperator of the value>)

here a is the array variable. u can use error instead of a
then print like this

print a[1]
print a[2]

u'll cum to know the thing

Regards,
Pankaj

hi there! (i'm at SunOS 5.9)

i'm trying to split a string with *split*, but i can't.

for example:

in shell i do:

$ string='hi go bye'
$ then i try: awk 'split($string, strings, " ")'
(and a get the following)
$ awk: syntax error near line 1
$ awk: illegal statement near line 1

i've tried a lot of choices, with files and more, and i can't get what's going on.

how can i make it work?

try using nawk

Hi,

I have this,

awk '{for(x=1;x<=NF;x++) {array[x] = $x}}' input.txt

where input.txt has only one line (comma deliminated) and I am trying to get the data into an array. For some reason, the array is empty. Can someone please tell me what is wrong with this statement?

Thanks.

@viko: You need to pass the shell variable to awk

$ str="a b c"
$ echo $str
a b c
$ awk -v string="$str" 'BEGIN { split(string,a); for(i in a) print "=> " a; }'
=> b
=> c
=> a

@ycshen: You need to pass comma as the field separator.

awk -F, {...}

thanks agn, that was helpful for me, but i don't know how to use the array back in shell

i know
a=`awk -v string="$str" 'BEGIN { split(string,a); for(i in a) print "=> " a[i]; }'`

but obviously that doesn't bring me the entire array

can someone help me a little more?