pogibas
pogibas

Reputation: 28319

awk print multiple column file into single column

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

Answers (4)

Birei
Birei

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

glenn jackman
glenn jackman

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

Dennis Williamson
Dennis Williamson

Reputation: 359905

Perhaps a bit cryptic:

awk '1' RS='[[:space:]]+' inputfile

It says "print every record, treating any whitespace as record separators".

Upvotes: 4

tonio
tonio

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

Related Questions