Home » Infrastructure » Unix » How to grep for TAB
How to grep for TAB [message #97587] Wed, 28 August 2002 08:34 Go to next message
Krish Jayarangan
Messages: 10
Registered: August 2002
Junior Member
Hi,
I have to write a script that takes a command line argument. If that argument contains the TAB character(literally), then I have to echo an error.
Any help would be appreciated. By the way the OS is HP11.00.
ex:
command arg1
where the arg1 is like "MY NAME". The user literally types a TAB between MY and NAME.
Re: How to grep for TAB [message #97590 is a reply to message #97587] Thu, 29 August 2002 09:55 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
This works but I'm sure there's a much easier way.
#!/bin/ksh
VAL1a='MY NAME'        # Space
VAL2a='MY       NAME' # Tab character
VAL1b=`echo $VAL1a | tr '11' ' '`
VAL2b=`echo $VAL2a | tr '11' ' '`

if [ "$VAL1a" != "$VAL1b" ]; then
  echo VAL1a has a tab
fi

if [ "$VAL2a" != "$VAL2b" ]; then
  echo VAL2a has a tab
fi
Re: How to grep for TAB [message #97594 is a reply to message #97587] Thu, 29 August 2002 23:20 Go to previous messageGo to next message
Manolo
Messages: 3
Registered: August 2002
Junior Member
Hello Krish:
One possible solution could be:

test.scr
========

#We type the TAB key literally: grep "#TAB#"
parameter=`echo "$*" | grep " "`

if [[ -z "$parameter" ]]
then
echo "It has a Tab character"
else
echo "It doesn't have a Tab character"
fi

There's one thing to take into account:
If you invoke the script whith the parameters not enclosed in quotation marks the script will not detect the tab character since it is changed to a SPACE character.

I.E.
> test.scr My#TAB#Name
> It doesn't have a Tab character

> test.scr "My#TAB#Name"
> It has a Tab character

Hope this will be usefull.
Re: How to grep for TAB [message #97596 is a reply to message #97590] Fri, 30 August 2002 08:14 Go to previous messageGo to next message
Krish Jayarangan
Messages: 10
Registered: August 2002
Junior Member
Thanks a lot Andrew.
It works fine.
-Krish.
Re: How to grep for TAB [message #97795 is a reply to message #97587] Fri, 31 January 2003 14:22 Go to previous message
Dave Abercrombie
Messages: 1
Registered: January 2003
Junior Member
To grep for a tab, just simply grep for it!

The trick is getting it to grep. In bash, as you are typing the quoted regular expression, hit Ctrl-V then the TAB key to get the TAB in there. In a shell script, getting a literal TAB into the quoted regular expression depends on the editor (Ctrl-Q in emacs). As a last resort, use grep -f and use a pattern file.
Previous Topic: Oracle8i solaris intel
Next Topic: Re: Upgrade from 5.0 to 8.0
Goto Forum:
  


Current Time: Fri Mar 29 10:44:27 CDT 2024