Solaris / OpenSolarisThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.