Command execution in grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command execution in grep
# 1  
Old 10-05-2010
Command execution in grep

I'm taking Unix Scripting course and we've been given the following command

Code:
grep -l "`echo '\t'`" foo

What this command is basically doing is giving the lines in the ordinary file foo that contains the letter 't'.

From my understanding, command substation occurs first. So echo '\t' is executed first.

Now, the command looks like this
Code:
grep -l "\t" foo

However, the double quotes don't save backslashes so the \t turns into just the character t which means now it is simply looking for that.

Now, this is what the question was about; to answer what the command is really doing.

However, I'm lost when I try to find a way to change this command to show simply \t

If I change the code to this
Code:
grep -l "`echo '\\t'`" foo

it still does the same thing.

However, when I change it to this
Code:
grep -l "`echo '\\\t'`" foo

then it tries to find \t but I don't see why it needs three backslahes.

Can anyone explain the logic in why it needs three and not two backslahes?
# 2  
Old 10-05-2010
From: Bash Guide Guide for Beginners - Quoting characters

answer in bold:

Using double quotes the literal value of all characters enclosed is preserved, except for the dollar sign, the backticks (backward single quotes, ``) and the backslash.

The dollar sign and the backticks retain their special meaning within the double quotes.

The backslash retains its meaning only when followed by dollar, backtick, double quote, backslash or newline. Within double quotes, the backslashes are removed from the input stream when followed by one of these characters. Backslashes preceding characters that don't have a special meaning are left unmodified for processing by the shell interpreter.

A double quote may be quoted within double quotes by preceding it with a backslash.
# 3  
Old 10-05-2010
What turk451 said is correct. To add just a bit more.....

You need to ultimately pass \\t to grep. If the search string given to grep has \t then grep will match a tab. Passing the double slant to grep is necessary to escape it such that grep treats it as a two character pattern.

Quote:
Code:
grep -l "\t" foo

However, the double quotes don't save backslashes so the \t turns into just the character t which means now it is simply looking for that.
In this case the shell is passing the backslant, but it is grep that is interpreting it as a tab rather than two characters.

As for why you need three backslants...
When the shell sees \\\t it replaces the first two with a single slant, and because \t isn't a special case it leaves it alone resulting in \\t being passed to grep which is exactly what you need.

Last edited by agama; 10-05-2010 at 10:24 PM.. Reason: typo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Execution of command at command prompt

Hello Experts, I am still learning linux and have come across a question, hope to get some answer. I have two servers, and both have the same version of svn client installed and both have the same user_id. my SVN client version: svn, version 1.6.11 (r934486) compiled Mar 2 2011,... (4 Replies)
Discussion started by: babyPen1985
4 Replies

2. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Want to terminate command execution when string found in the command output

Hi Experts, I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task. I am running below command to snmpwalk the router.. snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv... (19 Replies)
Discussion started by: Hanumant.madane
19 Replies

4. Shell Programming and Scripting

execution problem with grep

how to use grep word from sentence grep -o "hai" haighaihaihai Is above cmd possible in linux ? can any one help me? Thanks, (5 Replies)
Discussion started by: kavi.mogu
5 Replies

5. Shell Programming and Scripting

Execution problems with grep command in scripting

Hi All, I was looking for grep command option which can exactly matches the word in a file, for examples you may be seeing one word that is also in another word, there might be lkk0lv23 and a lkk0lv234 in which case lkk0lv23 will put BOTH hosts from the grep in. I was using this in a bash... (2 Replies)
Discussion started by: bobby320
2 Replies

6. Shell Programming and Scripting

Code for execution command by command

a) cd /root/user/dir/sd/disk ./runInstaller -silent -responseFile b) cd /root1/user1 cp /root/user/dir/sd/disk/ram.txt now a) executes and starts running but b) interupts a) and is executed while a) is running on the other hand so I want b) to start only after successfull completion of... (6 Replies)
Discussion started by: sriki32
6 Replies

7. Shell Programming and Scripting

Execution problem with grep script (2 variables)

#!\bin\sh TEST=test.log GREP=\usr\bin\grep $GREP -i 'dog\|cat' ${TEST} Why doesn't grep run at all? (10 Replies)
Discussion started by: jazzaddict
10 Replies

8. UNIX for Advanced & Expert Users

command execution alert

hi Guys! My requirement is... I need to get notified if somebody executes a specific commands...like kill or httpd stop.... something like that.... can somebody help me out... Regards, kiran (8 Replies)
Discussion started by: dddkiran
8 Replies

9. Shell Programming and Scripting

command execution time

Hi all, I want to display a progressbar depending upon the completion status of a command. I am coding from scratch as I dont want to use in place code. so Is there anyway of getting the progress of a command in percentage or in any other units while its running , is it possible using the top... (5 Replies)
Discussion started by: hashin_p
5 Replies

10. UNIX for Advanced & Expert Users

command execution ??

hi i have small shell script as follows cd /utilities/promoter/tmp grep SENDREPLY $1 | grep 'zzzzz@zzz.com' | awk -F" -f1 > /tmp/$LOGNAME/$1.request cd /tmp/$LOGNAME grep -e "\.sql" -e "\.md" $1.request > upd_$1.txt grep -v -e "\.sql" -e "\.md" $1.request > copy_$1.txt ... (1 Reply)
Discussion started by: zedex
1 Replies
Login or Register to Ask a Question