Help needed - UNIX command


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Help needed - UNIX command

The 'env' command prints out all of the environment variables and their values.
Come up with a command that prints a list of environment variables whose names
end with either 'NAME', 'DIR' or 'PATH'.

What I've tried.
Code:
$ env | grep '[NAME, DIR, PATH]'

Although it highlights the part of the variables it does not list them as it should. It actually prints the whole lines including the variables. Not sure what I'm missing.

University Of Technology Sydney, Unix Systems Programming
# 2  
Hello edujs7,

Could you please try following.

Code:
env | awk 'BEGIN{FS="="} $1 ~ /.*(NAME$|DIR$|PATH$)/'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Quote:
Originally Posted by Scrutinizer
Try:
Code:
env | grep -E '^(NAME|DIR|PATH)='

This gives me the below; seems to be only picking up "PATH=" and nothing else.

Code:
$ env | egrep '^(NAME|DIR|PATH)='

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

--- Post updated at 04:06 AM ---

Thank you RavinderSingh13

I get the below output - however, I believe I only need the Variables listed and nothing else.

Code:
env | awk 'BEGIN{FS="="} $1 ~ /.*(NAME$|DIR$|PATH$)/'

USERNAME=eduardo
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
TEXTDOMAINDIR=/usr/share/locale/
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
WINDOWPATH=2
LOGNAME=eduardo
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

# 5  
Quote:
Originally Posted by edujs7
Thank you RavinderSingh13
I get the below output - however, I believe I only need the Variables listed and nothing else.
Code:
env | awk 'BEGIN{FS="="} $1 ~ /.*(NAME$|DIR$|PATH$)/'

Hello edujs7,

In case you want to have only variable names then try following.
Code:
env | awk 'BEGIN{FS="="} $1 ~ /.*(NAME$|DIR$|PATH$)/{print $1}'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
In a regular expression (RE) the ^ means the beginning of the line; together with the = it enforces an exact match. Omit the ^ and it must only end with the given pattern.

$1 ~ /.*(NAME$|DIR$|PATH$)/ is correct, because the $1 field is = delimited, and in a RE the $ enforces the end of the field. The .* means any characters but is not needed here because any leading characters are always permitted in a RE (unless there is a ^ anchor). So it is optimized $1 ~ /NAME$|DIR$|PATH$/ or with a common $ anchor $1 ~ /(NAME|DIR|PATH)$/.

Last edited by MadeInGermany; 4 Weeks Ago at 02:03 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Thanks so much - that worked Smilie

Sorry to be a pain but would you please be able to explain what the actual command does Smilie - and wouldn't egrep work for this?
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