How to convert from row to column?
I have a .txt file with numbers ordered like this (on the same row):
106849_01373 106849_01967 106850_00082 23025.7_01059
I would like to convert them like that:
106849_01373
106849_01967
106850_00082
23025.7_01059
I have no idea which command to use. Can somebody help me with this?
Pretty easy with tr
:
tr -s '[:blank:]' '\n' <file.txt
Example:
% cat file.txt
106849_01373 106849_01967 106850_00082 23025.7_01059
106849_01373 106849_01967 106850_00082 23025.7_01059
106849_01373 106849_01967 106850_00082 23025.7_01059
106849_01373 106849_01967 106850_00082 23025.7_01059
% tr -s '[:blank:]' '\n' <file.txt
106849_01373
106849_01967
106850_00082
23025.7_01059
106849_01373
106849_01967
106850_00082
23025.7_01059
106849_01373
106849_01967
106850_00082
23025.7_01059
106849_01373
106849_01967
106850_00082
23025.7_01059
Here is one with xargs
xargs -n1 < file.txt
Demo:
$ cat file.txt
106849_01373 106849_01967 106850_00082 23025.7_01059
$ xargs -n1 < file.txt
106849_01373
106849_01967
106850_00082
23025.7_01059
heemayl's answer is the way to go, however here's an alternative using Perl:
perl -lane '$,="\n"; print(@F)' file.txt
-
-l
: enables automatic line-ending processing. It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted, sets $\ to the current value of $/. -
-a
: turns on autosplit mode when used with a -n or -p. An implicit split command to the @F array is done as the first thing inside the implicit while loop produced by the -n or -p. -
-n
: causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed -n or awk:LINE: while (<>) { ... # your program goes here }
-e
: may be used to enter one line of program;-
$,="\n"
: sets the output field separator to a newline; -
print(@F)
: prints the fields separated by the output field separator.
% cat file.txt
106849_01373 106849_01967 106850_00082 23025.7_01059
% perl -lane '$,="\n"; print(@F)' file.txt
106849_01373
106849_01967
106850_00082
23025.7_01059
AWK
approach. Basically changing the output separator for fields, and looping. The test file is your example pasted over and over with ENDLINE at the end
$ awk 'BEGIN{OFS="\n"}{for(i=1;i<=NF;i++) print $i}' some_data
106849_01373
106849_01967
106850_00082
23025.7_01059
ENDLINE
106849_01373
106849_01967
106850_00082
23025.7_01059
ENDLINE
106849_01373
106849_01967
106850_00082
23025.7_01059
ENDLINE