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 |
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 ? |
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, |
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.
|
Hasn't Solaris basename.
|
find . | awk -F/ '{print $NF}'
or cat filename_with_file_list | awk -F/ '{print $NF}' |
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 ? |
sure.
-F designates a field seperator, which here is '/' $NF is the last column/field in a line |
Quote:
|
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 |
Code:
FILENAME=/export/home/cool/test_files.txt |
Is there a way I can test the script without execute it ?
|
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 |
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. 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. |