Help needed - UNIX command


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
We already did. The BEGIN block, running before everything else, sets the field separator as =. Then the expression tells AWK to print all lines where the first field, $1, matches the given regex.

You could, kind of, sort of use grep, depending on if your system has egrep for all the bits and pieces that made solving the problem in awk easy; but even if you have extended support, it would be more awkward since it doesn't have columns like awk does. Why not use a tool that has all the right things available by default and is much simpler to use?
This User Gave Thanks to Corona688 For This Post:
# 9  
Assuming that you have submitted you homework by now... and expanding a bit on what Corona688 already said...

The command:
Code:
env | awk -F= '$1 ~ /(NAME|DIR|PATH)$/{print $1}'

tells awk to read the output produced by env, to split each line it reads into fields separated by the = character, and then look at the first field in each line. If, and only if, that first field ends with one of the three strings you're looking for, it prints the contents of the first field on that line. It then goes on to process the next line of input from env.

You could come close to what you want with egrep using:
Code:
env | egrep -o '^[!=]*(NAME|DIR|PATH)='

which with my current environment would give you:
Code:
CDPATH=
LOGNAME=
PATH=
TMPDIR=
XPC_SERVICE_NAME=

but then you would have to post-process the output to get rid of the = that you don't want to print. You could easily do that with a second grep or egrep, but it is much more efficient to invoke awk once instead of egrep and another grep or egrep.

If you do decide to use two greps instead of awk, note that you can do it with two invocations of grep without using egrep at all (and grep is frequently slightly smaller and faster than egrep, but this can vary from operating system to operating system):
Code:
env | grep -o '^[^=]*' | grep -e 'NAME$' -e 'PATH$' -e 'DIR$'
CDPATH
LOGNAME
PATH
TMPDIR
XPC_SERVICE_NAME

In the above, the first grep throws away everything on each line starting with the first = found on each line of output from env and the second grep throws away any lines that don't end with one of the three strings you're searching for.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Command needed
ATWC
Hi, I have the date as 20130101 and i need it to rephrased to 2013-01-01. Any command which can do this.... Shell Programming and Scripting
5
Shell Programming and Scripting
Unix Script or command Needed
kvrprakash
Hi, I am new to Shell Scripting. I need to pick the files from one directory and place them in another directory based on time stamp.It should pick the files if the files is not accessed for 1 hour. Can any one please help me how to work on this. Thanks in Advance. Regards, Prakash... Shell Programming and Scripting
1
Shell Programming and Scripting
dd command help needed.
pinga123
Hi I m creating a file which will serve as a virtual harddisk . I m using following command to create a file. #dd if=/dev/zero of=/tmp/Sample.dat bs=1M count=1024 I have following doubts regarding the operation. 1) In above example i m creating a file with byte size of 1M .However in...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
Unix help needed !!
darshaan
friends i wanted to now whehter there exists a book as a solution to the questions posted in exercies of the Design of Unix Operating system by Maurice J Bach !..its really urgent !!... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
Unix help needed!
JJJ
I have a few questions about Unix. Can you please help! 1.How do I create an alias that greps the password file for my username and returns my password file entry? Once I get the alias to work I want to be able to let a friend use it .So that he can use the alias to locate his password entry...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers