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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract file names from file and set variable to 'highest' one
# 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:
Mod Comment Please use CODE tags, as required by forum rules, when displaying sample input, output, and code segments.

Last edited by Don Cragun; 12-21-2017 at 12:55 AM.. 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Programming

FORTRAN: Loop over variable file names

Hi guys I'm a beginner in fortran. So excuse me for my naivety, let me briefly describe what I was trying to do. I have let's say 2 files named reac-1 and reac-2. After opening these files I've to do some calculations, close these files and open the same files again in a loop. So my faulty code... (6 Replies)
Discussion started by: saleheen
6 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. Shell Programming and Scripting

Extract file names from a file

I'm trying to extract a list of each .cfg file name mentioned in a file. I've made some progress using sed, but I'm still not there. Any help would be appreciated. My input looks like this: 07:00:00.000 spn redo 08:04:36.200 CMD OBJ_INIT 08:04:37.200 CMD OBJ_INIT ... (8 Replies)
Discussion started by: jricks
8 Replies

5. Shell Programming and Scripting

how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array. plz help me how to accomplish this. Thanks in advance. (4 Replies)
Discussion started by: siteregsam
4 Replies

6. Shell Programming and Scripting

set a variable to be a list of file names

Hi all, I am trying to get a list of all the .tif images in my folder and set a variable that holds all the file names using C shell. What is the best way to do it? any help would be greatly appreciated. Yang (4 Replies)
Discussion started by: lionheartyoung
4 Replies

7. UNIX for Dummies Questions & Answers

command to extract sub-string out of file names

I have these files in a directory. It may have more class than the sample below: DEPT_CHEM101LEC_D_20110301.DAT DEPT_CHEM101LAB_D_20110301.DAT DEPT_BIO105LEC_D_20110325.DAT DEPT_BIO105LAB_D_20110325.DAT DEPT_CSC308LEC_D_20110327.DAT DEPT_CSC308LAB_D_20110327.DAT Is there way to extract out... (5 Replies)
Discussion started by: lv99
5 Replies

8. Shell Programming and Scripting

Replace variable names in text file with its value

Hi I have a text file (mytext.txt), the content of which is as following: My name is <@MY_NAME@> and my age is <@MY_AGE@> . Now i have another property file (myprops) which is as following: MY_NAME=abcdefgh MY_AGE=000000 I was wondering, how can i replace the tags of mytext.txt, with its... (1 Reply)
Discussion started by: vigithvg
1 Replies

9. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

10. UNIX for Dummies Questions & Answers

Variable assignment for file names.

I am trying to process error files in selected directories. I can count the files that are there and export the contents to a file for either emailing or printing. The next step is to move the files to a processed directory with the name changed to .fixed as the last extension. for file in... (2 Replies)
Discussion started by: jagannatha
2 Replies
Login or Register to Ask a Question