Help needed - UNIX command


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help needed - UNIX command
# 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 for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #213
Difficulty: Medium
OSPF-TE is an extension to OSPF extending the expressivity to allow for traffic engineering and use on non-IP networks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command needed

Hi, I have the date as 20130101 and i need it to rephrased to 2013-01-01. Any command which can do this. (5 Replies)
Discussion started by: ATWC
5 Replies

2. Homework & Coursework Questions

Unix Homework Help needed

1 Petras Pavardenis 1980 5 08 Linas Bajoriunas 1970 10 3 Saulius Matikaitis 1982 2 5 Mindaugas Stulgis 1990 7 6 Rimas Nasickis 1964 10 7 ... (1 Reply)
Discussion started by: vaidastf
1 Replies

3. Shell Programming and Scripting

Unix Script or command Needed

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 (1 Reply)
Discussion started by: kvrprakash
1 Replies

4. UNIX for Dummies Questions & Answers

dd command help needed.

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... (1 Reply)
Discussion started by: pinga123
1 Replies

5. Shell Programming and Scripting

Help needed in unix commands

i define a function outside the nawk command which trims the leading and trailing spaces like function TrimSpace(x) { y=gsub(/^+|+$/,"",x) { return x } } and i want to call this function in nawk commad like. nawk '{print TrimSpace($1)}' file Note:... (2 Replies)
Discussion started by: malikshahid85
2 Replies

6. Shell Programming and Scripting

Help Needed in Unix Script

Hi, I have multiple servers and each server hosts multiple databases. Now i have requirement as below 1) Create a file in any server which will hold all the database name. 2) Write a shell script that will read the file and connect to each and every database. and log out of it one by... (2 Replies)
Discussion started by: amritansur
2 Replies

7. Shell Programming and Scripting

help needed in ls command

Hi all, i need to write an ls command which will pick up ind_01.txt/pak_01.txt from ind_01.txt pak_01.txt usa_01.txt files in a directory.. i wrote a ls command by storing ind and pak in 2 variables and listing it. Its working fine. But the pattern to be matched should be sent in... (1 Reply)
Discussion started by: anju
1 Replies

8. UNIX for Dummies Questions & Answers

Help needed in Basic UNIX

hi friends, How to obtain list of groups we r a member of and redirect it to a file. how to append the details of current OS to a file. how to append the estimated file space to a file. how to append the details of users loged on along wth their current activity into a file. Thank you...I'm... (3 Replies)
Discussion started by: bobby36
3 Replies

9. UNIX for Dummies Questions & Answers

Unix help needed !!

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 !! (1 Reply)
Discussion started by: darshaan
1 Replies

10. UNIX for Dummies Questions & Answers

Unix help needed!

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... (1 Reply)
Discussion started by: JJJ
1 Replies

Featured Tech Videos