0

I made a file with tab separated numbers on 2 lines.

mac$ cat tab_sep_file.tsv 
1   2   3
4   5   6

Are they real tabs or spaces, I hear you ask. Yes, they are real tabs:

mac$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> with open('tab_sep_file.tsv') as f:
...     a = f.read()
... 
>>> print repr(a)
'1\t2\t3\n4\t5\t6\n\n'
>>> 

Let's print out a single column

mac$ cat tab_sep_file.tsv | awk -F "\t" "{print $1}"
1   2   3
4   5   6

Why doesn't this work? I've tested it on several TSVs

LittleBobbyTables
  • 4,361
  • 9
  • 38
  • 67
  • Possible duplicate of [awk single or double quote usage](http://stackoverflow.com/questions/23908267/awk-single-or-double-quote-usage) – mklement0 May 17 '17 at 02:41
  • 2
    This has nothing to do with awk, it's a shell issue. Just follow normal shell quoting rules - always use single quotes unless you **NEED** double quotes for some specific purpose, and the use double quotes untile you **NEED** no quotes for some other purpose. Learn the difference between sing, double, and no quotes in shell scripts. – Ed Morton May 17 '17 at 03:26

2 Answers2

1

Use single quotes in the Awk expression like this: '{print $1}'. With double quotes $1 expands to null.

Also, there's no need to use the -F option when whitespace (spaces and tabs) are field separators by default.

Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31
1

Put your print statement in single quotes.

echo $'a\tb' | awk -F "\t" '{print $1}'

You also technically don't need the tab separator here. awk will handle it automatically.

echo $'a\tb' | awk '{print $2}'
Andy Ray
  • 30,372
  • 14
  • 101
  • 138