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_01059I have no idea which command to use. Can somebody help me with this?
6 Answers
Pretty easy with tr:
tr -s '[:blank:]' '\n' <file.txtExample:
% 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 0 Here is one with xargs
xargs -n1 < file.txtDemo:
$ 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 1 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 Using sed:
sed -e 's/\s\{1,\}$//' -e 's/\s\+/\n/g' file.txt > split_file.txt 4 I just add a Python solution for fun:
python3 -c 'import sys; f=open(sys.argv[1]); print(*f.read().split(),sep="\n")' mytestfileThis command runs the one-line Python 3 script in 'single quotes' with the file name you want to convert as argument in the end. The syntax is like this:
python3 -c 'PYTHON_COMMAND_OR_1-LINE-SCRIPT' ARGUMENTSThe 1-line-script we use is this (expanded to multiple lines for clarity):
import sys
f=open(sys.argv[1])
print(*f.read().split(),sep="\n")It imports the sys module to read command-line arguments, takes the first given argument as file name to open and prints each whitespace separated data chunk from the file in a single line.
$ cat mytestfile
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
$ python3 -c 'import sys; f=open(sys.argv[1]); print(*f.read().split(),sep="\n")' mytestfile
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