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
echo seems to trim whitespace joeyg Shell Programming and Scripting 2 02-28-2008 04:41 PM
TO break a line aajan Shell Programming and Scripting 7 08-28-2007 04:12 AM
To Trim spaces at the end of line sbasetty Shell Programming and Scripting 1 01-31-2007 10:01 PM
Trim trailing spaces from each line in a file tipsy Shell Programming and Scripting 5 09-13-2006 05:48 AM
trim whitespace? msteudel Shell Programming and Scripting 4 07-07-2005 07:57 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 01-22-2007
moose1 moose1 is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 2
Trim whitespace and add line break

All,

I'm a newbie at shell scripting and regular expressions and I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but without much success beyond that. I appreciate any assistance.

################
ex:

HTML Code:
dog       cat        moose        telephone
     house       red      talk
balloon    deer         banana
################

Thanks so much in advance.
Moose
  #2 (permalink)  
Old 01-22-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
tr -s ' ' '\n' < myFile
  #3 (permalink)  
Old 01-22-2007
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Here's a crude way using awk:
Code:
awk '{ gsub("^ *","",$0); gsub(" *$","",$0); gsub("  *"," ",$0); gsub(" ","\n",$0); print $0 }' $file
Removes leading and trailing whitespace, replaces multiple consecutive spaces with one space, then replaces the remaining spaces with newlines.
  #4 (permalink)  
Old 01-22-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
awk ' { for(i=1;i<=NF;++i) print $i } ' file
  #5 (permalink)  
Old 01-22-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by moose1
[snip]
I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but without much success beyond that. I appreciate any assistance.

################
ex:

Code:
dog       cat        moose        telephone
     house       red      talk
balloon    deer         banana
################

[snip]

awk '$1=$1' RS=" " infile # use nawk on Solaris

or

/usr/xpg4/bin/awk NF RS=" " infile # on Solaris

or

gawk NF RS=" " infile
  #6 (permalink)  
Old 01-22-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed "s/  */\\
/g" f | sed "/^ *$/d"
  #7 (permalink)  
Old 01-22-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Or:

printf "%s\n" $(<file)
Closed Thread

Bookmarks

Tags
awk, awk trim, regex, regular expressions, trim, trim awk

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 08:07 PM.


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