Search code examples
bashawksedcuttr

Why is this code not replacing spaces with newlines?


I have a file like this:

17336   GRMZM2G000118   GRMZM2G000118  
190207  GRMZM2G404941  GRMZM2G404922   GRMZM2G104854   
307596  GRMZM5G800488   GRMZM5G800488 GRMZM2G152059   GRMZM2G152059   GRMZM2G152028   GRMZM2G152028

line delimited by space
I was trying to cut fields from the second field till end, and convert it to be one column:
output should be

GRMZM2G000118
GRMZM2G000118
GRMZM2G404941
GRMZM2G404922   
GRMZM2G104854
...

using this command:

cut -f 2- < input.txt | tr " "  "\n" > new_file.txt

but it does not work

The only command work to solve it is:

 awk '{for(i=2;i<=NF;++i)print $i}'  

Or

 sed  's/\s\+/\n/g'

My question is why the first way do not work?


Solution

  • I have file like this:

    17336   GRMZM2G000118   GRMZM2G000118  
    190207  GRMZM2G404941  GRMZM2G404922   GRMZM2G104854   
    307596  GRMZM5G800488   GRMZM5G800488 GRMZM2G152059   GRMZM2G152059   GRMZM2G152028   GRMZM2G152028
    

    line delimited by space I was trying to cut fields from the second field till end, and convert it to be one column: output should be

    GRMZM2G000118
    GRMZM2G000118
    ...
    

    using this command:

    cut -f 2- < input.txt | tr " "  "\n" > new_file.txt
    

    My question is why the first way do not work?

    You have two problems here:

    • the default delimiter for cut is a TAB.
    • multiple spaces are not squeezed together with cut, contrary to what awk does.

    To address the first problem, set the delimiter with -d ' '. To address the second one, squeeze the spaces with tr -s ' ':

    $ echo "hello     how are you" | cut -f2-
    hello     how are you
    $ echo "hello     how are you" | cut -d' ' -f2-
        how are you
    $ echo "hello     how are you" | tr -s ' ' | cut -d' ' -f2-
    how are you
    

    All together your command should be:

    tr -s ' ' < input.txt | cut -d ' ' -f 2- | tr " "  "\n" > new_file.txt
    

    So now you have a bunch of commands, time to move to the awk version : )