Advice using cut & echo combination commands

Hi,
I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space.
Example:

cat filea | while read line
do
  field1=`echo $line | cut -c1-2`
  field2=`echo $line | cut -c10-20`
  field3=`echo $line | cut -c21-35`
  echo $field1$field2$field3 >>outfile
done

Both field2 & field3 contain variable length text, the unused portion being space filled.

Thanks
Dave

Hi,

Post your sample input file and expected output.

Hi,
If input record is

 
123456789EXAMPLE    DESCRIPTION    1234

I want

12EXAMPLE    DESCRIPTION    

(with 3 spaces betwee EXAMPLE & DESCRIPTION & 4 spaces at end)

but I get

12EXAMPLE DESCRIPTION       

(with 1 space between EXAMPLE & DESCRIPTION & 1 space at end)

ie multiple spaces are automatically reduced to 1 space !

nawk '{print substr($0,1,2) substr($0,10,10) substr($0, 21,14)}' filea > outfile
1 Like

Just add quotes to the last echo

 echo "$field1$field2$field3" >>outfile
1 Like
#!/bin/bash
while read -r LINE
do
  echo "${LINE:0:2}${LINE:9:10}${LINE:20:35}"
done <"file"

2 Likes

reserve the spaces before and after DESCRIPTION.

echo "123456789EXAMPLE    DESCRIPTION    1234" |cut -c1-2,10-35

12EXAMPLE    DESCRIPTION
1 Like