Remove white space at the beginning of lines | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove white space at the beginning of lines

Shell Programming and Scripting


Tags
grep or

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-02-2008
tipi tipi is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 3 October 2008, 10:39 AM EDT
Location: Montreal
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Remove white space at the beginning of lines

Hi!

I store some data obtained with grep or awk in a file. The problem is that some lines have white space at the begining :

[w h i t e s p a c e]line1
line2
[whitespace]line3

I use something like
grep WORD INFILE >> OUTFILE
awk [script] >> OUTFILE

I would love if it were possible to remove the white whitout parsing the OUTFILE after the grep or awk command. Is there an option like I could put in the grep or awk saying "keep only what is at left of the white space"?

Any better idea?

Thanks a lot,

Tipi
Sponsored Links
    #2  
Old 09-02-2008
ShawnMilo ShawnMilo is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 10 November 2009, 8:27 AM EST
Posts: 252
Thanks: 0
Thanked 1 Time in 1 Post
You could pipe it through a Perl one-liner pretty easily.

| perl -pe 's/^\s+//'

Note: This will remove any leading whitespace. If the line is all whitespace, the \s+ will capture the newline character.
Sponsored Links
    #3  
Old 09-02-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 30 October 2014, 12:48 PM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
Or with sed:


Code:
sed 's/^ *//' file

    #4  
Old 09-02-2008
arunsoman80 arunsoman80 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 2 September 2009, 6:05 PM EDT
Location: New York
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:
cat INFILE | sed 's/^[ \t]*//' > OUTFILE
Sponsored Links
    #5  
Old 09-02-2008
tipi tipi is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 3 October 2008, 10:39 AM EDT
Location: Montreal
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, thanks a lot!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Space at beginning of the line Sara_84 Shell Programming and Scripting 2 03-17-2012 03:22 AM
remove white space from specific columns in text file shelladdict Shell Programming and Scripting 8 05-10-2011 09:41 AM
How to remove white spaces from the beginning an end of a string in unix? mady135 Shell Programming and Scripting 3 10-05-2010 11:39 AM
Extract lines between 2 strings add white space dcfargo Shell Programming and Scripting 4 07-26-2010 12:29 PM
Add new lines with a space in beginning Aejaz UNIX for Advanced & Expert Users 8 05-01-2008 12:21 PM



All times are GMT -4. The time now is 02:07 AM.