cleanup $PATH with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cleanup $PATH with awk
# 8  
Old 06-17-2009
Quote:
you can put the last ":" in a new line if you change $1 to $0 in the code:
Didn't change anything, when I use
Code:
PATH=` echo "$PATH" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$0]++'`

Also, with printf no change.
# 9  
Old 06-17-2009
Quote:
Originally Posted by doc_symbiosis
Didn't change anything, when I use
Code:
PATH=` echo "$PATH" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$0]++'`

Also, with printf no change.
Code:
what about /usr/ucb/echo -n did it work for you?

# 10  
Old 06-17-2009
ah ok, the '''-n''' for echo didn't work on solaris.

Code:
PATH=`printf "$PATH" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$0]++'`

So I have two lines as output. How can I now only use the first one for $PATH?
# 11  
Old 06-17-2009
Quote:
Originally Posted by doc_symbiosis
ah ok, the '''-n''' for echo didn't work on solaris.

Code:
PATH=`printf "$PATH" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$0]++'`

So I have two lines as output. How can I now only use the first one for $PATH?
Just add the below (in bold):-

Code:
PATH=`printf "$PATH" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$0]++'` | nawk 'NR==1'

BR
# 12  
Old 06-17-2009
You can't change the PATH variable this way. The code is running in a new shell (child), once you return to the current shell the value is lost. I should unravel where the directory is set twice.
In the meantime, if you want to change the PATH variable in the current shell you're working on, you can make a script like:

Code:
#!/bin/ksh

export PATH=`echo "$PATH" | awk -F":" '{for (i=1;i<=NF;i++)if(!x[$i]++)print $i}'`

and run the script as follow:

Code:
. /path/of/script/scriptname

Bear in mind that the PATH variable is unchanged if you close the current shell.
# 13  
Old 06-17-2009
Quote:
Originally Posted by Franklin52
You can't change the PATH variable this way. The code is running in a new shell (child), once you return to the current shell the value is lost. I should unravel where the directory is set twice.
In the meantime, if you want to change the PATH variable in the current shell you're working on, you can make a script like:

Code:
#!/bin/ksh

export PATH=`echo "$PATH" | awk -F":" '{for (i=1;i<=NF;i++)if(!x[$i]++)print $i}'`

and run the script as follow:

Code:
. /path/of/script/scriptname

Bear in mind that the PATH variable is unchanged if you close the current shell.
That's true but if you are a root user then you can change it ...
# 14  
Old 06-17-2009
Quote:
Originally Posted by ahmad.diab
That's true but if you are a root user then you can change it ...
Nope, if you open a shell, change a variable and close the shell, the new value of the variable is lost, even if you login as root!

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse Directory path - awk

Hi All, Need some help in parsing a directory listing .. output into 2 files Input file level1,/level2/level3/level4/ora001,10,IBB23 level1,/level2/level3/level4/ora001/blu1,,IBB23 level1,/level2/level3/level4/ora001/clu1,,IBB23 level1,/level2/level3/level4/ora002,,IBB24... (10 Replies)
Discussion started by: greycells
10 Replies

2. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Return path of specific tag using awk

The below awk is used with the attached index.html and matches the specific user id in the sub portion with path of /rundb/api/v1/plugin/49/. The command does run but the output is blank. Something changed in the file structure as it used to work. So using the first line in the output: ... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Variable of Path directory is not parsing in awk

Hi All, i had to split one files into 10 equally. For that i have coded below awk. OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst DIR_NM=`dirname ${OUTPUT_FILE}` awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE} ... (7 Replies)
Discussion started by: looney
7 Replies

5. AIX

Simple AWK cleanup/questions AIX

I have an unfortunate need to redo a bunch of disk settings on a VIOS on AIX, so I was putting together a quick script to scrub everything it has, make the changes, and then put the mappings back. It works, I just am trying to get my awk a bit more up-to-snuff and wanted to know the proper way to... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

sed/awk for extracting directory from file path

Hi, I have following path: set file_path = D:/forums/prac/somedir/new1/file1.txt or set file_path = E:/new/forums1/prac/somedir/new2/file2.txt I need to grep "somedir" from file path. In this case preceding directory "prac" remains same for both the paths, but directories preceding... (7 Replies)
Discussion started by: sarbjit
7 Replies

7. Shell Programming and Scripting

Copy respective path next to last column with awk

Hi to all, I have the short print out sample of the DOS command "dir S/" as showed below. Directory of C:\Program Files\Winamp\Skins\Bento\window 02/12/2010 11:35 p.m. <DIR> . 02/12/2010 11:35 p.m. <DIR> .. 11/12/2009 10:31 a.m. 13,556... (3 Replies)
Discussion started by: cgkmal
3 Replies

8. Shell Programming and Scripting

Trouble with a file path as awk output in for loop

When I run the following command in the shell it works fine. It prints a city name and then a path for a file. ~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"` >do >echo $i >done Now, when I place it in this shell script (sh) it prints... (6 Replies)
Discussion started by: afroCluster
6 Replies

9. Shell Programming and Scripting

awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted. This happened due to manual manipulation of /etc/passwd files. I need to do this for 40 servers. Can anyone help me in achieving this? Even reducing a step or two will be... (6 Replies)
Discussion started by: pdtak
6 Replies

10. Shell Programming and Scripting

Help with cleanup

I am trying to add a unique string to a variable to prevent some name space collisions. DATAFILE=/u001/app/unica/affinium644/campaign/partitions/limited/tmp/ebf9aaah.t~# DATETIME=`date +%Y%m%d_%H%M%S` echo $DATAFILE > tmpnme.txt sed 's_/_ _g' tmpnme.txt > tmpnme2.txt DATA=$(cat tmpnme2.txt)... (2 Replies)
Discussion started by: whdr02
2 Replies
Login or Register to Ask a Question