Home Man
Search
Today's Posts
Register

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
# 1  
Old 12-19-2017
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.
# 2  
Old 12-19-2017
Why can't you use the batch script? What is that script, and what is not working with it?
# 3  
Old 12-19-2017
Well, its Windows, not *Nix.

Client moved to exa server from Windows.
# 4  
Old 12-19-2017
Like so?
Code:
VAR=$(sort -r files.txt | sed -n '\#outbox/logs/Client_# {s#.*/##; p; q}'
echo $VAR
Client_2964.log

# 5  
Old 12-20-2017
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.
# 6  
Old 12-20-2017
Yes. I missed the trailing ) when copying the line. What were your (re)actions when encountering this error message?
# 7  
Old 12-20-2017
Hi Rudi -

Yes - I'm trying to understand where to place that but can't seem to make it work.
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
FORTRAN: Loop over variable file names saleheen Programming 6 04-25-2015 02:09 PM
Exclude certain file names while selectingData files coming in different names in a file name called dsravanam Shell Programming and Scripting 1 11-16-2014 09:56 AM
Extract file names from a file jricks Shell Programming and Scripting 8 08-14-2012 04:42 AM
how to assign file names to array variable? siteregsam Shell Programming and Scripting 4 02-29-2012 01:08 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
Replace variable names in text file with its value vigithvg Shell Programming and Scripting 1 12-24-2009 08:54 AM
File Names in a Variable in a loop spkandy Shell Programming and Scripting 3 06-11-2009 05:55 AM
Variable assignment for file names. jagannatha UNIX for Dummies Questions & Answers 2 05-29-2003 04:38 PM


All times are GMT -4. The time now is 05:29 AM.

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