Bash: Pattern matchin - how to do matching on a variable?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
my $outPut = `ls -ltr foo`;
# The output of ls -ltr has 7 columns of data, with the filename
# in the last column
$outPut =~ /^*\ (\S+)$/;
my $fileName = $1;
The above code will parse the filename from the output of an ls -ltr command. How would I do that in BASH?
is getting file names so difficult? use shell expansion, and DON'T use ls -l + awk to get file names unless you are pretty sure files don't have spaces.
Code:
for myfile in *
do
echo "$myfile"
done
Last edited by ghostdog74; 12-07-2009 at 10:58 PM.
Well, the issue of having an \n embedded in a filename is not really a legitimate concern. Anyone who were to do this would surely be fired, and their files would be renamed by their replacement. But the unix/linux timestamp portability point is probably a legitimate cause for concern.
not when trying to get other fields besides file names.
Even then, for maximum robustness; knowing the (possibly pathological!) filename, the "other fields" data can be found using the stat command.
Possibly over-defensive in many real-life situations and surely slow but appealing to those of us given aversion therapy in the school of "if it can go wrong, it will go wrong".
well, ls uses the same "stat" libraries to retrieve those information as well. Also, stat may not be in some Unix platforms, but ls is.
There are no right answers, only choices with various pros and cons. When deciding which choices to make, we balance the pros and cons, considering: maintainability, portability (thanks for that), skill level, robustness and time (both development and execution) ... .
The "sweet spot" will vary depending on usage -- a quick-and-dirty "one off", a cross-platform installation utility, a backup script, system-wide vs. private usage etc.
Are there many *n*x platforms that don't have stat -- apart from very cut-down versions such as BusyBox (which may have stat -- I don't know)?
(I don't know awk or sed so bear with me - I might make incorrect comments)
If use the code above, it seems to work, as long as the filename does not have spaces in the filename .
Quote:
But then what's the point of the -l flag?
Without the -l flag, only the filenames are displayed. At least with the -ltr option, I know that the filename is anything that follows the time/date of the file.
Quote:
Or, sometimes I do something like this with grep:
Code:
if [ `echo $foo | grep -Ec "regex"` -eq 1 ]; then
# do stuff
else
# do other stuff
fi
I need to extract the filename, not do a comparison. I can do this easily in Perl. Not so easy in Bash.
Without the -l flag, only the filenames are displayed. At least with the -ltr option, I know that the filename is anything that follows the time/date of the file.
This doesn't make sense to me at all.
Quote:
I need to extract the filename, not do a comparison. I can do this easily in Perl. Not so easy in Bash.
It isn't a comparison really -- that's a control structure you can use to perform various actions if a file name matches a regular expression. Anyway, use grep.
always produces one 'record' per file per line of output, and anything(!) after the date/timestamp in each rec is the filename, with or without spaces in it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.