cleanup $PATH with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cleanup $PATH with awk
# 1  
Old 06-17-2009
cleanup $PATH with awk

Hello there,

I want to remove duplicate directories of $PATH. I already have the code (found in this forum), which removes duplicate lines in a textfile.
Code:
awk 'x[$0]++==0' filename

By how do I use this for the $PATH-variable?
# 2  
Old 06-17-2009
Code:
echo $PATH | awk -F":" '{for (i=1;i<=NF;i++)print $i}'

PIPE this to your command.
# 3  
Old 06-17-2009
Use the below code.

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

or 
PATH=$(echo "$PATH" | /usr/xpg4/bin/awk -v RS=':' -v ORS=":" '!a[$1]++')

these both syntax will work choose one of them

BR

# 4  
Old 06-17-2009
Very much thanks for the fast replies to both of you.

I've have made this from the sample of rakeshawasthi
Code:
echo $PATH | awk -F":" '{for (i=1;i<=NF;i++){if (x[$i]++==0) print $i}}'

But I think the example of you, ahmad.diab, I still a little more elegant, and make nearly exactly, what I want.

-----Post Update-----

I just noticed, that I get a colon at the end of $PATH for everytime I run the command.

-----Post Update-----

I noticed, that when ich use "echo -n" there is only one colon at the end undepending of the number of runs of the command. But I still can't export $PATH, because of the trailing colon.

Last edited by doc_symbiosis; 06-17-2009 at 09:27 AM..
# 5  
Old 06-17-2009
Quote:
Originally Posted by doc_symbiosis
Very much thanks for the fast replies to both of you.

I've have made this from the sample of rakeshawasthi
Code:
echo $PATH | awk -F":" '{for (i=1;i<=NF;i++){if (x[$i]++==0) print $i}}'

But I think the example of you, ahmad.diab, I still a little more elegant, and make nearly exactly, what I want.

-----Post Update-----

I just noticed, that I get a colon at the end of $PATH for everytime I run the command.

-----Post Update-----

I noticed, that when ich use "echo -n" there is only one colon at the end undepending of the number of runs of the command. But I still can't export $PATH, because of the trailing colon.
you can put the last ":" in a new line if you change $1 to $0 in the code:

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

# 6  
Old 06-17-2009
Sorry, with echo -n didn't work on solaris. But there I used
Code:
printf "$PATH"

instead and the problem still remains...Smilie
# 7  
Old 06-17-2009
Quote:
Originally Posted by doc_symbiosis
Sorry, with echo -n didn't work on solaris. But there I used
Code:
printf "$PATH"

instead and the problem still remains...Smilie
Code:
echo -n in solaries is exist the below path:
don't worry :)

/usr/ucb/echo -n
BR

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