Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-02-2008
Registered User
 
Join Date: Jul 2008
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
Registered User
 
Join Date: Jun 2006
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
Moderator
 
Join Date: Feb 2007
Location: The Netherlands
Posts: 7,503
Thanks: 73
Thanked 473 Times in 452 Posts
Or with sed:


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

    #4  
Old 09-02-2008
Registered User
 
Join Date: Jul 2008
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
Registered User
 
Join Date: Jul 2008
Location: Montreal
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, thanks a lot!
Sponsored Links
Closed Thread

Tags
grep or

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 05:37 AM.