remove leading spaces from a line

Hi friends

I need some help,

I have a file which looks as follows

TEMP 014637065 014637065 517502 517502 RTE
517502 517502 RTE
AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635
ANOTHER_TEST 12 5433 FTHH 5653 833
TEST 123 123 3235 5353 353 53 35 353 535 3
YTERS GJK JKLS

when ever there is a leading space on line(here there is a space at the starting of line 2 and 6 which may not be visible in thread), I need to remove that and append to the previous line. This means I want an output file which looks like
TEMP 014637065 014637065 517502 517502 RTE 517502 517502 RTE
AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635
ANOTHER_TEST 12 5433 FTHH 5653 833
TEST 123 123 3235 5353 353 53 35 353 535 3 YTERS GJK JKLS

awk '/^ /{print p $0; p=""; next}
p !~ /^ / && p {print p}
{p=$0}
END{if(!/^ /) {print}}
' file

Regards

Hello Franklin,
I tried with the above option it is giving syntax error for second line
error messages are
*******
awk: syntax error near line 1
awk: bailing out near line 1
*******
also i tried with awk '/^ /{print p$0;next}{p=$0}END{print}' but here it will not consider the lines without a leading space

pls help

Use nawk or /usr/xpg4/bin/awk on Solaris

Regards

Frank,
thanks a lot it is working perfectly with nawk.

here also i have a small problem
if there are leading spaces in 2 consecutive lines then it is not considering the second one..
that is
*** input file
TEMP 014637065 014637065 517502 517502 RTE
517502 517502 RTE
226
AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635
ANOTHER_TEST 12 5433 FTHH 5653 833
TEST 123 123 3235 5353 353 53 35 353 535 3
YTERS GJK JKLS

if leading spaces are there in lines 2 and 3 then the output should look like
TEMP 014637065 014637065 517502 517502 RTE 517502 517502 RTE 2634
AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635
ANOTHER_TEST 12 5433 FTHH 5653 833
TEST 123 123 3235 5353 353 53 35 353 535 3
YTERS GJK JKLS

Can you pls help?

Also can you just explane me the awk code if you have time
Once again many thanks for youe help :slight_smile:

Try this:

awk '
/^ /{p=p $0;next}
p{print p}
{p=$0}
END{print p}
' file

Explaination of the code:

/^ /{p=p $0;next} # concatenate lines that begin with a space with the previous line and read next line

The next commands effect the other lines:

p{print p} # Print the previous line if set
{p=$0} # Set p
END{print p} # There are no more lines, print previous line(s)

Use nawk or /usr/xpg4/bin/awk on Solaris

Regards

Hello Frank,

many thanks for the help. It is working perfectly.

Cheers