Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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
share|improve this answer
1  
xargs is a great tool, and If you hadn't mentioned it, I would have, but it does have one drawback in that if you omit the command it substitutes echo (which unlike in some shells, xargs does not have a builtin echo) and -n1 tells xargs to call the command for every arg, so with this example you would have a minimum of six processes involved (the shell to do the io redirection, xargs, and four echos). This is not so bad at this scope, but it is measurably slower at a couple thousand lines output. – hildred Mar 22 at 5:17

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
share|improve this answer
1  
fantastic thanks! – Annemieke Smet Mar 21 at 12:50

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
share|improve this answer

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
share|improve this answer

Using sed:

sed -e 's/\s\{1,\}$//' -e 's/\s\+/\n/g' file.txt > split_file.txt
share|improve this answer
    
Not quite..it would add a trailing newline after each (initial) line.. – heemayl Mar 21 at 12:53
    
Check with multiple lines, you would get a clear case then.. – heemayl Mar 21 at 12:57
    
Could you add an example with your answer.. – heemayl Mar 21 at 12:58
    
@heemayl You meant it would turn trailing spaces into newlines. Your syntax doesn't make it easy to understand. :/ – techraf Mar 21 at 13:21

I just add a Python solution for fun:

python3 -c 'import sys; f=open(sys.argv[1]); print(*f.read().split(),sep="\n")' mytestfile

This 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' ARGUMENTS

The 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.