I have a file like this which is a two-column tab-separated file.


I want to get an output like this:


I was trying to use the fold command inside awk. Is it possible to use another command within awk?

Also, the width of each line I want is 15, so I tried something like this, but it didn't work:

awk -F "\t" '{a=$(fold -w 50 $1);print a,$2}' file.txt 

How can I do this?

  • Your example output is neither two column nor is it clear where the TAB would be. – Anthon Mar 10 '15 at 18:58
  • I changed it. My input file is 2 column tab separated. I never said out put to be a 2 column. Sorry if it caused a confusion – user3138373 Mar 10 '15 at 19:01

With python test.py < input and test.py:

import sys
for i in sys.stdin:
     s, ident = i.rstrip().split()
     print '>{0}'.format(ident)
     while s:
          print s[:15]
          s = s[15:]
  • Hi Anthon Anything in awk?? I just want to use another command within awk. Thanks for the solution – user3138373 Mar 10 '15 at 18:58
  • @user3138373 Sorry, I don't think my awk skills will get you anything useful, nor am I sure you can call other commands from awk. – Anthon Mar 10 '15 at 19:07

Here are a couple of ways:

  1. Perl

    perl -ane '$F[0]=~s/.{15}/$&\n/g; print ">$F[1]\n$F[0]\n"' file 
  2. awk

    awk '{i=0; printf ">%s\n",$2;
                printf "%s\n", substr($1,i,15);i+=15
            }}' file

If you really want to use fold within awk, you could do

awk '{printf ">%s\n",$2; system("echo " $1 "| fold -w 15 ") }' file

Your attempt failed because $() is a shell thing, not an awk thing. To run system commands from within awk, you need to use system(). Then, in order to pass the value of $1 (the sequence) and not the actual string $1 to the shell (if you do, the shell will try and evaluate it and it will return a blank since $1 is not set), you need to exclude the $1 from the quotes.

So, in this example, I am using

               |-------------------------> closing quotes for the 1st part
               |                    |----> closing quotes for the 2nd part
               v                    v   
system( " echo "  $1  " | fold -w 15")
        - ----    --- - ------------
        |  |       |  |       |----------> the 2nd part
        |  |       |  |------------------> opening quotes for the 2nd part       
        |  |       |---------------------> The awk variable, `$1`, 
        |  |                               outside the quotes.         
        |  |-----------------------------> The 1st part       
        |--------------------------------> opening quotes for the 1st part
  • Thanks for the answer, can you explain the answer with fold command?? – user3138373 Mar 10 '15 at 19:05
  • @user3138373 $() is a shell construct, not awk. To call a system command with awk, you use system(). The quoting is funky to protect $1 from being expanded by the shell. – terdon Mar 10 '15 at 19:08
  • Thanks terdon. Also I tried removing double quotes ; something like this and it gave me error so what are those quotes doing awk '{printf ">%s\n",$2; system(echo "$1" | fold -w 15 ) }'file.txt – user3138373 Mar 10 '15 at 19:12
  • @user3138373 see updated answer, is that clearer? – terdon Mar 10 '15 at 19:14
  • It's a little unclear; it means echo was in double quotes and "|fold -w 15" in double quotes?? I though $1 is in double quotes. Looking more carefully – user3138373 Mar 10 '15 at 19:18
awk '{ print ">"$2 ; while (length($1)) { print substr($1,1,15) ; $1=substr($1,16) } }'
  • Nice, +1. I recommend using printf though since print ">",$2 will add a space between the > and $2. – terdon Mar 10 '15 at 19:05
  • terdon, removing the comma in print serves as well to achieve that. – Janis Mar 10 '15 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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