LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-08-2009, 07:30 PM   #16
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207

Quote:
Originally Posted by chrism01 View Post
SQADude is saying that

ls -l

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.
So the point is just that multiple filenames don't show up in rows? Well, I suppose as someone else noted above, ls -1 (not -l) would avoid that.

Last edited by jhwilliams; 12-08-2009 at 07:32 PM.
 
Old 12-08-2009, 07:46 PM   #17
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
I agree. The OP should clarify whether he needs other info as well.
 
Old 12-08-2009, 08:02 PM   #18
SQADude
LQ Newbie
 
Registered: Nov 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jhwilliams View Post
So the point is just that multiple filenames don't show up in rows? Well, I suppose as someone else noted above, ls -1 (not -l) would avoid that.
Using -1tr (that's a '1', not "l") would fix my problem. All I would have to do is to take the last row and the last row would be the filename.

However, for academic sake (I'm trying to learn sed/regex), how would I do this?

Do an "ls -ltr" (not a 1 but an l) on a file and then extract its filename?

For example, let's just say that there is only one file called "foo space" in this directory. If I do an "ls -ltr foo*", I get this output:

-rw-rw-rw- 1 root root 1001 Dec 8 17:58 foo space

I want to get "foo space" in a variable called fileName.

In perl, I could do this:
Code:
my $outPut = `ls -ltr foo*`;

$outPut =~ /:..\ (.+)$/;
my $fileName = $1;
print '$fileName is: ', "$fileName \n";
BTW, the Perl code above works. I tried it.

How would I do that in BASH?
 
Old 12-08-2009, 08:23 PM   #19
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
In the case you have given, this would be sufficient:
Code:
filename=foo*
If there is no filename matching the glob foo*, then $filename will contain the literal string 'foo*'. So you might want to check to see that the glob has actually matched a file:

Code:
[ -f "$filename" ] || echo "$filename" is not a file.
If you are expecting multiple matches on a glob (as is probably more reasonable) you might combine the above approach with the Bash array capability.

Code:
# Get an array, with one filename per entry.
filenames=(foo*)

# Either you have 0 filenames, or 1..n filenames.
# Check the first entry of the array to see if it is a filename.
# If not, error out.
[ -f "${filenames[0]}" ] || echo "no matches" && exit 1

# Do whatever it is you have to do -- perhaps with a for loop.
for item in "${filenames[@]}"; do 
    echo "$item";
done

Last edited by jhwilliams; 12-08-2009 at 08:44 PM.
 
Old 12-08-2009, 09:44 PM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Arrow

Quote:
Originally Posted by SQADude View Post
In perl, I could do this:
Code:
my $outPut = `ls -ltr foo*`;

$outPut =~ /:..\ (.+)$/;
my $fileName = $1;
print '$fileName is: ', "$fileName \n";
BTW, the Perl code above works. I tried it.

How would I do that in BASH?
what is wrong with using this ?:

Code:
for file in foo*
do
 echo "do everything in this loop" 
 # if you want to check if its a file
 if [ -f "$file" ] ;then
    echo "$file"
 fi
done
quote your variables when you are using it, so that spaces are taken care of.
 
Old 12-09-2009, 01:03 AM   #21
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ghostdog74 View Post
what is wrong with using this ?:
If there are no files matching foo* then the shell will expand foo* to exactly that, foo*, unless shopt nullglob is set. It is unset (displayed as "off" by the shopt command) by default on Slackware 13.0.
 
Old 12-09-2009, 01:11 AM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by jhwilliams View Post
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.
Wow! [irony]Nice organsation.[/irony] Typically files with "pathological" characters in their names are not created by people but by software bugs or file system corruption. There are quite a few threads on LQ posted by people needing to delete or rename such files -- and they usually have no idea how those file names were created.
 
Old 12-09-2009, 01:36 AM   #23
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by catkin View Post
If there are no files matching foo* then the shell will expand foo* to exactly that, foo*, unless shopt nullglob is set. It is unset (displayed as "off" by the shopt command) by default on Slackware 13.0.
yes that's right. nullglob should be set if that's the case (or specifically use -f to test for file existence). However that's not the point, since OP is using ls with wildcard expansion produces error as well.

Code:
for file in foo*
do
 if [ -f "$file" ];then
   echo "found..continue processing here"
 fi
done
 
Old 12-09-2009, 01:47 AM   #24
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ghostdog74 View Post
yes that's right. nullglob should be set if that's the case (or specifically use -f to test for file existence).
Sorry -- I missed that; the -f test handles it .
 
  


Reply

Tags
bash, matching, pattern, regex


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Pattern matching in a bash case statement using regex ciphyre Programming 1 01-31-2009 12:20 PM
Pattern matching in BASH. ccin1492 Programming 8 12-19-2008 11:00 AM
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM
pattern matchin with perl gene_gEnie Programming 6 09-29-2001 11:40 PM


All times are GMT -5. The time now is 11:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration