5

I'm trying to use AWK to place every word within a text document on a new line. I don't really know how to use AWK but I've found some commands online which should solve my problem. I've tried the following commands:

$ awk '{ for (i = 1; i <= NF; i++) print $i }' input.txt > output.txt 

and

$ awk '{c=split($0, s); for(n=1; n<=c; ++n) print s[n] }' input.txt > output.txt

However, both of these commands have the same effect, which is that all spaces are removed.

For clarity, lets say that input.txt contains the text:

The fox jumped over the dog

output.txt should contain:

The
fox
jumped
over
the
dog

However output.txt contains:

Thefoxjumpedoverthedog

I'm using Cygwin on Windows 7 to use these commands. Is there something I'm missing within the commands?

hjalpmig
  • 702
  • 1
  • 13
  • 39
  • That's not reproducible on standard Awk platforms. Maybe your Awk prints Unix line endings and the tool you are using to examine the files doesn't know what to do with them? The Awk scripts are fine; how to get them to run correctly on your platform is the isue here, but that's not really a programing problem. – tripleee Mar 29 '16 at 17:31
  • @tripleee Thanks, I thought it would be something to do with compatibility between Win/Unix systems. – hjalpmig Mar 29 '16 at 17:38
  • ah, compatibility between Windows and Unix, yep. Try `cat -vet file`. If you see `^M$` at the end of lines, the use `dos2unix file` to convert to unix format. (Or if I misunderstand the "direction" of your data, there is also `unix2dos file`. Good luck. – shellter Mar 29 '16 at 17:51
  • Also, pluse-uno for well structured question with small sample data, expected output, current output and ... (wait for it everyone) .... (gasp) some code! . Please keep posting and good luck! ;-) – shellter Mar 29 '16 at 17:52

3 Answers3

5

another alternative

echo "the fox jumped over the dog" | awk -v OFS="\n" '{$1=$1}1'

to read from a file awk ... inputfile

however, I'm not sure it will solve your case. If you awk is broken you can try tr

echo ... | tr ' ' '\n'

will do.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Your first command works for me. How can I change it so that instead of typing the text it reads it from a file, and saves the output to another file? – hjalpmig Mar 29 '16 at 17:45
  • same as you normally do, `awk ... inputfile > outputfile` – karakfa Mar 29 '16 at 17:49
3

According to the manpage, print in awk prints its arguments:

separated by the current output field separator, and terminated by the output record separator

So your first command is ok, but you need to make sure your output record separator is a new line. The default output record separator is a newline, but try making sure:

awk -v ORS='\n' '{ for (i = 1; i <= NF; i++) print $i }' input.txt > output.txt

On Cygwin only, you might be running into issue with Windows/DOS line endings. Try also ORS='\r\n'. Alternatively, pipe the output through unix2dos.

szym
  • 5,606
  • 28
  • 34
-2

You can do this trivially in Perl:

$ echo "The fox jumped over the dog" | perl -ple 's/\h/\n/g'
The
fox
jumped
over
the
dog

Same works in awk:

$ echo "The fox jumped over the dog" | awk '{gsub(/ /,"\n"); print}'
The
fox
jumped
over
the
dog
dawg
  • 98,345
  • 23
  • 131
  • 206