Question in the command of Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question in the command of Grep
# 1  
Old 07-24-2011
Bug Question in the command of Grep

I have the following command using Grep in a complex shell script. Can you please advise what does the below expression mean?
# grep ^${File1}\| $Textfile > /deve/null 2>&1

Questions,

1) Wanted to know the significance of "^" in this line
2)What does a backslash"\" before the pipeline does
3)In the last part I guess they are trying to write the output of the search in the path /deve/null and i dont understand the meaning of 2>&1

Apart from explaining the above questions, please give me an overall idea from your knowledge that what this line tries to do in the script. Thanks in advance for your help..Smilie
# 2  
Old 07-25-2011
Quote:
Originally Posted by sreedharust
I have the following command using Grep in a complex shell script. Can you please advise what does the below expression mean?
# grep ^${File1}\| $Textfile > /deve/null 2>&1

Questions,

1) Wanted to know the significance of "^" in this line
The carrot (^) signifies the start of a record. Thus the pattern would be matched only if it's at the beginning of the record.

Quote:
2)What does a backslash"\" before the pipeline does
The backslash escapes the character and prevents the shell from interpreting it as a pipe symbol. In this instance it is passed as the end of the pattern to grep rather than causing the shell to treat the next token as a command.

Quote:
3)In the last part I guess they are trying to write the output of the search in the path /deve/null and i dont understand the meaning of 2>&1
Yes, they are trying to redirect output. The 2>&1 redirects standard error to the same file as standard output. I'd also say that the file they want to redirect to is wrong. The file /dev/null (no in deve) is a special file that drops the output on the floor so to speak.

Quote:
Apart from explaining the above questions, please give me an overall idea from your knowledge that what this line tries to do in the script.
In this case my guess is that the programmer was interested in having the result of the grep (0/success) if the pattern was found in the file and thus the output could be ignored and the exit code ($?) tested after the command. If your version of grep supports it, use the -q option rather than redirecting the output. This option (quiet) causes grep to not write any output to stdout and will stop on the first match -- much more efficient when a huge file is involved. Something like this:

Code:
   if grep -q  "^$file1|" $textfile
   then
     echo "$textfile references $file1"
   else
     echo "$textfile doesn't reference $file1"
   fi

You'll also notice that I put the pattern in quotes and that eliminates the need to escape the pipe symbol -- makes the pattern much easier to read.

Hope this has helped.

Last edited by agama; 07-25-2011 at 12:02 AM.. Reason: additional clarification
This User Gave Thanks to agama For This Post:
# 3  
Old 07-25-2011
Bug Thank you very much..

Wow!! thanks for the detailed response and I got the concept now..... that too in a short time.....

Yes you are right the path name is /dev/null .. Its my typo...

Also the advise of using -q option in the grep was really useful ...

Thank you very muchSmilie .......

Regards

Sreedhar S
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question on grep command (extract a string)

Dear all, I have a file a.txt like below: 1_234560_A_G_b37 1 2 1 2 2 2 ... 1_35465767_C_T_b37 2 1 1 2 2 2 ... 2_490638010_A_T_b37 1 2 1 2 2 2 ... 10_4567899_T_G_b37 2 2 1 2 2 2 ... ... what I want to do is extracting rows starting with "10_" like : 10_4567899_T_G_b37 2 2 1 2 2... (1 Reply)
Discussion started by: forevertl
1 Replies

2. Shell Programming and Scripting

Question about grep

can anyone tell me what the \/$ means? from grep \/$ (8 Replies)
Discussion started by: Nick1097
8 Replies

3. Shell Programming and Scripting

grep question please

i have files with "DOMAINSOLVER ACMS" with any number of spaces in between the two words on its own line and i can find it with the following: grep -c "DOMAINSOLVER* ACMS" $FILENAMEbut i need to exclude any lines matching: "$DOMAINSOLVER". i've tried a variety of quoting and escaping with no luck.... (4 Replies)
Discussion started by: crimso
4 Replies

4. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies

5. UNIX for Dummies Questions & Answers

Another grep question

Hi guys, I'm very new to unix but am liking it a lot so far, so please be gentle on this newb. I tried using the search for this question, but no luck. Anyways, i've got a few data fields... for example John Adress1 Adress2 Sam Adress3 Adress4 Jason Adress5 Adress6 Is... (4 Replies)
Discussion started by: Uss_Defiant
4 Replies

6. Shell Programming and Scripting

grep question

hello people, All my servers have 4 mounts with this norme. For example, if my hostname is siroe. df -h | grep `hostname` /dev/dsk/c1t3d0s6 404G 399G 800M 100% /siroe3 /dev/dsk/c1t2d0s6 404G 399G 800M 100% /siroe2 /dev/md/dsk/d6 20G 812M 19G ... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

7. UNIX for Dummies Questions & Answers

Grep question.

Hi, I am executing the below command. grep ".UPDATE" file1.txt | grep -v MQQUEUE > Myprog1 The expected output is all lines in file1.txt which have the string ".UPDATE" and dont contain the string MQQUEUE. However, the output which I am getting is just searching for the string... (3 Replies)
Discussion started by: saurabhsinha23
3 Replies

8. UNIX for Advanced & Expert Users

how to exclude the GREP command from GREP

I am doing "ps -f" to see my process. but I get lines that one of it represents the ps command itself. I want to grep it out using -v flag, but than I get another process that belongs to the GREP itself : I would like to exclude # ps -f UID PID PPID C STIME TTY TIME CMD... (2 Replies)
Discussion started by: yamsin789
2 Replies

9. Shell Programming and Scripting

grep question

Hi, I am currently using grep -c to scan lines for certain data. I am just wondering if you can search a specific column of a file using grep -c. Thanks (6 Replies)
Discussion started by: Jaken
6 Replies

10. Shell Programming and Scripting

small question of echo | grep command

Hi, i've got the following: a=`echo $b | grep '^.*/'` i'm storing in the variable the value of the variable b only if it has a / somewhere. It works, but i don't want to print the value. How do i give the value of b to the grep command without the echo? thanks! (5 Replies)
Discussion started by: kfad
5 Replies
Login or Register to Ask a Question