PDA

View Full Version : AWK in Shell Script



Elliot
28th July 2007, 09:52 AM
Hi,

I have a shell script which gets parameters passed in. One parameter i need to split into 2 variables.

The parameter is called $INSTANCE and its format is always "username/foldername"

So..

fred/mystuff
dave/mymusic
jim/myvideos

What i want to do is keep the INSTANCE variable but also Create 2 more.

$USER and $FOLDERNAME

I do not need the / inbetween them, just the 2 values splitting.

Is there a way to do this with awk, or something else that might be better?

Thanks in advance.

Elliot

gthill
28th July 2007, 11:02 AM
[gordon@tc2 Music]$ cd /home/gordon/Music/
[gordon@tc2 Music]$ pwd | cut -d/ -f2
home
[gordon@tc2 Music]$ pwd | cut -d/ -f3
gordon
[gordon@tc2 Music]$ pwd | cut -d/ -f4
Music
[gordon@tc2 Music]$ folder=`pwd | cut -d/ -f4`
[gordon@tc2 Music]$ echo "$folder"
Music

gthill
28th July 2007, 11:08 AM
[gordon@tc2 Music]$ pwd | awk -F / '{print $2}'
home
[gordon@tc2 Music]$ pwd | awk -F / '{print $3}'
gordon
[gordon@tc2 Music]$ pwd | awk -F / '{print $4}'
Music

Elliot
28th July 2007, 03:45 PM
That is great works a treat, i used the first one and did:

$USERNAME
USERNAME=`pwd | cut -d/ -f3`