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..
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:
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 01:02 AM..
Reason: additional clarification
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)
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)
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)
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)
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)
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)
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)
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)