The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-15-2008
lijojoseph lijojoseph is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
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
  #2 (permalink)  
Old 03-15-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk '/^ /{print p $0; p=""; next}
p !~ /^ / && p {print p}
{p=$0}
END{if(!/^ /) {print}}
' file
Regards

Last edited by Franklin52; 03-15-2008 at 07:34 AM.. Reason: modifation code
  #3 (permalink)  
Old 03-15-2008
lijojoseph lijojoseph is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
Quote:
Originally Posted by Franklin52 View Post
Code:
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

Last edited by lijojoseph; 03-15-2008 at 09:12 AM..
  #4 (permalink)  
Old 03-15-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Use nawk or /usr/xpg4/bin/awk on Solaris

Regards
  #5 (permalink)  
Old 03-15-2008
lijojoseph lijojoseph is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
Smile

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
  #6 (permalink)  
Old 03-15-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
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
  #7 (permalink)  
Old 03-17-2008
lijojoseph lijojoseph is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
Hello Frank,

many thanks for the help. It is working perfectly.

Cheers
Sponsored Links
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0