LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 06-18-2013, 02:04 PM   #1
kdn242
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Rep: Reputation: Disabled
awk with Regular Expression


Hi All,

I have a question related to awk command and regular expression. I got a text file with contain the directory path, and I want to get the file name out of the directory path and put it into a variable for later use.

Ex: /test/files/location/filename.txt

I want to get the filename.txt out of the directory path and put int into variable.

Thanks for your time!!!

kdn242
 
Old 06-18-2013, 02:26 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Welcome to LQ !

Is your question related to Solaris ?

What precisely contains your input file, just paths ?

What have you tried so far ?

Why do you want to use awk and not something else that might better suit the job ?
 
Old 06-18-2013, 02:41 PM   #3
kdn242
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
yes, the environment is Unix Solaris.

I use a text file which contains directory path and try to reconstruct the copy command using the file directory that listed in the text file.

I think that awk could find me the file name by regular expression and I can put it into a variable for use.

this is the regular expression ('[^//]*$') that can match the file name, but I have hard time to get it works in awk format

Any advice,
 
Old 06-18-2013, 03:33 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Your regular expression looks correct although there is an extra "/" but unless I'm missing what exactly you are trying to achieve, I would use basename instead of awk in your case.
 
Old 06-18-2013, 03:33 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Hasn't Solaris basename.
 
Old 06-18-2013, 03:45 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
find . | awk -F/ '{print $NF}'

or

cat filename_with_file_list | awk -F/ '{print $NF}'
 
1 members found this post helpful.
Old 06-18-2013, 03:53 PM   #7
kdn242
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
find . | awk -F/ '{print $NF}'

or

cat filename_with_file_list | awk -F/ '{print $NF}'

This works perfectly, can you give me the explanation on your awk command please ?
 
Old 06-18-2013, 03:56 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
sure.

-F designates a field seperator, which here is '/'
$NF is the last column/field in a line
 
1 members found this post helpful.
Old 06-18-2013, 04:22 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by whizje View Post
Hasn't Solaris basename.
All Unix and Unix like OSes have basename which is part of the core utilities (POSIX).
 
Old 06-19-2013, 10:26 AM   #10
kdn242
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
This is my codes:

#!/bin/bash

FILENAME=/export/home/cool/test_files.txt
count=0
cat $FILENAME | while read LINE
do

filename=$LINE | awk -F/ '{print $NF}'
cp -p $filename $LINE
done

I try to echo out the file name that is listed in the test_files.txt ( directory path ) to reconstruct the copy command to distribute the files into their directory, but it seems to not working.

Please give me some advice on this case!!!

I'm really appreciated your time.

kdn242
 
Old 06-19-2013, 10:36 AM   #11
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Code:
FILENAME=/export/home/cool/test_files.txt
count=0
cat $FILENAME | while read LINE
do
 filename=$($LINE | awk -F/ '{print $NF}')
 cp -p $filename $LINE
done
Here. this is untested.
 
Old 06-19-2013, 10:47 AM   #12
kdn242
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Is there a way I can test the script without execute it ?
 
Old 06-19-2013, 01:36 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Not really but you can print the copy command instead of doing it (I also fixed a bug and removed a couple of useless instructions).

Code:
FILENAME=/export/home/cool/test_files.txt
while read LINE
do
 filename=$(printf "%s" "$LINE" | awk -F/ '{print $NF}')
 echo cp -p "$filename" "$LINE"
done < $FILENAME

Last edited by jlliagre; 06-19-2013 at 02:09 PM.
 
Old 06-19-2013, 02:53 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you're using bash, ksh or a similar bourne-based shell you can run your script with the -n flag to test the syntax, without actually executing anything. ( e.g. use either a shebang like "#!/bin/bash -n", or the command "set -n" before the lines you want to test. )

In any case though, the use of awk should be academic here. Once you have a text string stored inside a variable, you can nearly always use parameter substitution or some other built-in string manipulation on it, more efficiently.

Code:
#print the basename and path of the file.
FILENAME=/export/home/cool/test_files.txt
echo "${FILENAME##*/}"
echo "${FILENAME%/*}"

#print any arbitrary path position
IFS=/ read -ra FILENAME <<<'/export/home/cool/test_files.txt'
$ echo "${FILENAME[2]}"
The basic substitutions used here are good in all posix-compliant shells. The third one splits the string with an array, so it needs a shell with array support. The above is for bash here, but it can be done in ksh too with slight modification. Just change -a to -A in read.


Finally, as has been mentioned before, your system also has basename and dirname applications that will also give you what you want.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK: Using a regular expression as a field separator Blackened Justice Programming 8 06-01-2012 07:07 AM
How can awk search a string without using regular expression? 915086731 Programming 8 09-07-2011 10:07 PM
Awk - substitution with parts of the matched regular expression used. c_moriarty Programming 4 04-30-2011 08:38 AM
[SOLVED] AWK: compare apache dates without using regular expression smallmeans Programming 13 05-17-2010 02:36 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration