1832785 Members
3410 Online
110045 Solutions
New Discussion

Scripting help

 
SOLVED
Go to solution
Sean OB_1
Honored Contributor

Scripting help

Howdy folks.

I need some help with a shell script.

I have the need to parse a file and set vars to what's in the file.

cat input.lst|while read VAR1 VAR2 rest

This works great except in some cases VAR1 can contain spaces. So in those situations VAR1 gets set to everything before the space and VAR2 to everything after.

Is there a way to define the delimiter? I tried enclosing VAR1 in ' and " in the input file with no change in output.

TIA and points for all responses.

Sean

4 REPLIES 4
Victor Fridyev
Honored Contributor

Re: Scripting help

Hi,

If you can put a delimiter (e.g. : ) between VAR1, VAR2 and rest; so try
cat input.lst.with.delim| awk -F: '{print $1,$2}' | while read VAR1 VAR2; do


Good luck
Entities are not to be multiplied beyond necessity - RTFM
Sean OB_1
Honored Contributor

Re: Scripting help

This is the same result. The read statement is the problem, so awk sends the vars correctly but the read still only picks up the first word before the space and assigns it to the first var.
curt larson_1
Honored Contributor
Solution

Re: Scripting help

the shell uses the environmental variable IFS (internal field separator) to split into fields characters that are read with read.

usually IFS is set to white space (blank, tab,newline). so if you have different delimiter you can set to something else. but you need to be careful because IFS is NOT used only with read. There it is best to set it back.

so you can do something like this:

oldIFS="$IFS"
IFS=","
cat input.lst|while read VAR1 VAR2 rest

IFS="$oldIFS"
H.Merijn Brand (procura
Honored Contributor

Re: Scripting help

First, DO NOT USE cat. It's just an extra unneeded process, very often used by mistake for scripting.

ALL available shells have redirection. That saves you a lot of system resources

NEVER DO

cat file | process

BUT

process < file

Then, all `real' scripting languages support a (input) field seperator. For both awk and perl, it's the '-F' option:

lt09:/home/merijn 110 > cat xx.txt
foo,bar,baz
,bogus,fries
kulu,,yuk
blah,ghur,
,,castor
pollux,,
lt09:/home/merijn 111 > awk -F, '{print $2}' xx.txt
bar
bogus

ghur


lt09:/home/merijn 112 > perl -aF, -nle'print $F[1]' xx.txt
bar
bogus

ghur


lt09:/home/merijn 113 >

Note here that in both cases, I didn't even use a redirection for the input. Both scripting languages take the file(s) to be processed as command line argument(s).

If the delimiting is becoming more complex, don't use -F. If for example, the VAR 1 and VAR2 are defined as to be delimited by the last whitespace on the line, perl would be like

# perl -nle'($VAR1,$VAR2)=(m/^(.*\S)\s+(\S+)$/);print$VAR2,$VAR2' file.txt

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn