Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whitespace Ignored when reading variables

Status
Not open for further replies.

mjm22

Programmer
Nov 11, 2003
10
GB
Hi

I use the below commands in a script to extract the header line from a file, remove the first couple of zeros and then write the value to another file.

hdrLine=`head -1 $REF_FILE`
echo $hdrLine | sed s/"00"//g | read COMP_LINE
echo $COMP_LINE >> $COMPLETE_FILE

The problem I have is that in the header record there are a number of fields of 8 characters that are left justified and padded with spaces (where the value is less the 8 chars). I need to preserve this fixed length but the above drops a lot of the whitespace.

I partly corrected this by quoting the variables in the echo command but the final field in the record still had a probelm and was being truncated. Eventually I used the following...

hdrLine=`head -1 $REF_FILE`
echo "'$hdrLine'" | sed s/"00"//g | read COMP_LINE
echo "$COMP_LINE" | sed s/"'"// >> $COMPLETE_FILE

This works but I am wondering if there is a better and less clunky way?

cheers

Mike
 
Try this :
Code:
head -1 $REF_FILE | sed s/"00"//g >> $COMPLETE_FILE


Jean Pierre.
 

Ah well, actually I missed some lines out from the above post. In between the reading of the header line and the writing to the other file I also do some additional verification and validation on the contents of COMP_LINE before writing it to the file. Otherwise yes, I could have done it like that.

Incidentally there is a typo in the original post. The g switch of the sed command should be on the second instance of sed rather than the first one.

Mike
 
'read' removes leading and trailing spaces because space is a field delimitor.

Try this:

Code:
head -1 $REF_FILE | sed s/"00"// | IFS= read COMP_LINE
echo "$COMP_LINE" >> $COMPLETE_FILE

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top