Reputation: 28319
My file looks like that:
315
717
461 737
304
440
148 206 264 322 380 438 496
801
495
355
249 989
768
946
I want to print all those columns in a single column file (one long first column).
If I try to
awk '{print $1}'> new_file;
awk '{print $2}' >> new_file
There are white spaces in between. How to solve this thing?
Upvotes: 1
Views: 4469
Reputation: 36252
A perl
solution:
perl -pe 's/\s+(?=\S)/\n/g' infile
Output:
315
717
461
737
304
440
148
206
264
322
380
438
496
801
495
355
249
989
768
946
Upvotes: 1
Reputation: 246754
You don't need as much as sed
for this: just translate spaces to newlines
tr ' ' '\n' < file
tr
is purely a filter, so you have to redirect a file into it.
Upvotes: 3
Reputation: 359905
Perhaps a bit cryptic:
awk '1' RS='[[:space:]]+' inputfile
It says "print every record, treating any whitespace as record separators".
Upvotes: 4
Reputation: 10541
You can simply use something like:
awk '{ for (i=1; i<=NF; i++) print $i }' file
For each line, iterate through columns, and print each column in a new line.
Upvotes: 3