UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Extract file names from file and set variable to 'highest' one

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-19-2017
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 17 July 2018, 6:46 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
Extract file names from file and set variable to 'highest' one

HI Folks -

I have a requirement where I need to scan a text file for a list of files.

The file, we'll called it, files.txt looks like such:

Code:
inbox/EBS/Client_GL_Detail_PBCS_112517_SEP2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_112617_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_112617_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_112617_SEP2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_120817_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_DEC2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_DEC2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_OCT2017.txt
 inbox/Client/Client_GL_Detail_PBCS_111417_SEP2017.txt
 inbox/Client/Client_GL_Detail_PBCS_111417_SEP2017.xls
 outbox/Client_2678.dat
 outbox/Client_2682.dat
 outbox/logs/Client_2954.log
 outbox/logs/Client_2955.log
 outbox/logs/Client_2956.log
 outbox/logs/Client_2957.log
 outbox/logs/Client_2958.log
 outbox/logs/Client_2959.log
 outbox/logs/Client_2960.log
 outbox/logs/Client_2961.log
 outbox/logs/Client_2962.log
 outbox/logs/Client_2963.log
 outbox/logs/Client_2964.log
 
 
 Total 489

I need to scan the file, searching for this string:

outbox/logs/Client_

Once found, I then need to understand if that string is suffixed with the highest numerical value on the end and then set that name (less the path) to a variable.
There will always be 4 digits and the 'highest' one is always the latest, which I need to extract.

I have done this on batch with ease but struggling here. I am using bash.

Thank you.
Sponsored Links
    #2  
Old 12-19-2017
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 17 July 2018, 1:31 PM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,486
Thanks: 145
Thanked 209 Times in 184 Posts
Why can't you use the batch script? What is that script, and what is not working with it?
Sponsored Links
    #3  
Old 12-19-2017
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 17 July 2018, 6:46 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
Well, its Windows, not *Nix.

Client moved to exa server from Windows.
    #4  
Old 12-19-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 July 2018, 8:37 AM EDT
Location: Aachen, Germany
Posts: 13,070
Thanks: 449
Thanked 4,013 Times in 3,690 Posts
Like so?
Code:
VAR=$(sort -r files.txt | sed -n '\#outbox/logs/Client_# {s#.*/##; p; q}'
echo $VAR
Client_2964.log

Sponsored Links
    #5  
Old 12-20-2017
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 17 July 2018, 6:46 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
Thank you Rudi!

I'm getting an error saying:

Code:
Unexpected EOF will looking for matching ')'

Moderator's Comments:
Extract  file names from file and set variable to 'highest' one Please use CODE tags, as required by forum rules, when displaying sample input, output, and code segments.

Last edited by Don Cragun; 12-20-2017 at 11:55 PM.. Reason: Add CODE tags.
Sponsored Links
    #6  
Old 12-20-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 July 2018, 8:37 AM EDT
Location: Aachen, Germany
Posts: 13,070
Thanks: 449
Thanked 4,013 Times in 3,690 Posts
Yes. I missed the trailing ) when copying the line. What were your (re)actions when encountering this error message?
Sponsored Links
    #7  
Old 12-20-2017
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 17 July 2018, 6:46 AM EDT
Posts: 159
Thanks: 35
Thanked 3 Times in 3 Posts
Hi Rudi -

Yes - I'm trying to understand where to place that but can't seem to make it work.
Sponsored Links
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find matching file in bash with variable file names but consisent prefixs cmccabe Shell Programming and Scripting 5 10-19-2016 09:06 AM
Extract file names from a file jricks Shell Programming and Scripting 8 08-14-2012 04:42 AM
set a variable to be a list of file names lionheartyoung Shell Programming and Scripting 4 05-02-2011 05:19 PM
command to extract sub-string out of file names lv99 UNIX for Dummies Questions & Answers 5 04-30-2011 11:40 AM
File Names in a Variable in a loop spkandy Shell Programming and Scripting 3 06-11-2009 05:55 AM



All times are GMT -4. The time now is 08:41 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?