Discussion:
Just want to exclude lines with tabs
(too old to reply)
xyz2041
2016-05-19 03:17:45 UTC
Permalink
Hi,

I just want to exclude lines that have tabs on them.

I've tried all of these, but none work.

What am I missing?


grep "$(echo -e \\t)" o.csv
grep "$(printf '\t')" o.csv
grep "\t" o.csv > o-out.csv
grep $',' o.csv > o-out.csv
grep $'\t' o.csv > o-out.csv
grep -P "\t" o.csv > o-out.csv
grep -r ',' o.csv
grep -v "[[:tab:]]" o.csv > o-out.csv
grep -v "\t" o.csv > o-out.csv
grep -v "\t" o.csv > o-out.csv
grep -v $"\t" o.csv > o-out.csv
grep -v $'\t' o.csv > o-out.csv
grep -v '[ \t]' o.csv > o-out.csv
grep -v \t o.csv > o-out.csv
grep -v '\t' o.csv > o-out.csv
grep -v -e "\t" o.csv > o-out.csv
grep -v- -e "\t" o.csv > o-out.csv
grep -v -e '\t' o.csv > o-out.csv
grep -w ',' o.csv > o-out.csv
Eric Blake
2016-05-19 14:42:57 UTC
Permalink
Post by xyz2041
Hi,
I just want to exclude lines that have tabs on them.
I've tried all of these, but none work.
What am I missing?
Grep doesn't interpret \t as a valid escape sequence. This works:

$ printf 'a a\nb\tb\nc c\n' | grep -v $'\t'
a a
c c

if your shell is new enough to have $'' support; otherwise, type a
literal tab in your shell window (often by 'ctrl-v tab' or 'ctrl-v
ctrl-i'), and be sure you properly quote it.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
John Cowan
2016-05-19 14:56:41 UTC
Permalink
Post by xyz2041
What am I missing?
That grep and similar Posix utilities do not understand \t. Instead,
use \011 (the octal code for tab):

$ grep -v '\011' o.csv > o-out.csv

You need to put the \011 in single quotes, otherwise the shell will interpret
the \0 sequence as equivalent to simple 0.
--
John Cowan http://www.ccil.org/~cowan ***@ccil.org
But no living man am I! You look upon a woman. Eowyn I am, Eomund's daughter.
You stand between me and my lord and kin. Begone, if you be not deathless.
For living or dark undead, I will smite you if you touch him.
Paul Eggert
2016-05-19 15:47:19 UTC
Permalink
Post by John Cowan
$ grep -v '\011' o.csv > o-out.csv
That doesn't work, as it's treated like grep -v '011'. Eric had the
right solution.
Karl Berry
2016-05-19 21:21:18 UTC
Permalink
grep "$(printf '\t')" o.csv

It seems to me this should have worked, unless your shell's quoting got
rid of the \ before printf could see it. Certainly this works:

tab=`printf '\t'` # or $(...), whatever
grep "$tab" somefile

This assignment of special characters to shell variables and then using
the variable in subsequent commands is the most portable (across decades
and "improvements" in standards, shells, systems, ...) approach I know
of. FWIW. -k
xyz2041
2016-05-20 03:19:45 UTC
Permalink
Thanks, everyone.

I just couldn't get Windows command line to let me put a
tab on the line, so I wrote a batch file:

set TAB=
grep -v "%TAB%" %1.%2 > %1-NoTabs.csv
grep -v "," %1.%2 > %1-NoCmas.tab

Just hit the tab key after "set TAB=". Not elegant but it works.

grep version:

K:\>grep -V
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

MD5: bb350a9ac2236b3e29f2229e77709f40

Thanks, again!
Post by xyz2041
grep "$(printf '\t')" o.csv
It seems to me this should have worked, unless your shell's quoting got
tab=`printf '\t'` # or $(...), whatever
grep "$tab" somefile
This assignment of special characters to shell variables and then using
the variable in subsequent commands is the most portable (across decades
and "improvements" in standards, shells, systems, ...) approach I know
of. FWIW. -k
Manuel Collado
2016-05-20 16:54:29 UTC
Permalink
Post by xyz2041
I just couldn't get Windows command line to let me put a
set TAB=
grep -v "%TAB%" %1.%2 > %1-NoTabs.csv
grep -v "," %1.%2 > %1-NoCmas.tab
Just hit the tab key after "set TAB=". Not elegant but it works.
Your solution doesn't work in my Win7 machine. The TAB key is tied to
the "filename expansion" feature of the command line.

If you can type a TAB after "set TAB=" and it really generates a tab
character, the you should be able also to type:

grep -v "<TAB>" ....

where <TAB> is the TAB key.

In my case I have to disable the filename expansion feature by launching
a secondary shell:

C:\Users\mcollado>cmd /F:off
Microsoft Windows [VersiĆ³n 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Reservados todos los derechos.

C:\Users\mcollado>grep -v "<TAB>" somefile

And it works.

Loading...