LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   awk with Regular Expression (https://www.linuxquestions.org/questions/solaris-opensolaris-20/awk-with-regular-expression-4175466524/)

kdn242 06-18-2013 02:04 PM

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

jlliagre 06-18-2013 02:26 PM

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 ?

kdn242 06-18-2013 02:41 PM

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,

jlliagre 06-18-2013 03:33 PM

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.

whizje 06-18-2013 03:33 PM

Hasn't Solaris basename.

szboardstretcher 06-18-2013 03:45 PM

find . | awk -F/ '{print $NF}'

or

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

kdn242 06-18-2013 03:53 PM

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 ?

szboardstretcher 06-18-2013 03:56 PM

sure.

-F designates a field seperator, which here is '/'
$NF is the last column/field in a line

jlliagre 06-18-2013 04:22 PM

Quote:

Originally Posted by whizje (Post 4974394)
Hasn't Solaris basename.

All Unix and Unix like OSes have basename which is part of the core utilities (POSIX).

kdn242 06-19-2013 10:26 AM

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

szboardstretcher 06-19-2013 10:36 AM

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.

kdn242 06-19-2013 10:47 AM

Is there a way I can test the script without execute it ?

jlliagre 06-19-2013 01:36 PM

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


David the H. 06-19-2013 02:53 PM

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.


All times are GMT -5. The time now is 07:18 AM.