Grep problem from The Unix Programming Environment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep problem from The Unix Programming Environment
# 1  
Old 06-08-2012
Grep problem from The Unix Programming Environment

Hi

Here is the problem ( Exercise 3-3, Using The Shell of The Unix Programming Environment, Kerninghan, Pike, 3rd edition ):

Predict what each of the following grep commands will do, and then verify your understanding.

Code:
grep \$
grep \\$
grep \\\$
grep '\$'
grep '\'$'
grep \\
grep \\\\
grep "\$"
grep '"$'
grep "$"

A file containing these commands themselves makes a good test case if you want to experiment.

Needless to say, my understanding was wrong.

The first command for example, prints all lines, which I don't understand - so I suppose I must be missing on basics.

Any hints will be highly appreciated.

Last edited by methyl; 06-08-2012 at 04:59 PM.. Reason: please use code tags
# 2  
Old 06-08-2012
Please post sample data (very important that the data includes lines containing one or more dollar character and some lines with various numbers of backslash characters) , the commands you typed, the results and any comments about what happened. I really like the idea of using the example grep lines as sample data because they seem to contain every variant.
While doing this task you will probably understand the basic concepts.

Please mention what Operating System and version you are running and what Shell you use. I can't imagine that it will be the original Bourne Shell (but it might be).

In unix fundamental commands, the dollar sign can mean the end of the line. When not escaped it introduces an Environment Variable. When double-quoted and escaped "\$" it becomes just a dollar character. When single-quoted it is just a dollar character '$' because single quotes disable parameter substitution. This example appears to be about teaching the fundamentals. If you use grep to look for end-of-line characters in a normal unix text file the output will be every line.


Quote:
The first command for example, prints all lines, which I don't understand - so I suppose I must be missing on basics
In the first example the grep is looking for end-of-line characters in a text file (i.e. line-feed characters) which are abbreviated to a dollar sign in much unix syntax. The dollar sign has been escaped with a backslash to stop it being interpreted by Shell as an introduction to an Environment Variable.

Last edited by methyl; 06-08-2012 at 05:18 PM.. Reason: multiple revisions and corrections
This User Gave Thanks to methyl For This Post:
# 3  
Old 06-10-2012

I suggest that you examine the problem one step at a time.

The first question is, what will the shell do to the arguments?

To see that, replace grep with echo:
Code:
echo \$
echo \\$
echo \\\$
echo '\$'
echo '\'$'
echo \\
echo \\\\
echo "\$"
echo '"$'
echo "$"

Whatever is printed is what will be passed to grep.

Try them separately as one line contains an error that will affect subsequent lines if you run them all as a single script.
This User Gave Thanks to cfajohnson For This Post:
# 4  
Old 10-28-2012
Thanks!

Thank you very much guys for your replies. I understand it better now!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using grep to find C programming definition

Let's say I have a file written in C programming ... int function A (Param1, Param2) { .... } How to grep this function? I tried grep -ER '^functionA(.*)/n{' filename > result.txt Not work :mad: Please use CODE tags on all sample input, output, and code segments; not just on shell... (4 Replies)
Discussion started by: cmdcmd
4 Replies

2. Programming

UNIX network programming execution problem

I have started reading the book Unix Network Programming, Volume 1: The Sockets Networking API. I downloaded all the source code and performed all the steps present in README file.Now when i compile my first program it give the following error. sainandan@nandan:~/unpv13e/intro$ ./daytimetcpcli... (1 Reply)
Discussion started by: bsainandan
1 Replies

3. Programming

UNIX Network Programming making files problem

I have started reading the book Unix Network Programming, Volume 1: The Sockets Networking API. I downloaded all the source code and the readme says I must make the files. zuro@zuro:~/book/unpv12e$ cd lib zuro@zuro:~/book/unpv12e/lib$ make gcc -g -O2 -D_REENTRANT -Wall -c -o... (5 Replies)
Discussion started by: xuro
5 Replies

4. UNIX for Dummies Questions & Answers

How does unix system administration, unix programming, unix network programming differ?

How does unix system administration, unix programming, unix network programming differ? Please help. (0 Replies)
Discussion started by: thulasidharan2k
0 Replies

5. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies

6. Programming

New to C Programming in UNIX environment

Hi all, I know the basics of C programming, but I dont know a single thing about how to write, compile and run C code in UNIX, also where liberaris are stored, how .headers files are being managed in unix, how to debug c code and what are the available GNU tools for c programming in linux env,... (6 Replies)
Discussion started by: patras
6 Replies

7. Shell Programming and Scripting

Unix Systems Programming Vs Unix Programming

Several months ago I found a link that explained the difference between how a Unix Systems Admin would do scripting compared to what a Unix Programmer would do. It showed a basic script and then show several iterations that explained how the Systems Admin would change it to make it better. I was... (0 Replies)
Discussion started by: BCarlson
0 Replies

8. Shell Programming and Scripting

programming for unix environment

i have to make a menu based program to allow iusers to select the system information about their machine that they want. All i have been taught is how to enter commands at the command prompt like simple commands such as pwd and cd directory to change directory (not even sure if that is right). But... (1 Reply)
Discussion started by: carlvernon
1 Replies

9. Post Here to Contact Site Administrators and Moderators

Java Programming in UNIX Environment

A suggetion: Don't we need to start Java Programming in UNIX Environment Forum ? (1 Reply)
Discussion started by: cub
1 Replies
Login or Register to Ask a Question