Set problem with spaces

#### ~]$ set "hello 'cat dog walk' money elephat" 
#### ~]$ echo $* | awk '{print $2}'
'cat

why is the second command above showing only

"'cat 

?

shouldn't the output be:

'cat dog walk' 

how can i fix this so it gives me the chosen column in its entirety?

Do you want to put the string( hello 'cat dog walk' money elephat ) in a variable?:

$ v="hello 'cat dog walk' money elephat"
$ echo $v
hello 'cat dog walk' money elephat

in this particular instance, i need to use set to set the variables. it looks as though the set command has issues accepting values between quotes as one single value.

If I understand what you are trying to do this is another way:

IFS=","
myvars="hello,cat dog walk,money,elephat"
for v in $myvars
do
  echo $v
done
1 Like

The command:

set "hello 'cat dog walk' money elephat"

sets $# to 1 and $1 to hello 'cat dog walk' money elephat .

The command:

echo $* | awk '{print $2}'

with its default field separator set to split fields on combinations of one or more spaces or tabs has field 1 set to hello , field 2 set to 'cat , field 3 set to dog , etc. and the awk command should print:

'cat

I have no idea how you got a leading " nor a trailing space from that command.

Without awk, the following shell commands might do what you want:

set hello 'cat dog walk' money elephat
printf "args=%d, arg1=%s, arg2=%s, arg3=%s, arg4=%s\n" $# "$1" "$2" "$3" "$4"

should print:

args=4, arg1=hello, arg2=cat dog walk, arg3=money, arg4=elephat

assuming that you're using a standards conforming shell such as bash or ksh.

1 Like

Try

set hello 'cat dog walk' money elephat
echo "$@" | awk '{print $2}'

As Don suggested, simpler than the awk thing is

echo "$2"
1 Like

@MadeinGermany: that will provide cat . To use awk ( instead of printf "%s\n" "$2" ) we need to get around the field splitting of awk with something like:

printf "%s\n" "$@" | awk '{print $2}' FS='\n' RS=�

or just:

printf "%s\n" "$@" | awk NR==2
2 Likes

unfortunately, non of these are working.

here's how i'm doing it.

i have to set the variable this way:

set $(cat myvars)

then get the fields this way:

echo $* | awk/print whatever column i need

or

echo $@ | awk/print whatever column i need.

the spaces are being a major pain

ihave to do this on a linux/sunos host. linux hosts are ubuntu/centos/red hat.

That is because in addition to your original problem specification you are using command substitution: $( ... ) which will cause the output of the command to be split into fields according to the IFS variable, which defaults to a space, a TAB and a newline. The same would go for variable expansion.

There is a difference between:

$ set -- hello 'cat dog walk' money elephant
$ echo "$2"
cat dog walk

and

$ var="hello 'cat dog walk' money elephant"
$ set -- $var
$ echo "$2"
'cat

in the first case there are 4 fields, and no field splitting is performed
in the second case there is field splitting after expansion of the variable "var", which results in 6 fields with the default IFS

--
You could try something like this:

$ oldIFS=$IFS
$ IFS="
"
$ set -- $(xargs -n1 < file)
$ IFS=$oldIFS
$ echo "$2"
cat dog walk
$
1 Like

this worked. thank you so much!!!!

This should work as well:

IFS="
" set -- $(xargs -n1 < infile2443)
echo "$2"

That will not work, set does not use IFS.