![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ksh - read file with leading spaces | momi | Shell Programming and Scripting | 2 | 03-17-2008 03:02 PM |
| Stripping leading spaces on right justified name | Marcia P | UNIX for Dummies Questions & Answers | 2 | 02-28-2006 10:32 PM |
| Removing leading and trailing spaces of data between the tags in xml. | jhmr7 | UNIX for Dummies Questions & Answers | 2 | 05-18-2005 10:27 AM |
| Strip leading and trailing spaces only in a shell variable with embedded spaces | jerardfjay | Shell Programming and Scripting | 6 | 03-07-2005 02:24 PM |
| Leading and Trailing Spaces | sleepster | Shell Programming and Scripting | 7 | 10-29-2003 11:48 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
Quote:
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 Last edited by lijojoseph; 03-15-2008 at 09:12 AM.. |
|
||||
|
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 ![]() |
|
||||
|
Try this:
Code:
awk '
/^ /{p=p $0;next}
p{print p}
{p=$0}
END{print p}
' file
/^ /{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 |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|