Remove space before a character

Hi guys,

I am new to shell scripting and I have a small problem...If someone can solve this..that would be great

I am trying to form a XML by reading a flat file using shell scripting
This is my shell script

LINE_FILE1=`cat FLEX_FILE1.TXT | head -1 | tail -1`
echo '<PolicyNumber>'${LINE_FILE1:16:12}'</PolicyNumber>' >>$XML_FILE

16 is the start position of the policy number in the flat file and 12 is the number of characters. Policy number is just 5 characters The result I am getting as below

<PolicyNumber>12345 </PolicyNumber>

There is a space coming after the end of policy number (ie., at 6th position), even though I have seven more spaces in the flat file.

I want to remove this space.

If someone could answer that would be really helpful.

Thanks

Use double quotes:

echo "<PolicyNumber>${LINE_FILE1:12:16}</PolicyNumber>"

to get all the spaces in variable $LINEFILE1. If you don't want the spaces in there, trim the range to less characters.
The reason why you were getting exactly one space there, no matter how many are there in your var, is because 'echo' reads strings separated by (any number of) whitespaces; it outputs string separated by one space. You might want to pass only one string to echo; then use the quotes around. E.g.:

$ echo one     two
one two
$ echo "one     two"
one     two

1 Like