Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK to have each word in a text written on a new line

Status
Not open for further replies.

deepagovind

Technical User
Jan 26, 2004
4
0
0
US
Hello all,

I have a text file that looks like this:

Today, tomorrow, perl,
\birds<7>, \animals<4>, awk, gawk


I wish to write it to a file in this manner

Today,
tomorrow,
perl,
\birds<7>,
\animals<4>,
awk,
gawk


I tried the following:
awk '{sub(" ", "\n", $0)}' space > newline.out

This did not work..

I greatly appreciate all inputs!!

Thanks!
 
The awk way:
awk '{gsub(/ +/,"\n");print}' space > newline.out
The tr way:
tr -s ' ' '\n' <space >newline.out

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
1. Change ouput field separator to newline.
2. For each line, force $0 to be rebuilt with OFS between each field by assigning to $1.
Code:
BEGIN{OFS="\n"}
{$1=$1;print}
 
awk '{ for (x = 1 ; x <= NF ; x ++ ) { print $x } }' file > file.out


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top