Trim whitespace and add line break


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim whitespace and add line break
# 1  
Old 01-22-2007
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  
Old 01-22-2007
Code:
tr -s ' ' '\n' < myFile

# 3  
Old 01-22-2007
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  
Old 01-22-2007
Code:
awk ' { for(i=1;i<=NF;++i) print $i } ' file

# 5  
Old 01-22-2007
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  
Old 01-22-2007
Code:
sed "s/  */\\
/g" f | sed "/^ *$/d"

# 7  
Old 01-22-2007
Or:

printf "%s\n" $(<file)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace a whitespace with line break?

Hello, I am a beginner in Unix and I'm learning how to program by myself. I am trying to create a shell script that is able to take in either a file containing input or by standard input, and replacing every whitespace with a new line break. so basically the input would be This <SP> is... (10 Replies)
Discussion started by: fozilla
10 Replies

2. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

3. UNIX for Dummies Questions & Answers

insert whitespace (tab) at start of each line

Hi all, I have this: begin data; dimensions nind=168 nloci=6; info BDT001.4 ( 1 , 1 ) ( 1 , 12 ) BDT003.4 ( 1 , 1 ) ( 12 , 12 ) BDT007.4 ( 1 , 1 ) ( 12 , 12 ) BDT009.4 ( 1 , 32 ) ( 12 , 22 ) etc, etc And need this: begin data; dimensions nind=168 nloci=6; info ... (2 Replies)
Discussion started by: MDeBiasse
2 Replies

4. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

5. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Shell Programming and Scripting

Trim a new line

Okay, I am trying to make a bash script to get a certain domains IP address (my home ip). My home is on a DHCP lease from my ISP, so I cannot always trust the IP address to remain constant. This is what I have so far for it: alias ip-home="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 |... (5 Replies)
Discussion started by: tnanek
5 Replies

8. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

9. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

10. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies
Login or Register to Ask a Question