1

I am trying to separate a tab-delimited file using awk.

awk -F'\t' -v OFS="\t" '{if ($5=="Pattern") print $0}' My_file

I came into problem that I can't not use tab completion with the option " -v OFS="\t" ". Whenever the command contain " -v OFS="\t" "(whether it is the only option or in combination with other option), I can not use tab completion for filename.

When I use:

awk -F'\t' '{if ($5=="Pattern") print $0}' My_file

The tab completion works fine.

And sometimes when I use the following command:

awk -v OFS='\t' '{if ($5=="Pattern") print $0}' My_file

There will be error:

bash: unexpected EOF while looking for matching `''

bash: syntax error: unexpected end of file

When I paste the filename, but not by tab completion, the command actually works.

Can anyone tell me what I am been doing wrong?

Thank you!

  • 2
    By "auto type-in" I assume you mean tab completion? – jasonwryan Oct 7 '16 at 4:44
  • 1
    Try: awk -v 'OFS="\t"; {if ($5=="Pattern") print $0}' – Hack Saw Oct 7 '16 at 5:54
  • Hi Jason, thanks for the correction. Yes, by auto type-in I mean use tab completion to complete the filename in command line. – Yichao Cai Oct 7 '16 at 6:45
  • When trying awk -v 'OFS="\t"; {if ($5=="Pattern") print $0}' , the tab completion works fine. But it raise another error "awk: 1: unexpected character '.'", if the file name contain ".". I rename the filename with no ".", but now the command takes a long time to run(which it shouldn't be) and I kill it with ctrl+c. – Yichao Cai Oct 7 '16 at 6:53
0

Try this instead :

awk 'BEGIN{FS="\t"; OFS="\t"}{if ($5=="Pattern") print $0}' My_file

If I understood your problem, that should do the trick.

  • Thank you so much! That actually works and the result is correct. Is it necessary to include the file delimiter option in the apostrophes? – Yichao Cai Oct 10 '16 at 8:04
  • I'm not quite sure what happened when you passed the FS value through the -v option, but using the -v option should be used primarly when you want to pass a value that is contained in a shell variable, like : $ awk -v x=$someValue ... if your value is a constant, you can initialize it in a BEGIN block. Anything in such a block in awk will be executed before going through the file. Similarly, there is an END block that is executed once all the file has been read. You can set field separator (FS) and output field separator (OFS) in any block. – Valentin B. Oct 10 '16 at 12:35
  • To answer your question you don't need to set FS and OFS inside the awk instructions, but apparently the presence of \t in your command line seemed to confuse your inline shell editor. You could also try $ myFS='\t'; awk -F"$myFS" -v OFS="$myFS" ..., it might work. – Valentin B. Oct 10 '16 at 12:37

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.