Extract file names from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract file names from a file
# 1  
Old 08-13-2012
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:
Code:
07:00:00.000  spn  redo       [4, 00:53:00, d:/cfg/apple1.cfg, MARY, d:/cfg/apple2.cfg, d:/cfg/pear.cfg, TRUE, FALSE, TRUE]
08:04:36.200  CMD  OBJ_INIT   [JOHN, d:/cfg/apple3.cfg]
08:04:37.200  CMD  OBJ_INIT   [JOE, d:/cfg/pear2.cfg]
07:53:26.200  CMD  OBJ_INIT   [SUE, d:/cfg/apple4.cfg]
06:27:49.717  CMD  OBJ_INIT   [BOB, d:/cfg/pear3.cfg]
06:12:51.717  CMD  OBJ_INIT   [SAM, d:/cfg/orange.cfg]
06:27:50.717  CMD  OBJ_INIT   [SAM, d:/cfg/orange2.cfg]
06:13:10.017  CMD  OBJ_INIT   [TONY, d:/cfg/grape.cfg]
07:00:00.000  spn  redo       [4, 00:53:00, d:/cfg/apple5.cfg, MARY, d:/cfg/apple.cfg, d:/cfg/pear4.cfg, TRUE, FALSE, TRUE]
08:04:36.200  CMD  OBJ_INIT   [JOHN, d:/cfg/apple6.cfg]

And I've been using
Code:
sed 's/^.*\(d:\)/\1/'

which has given me this:
Code:
d:/cfg/pear.cfg, TRUE, FALSE, TRUE]
d:/cfg/apple3.cfg]
d:/cfg/pear2.cfg]
d:/cfg/apple4.cfg]
d:/cfg/pear3.cfg]
d:/cfg/orange.cfg]
d:/cfg/orange2.cfg]
d:/cfg/grape.cfg]
d:/cfg/pear4.cfg, TRUE, FALSE, TRUE]
d:/cfg/apple6.cfg]

But I don't get the multiple filenames when they are on the same row, and I can't figure how to drop the stuff after the .cfg. What I want is this:
Code:
d:/cfg/apple1.cfg
d:/cfg/apple2.cfg
d:/cfg/pear.cfg
d:/cfg/apple3.cfg
d:/cfg/pear2.cfg
d:/cfg/apple4.cfg
d:/cfg/pear3.cfg
d:/cfg/orange.cfg
d:/cfg/orange2.cfg
d:/cfg/grape.cfg
d:/cfg/apple5.cfg
d:/cfg/apple.cfg
d:/cfg/pear4.cfg
d:/cfg/apple6.cfg


Last edited by Franklin52; 08-14-2012 at 04:45 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 08-13-2012
I'm sure this is not the best way... but it's a way...

Code:
sed 's/\.cfg/\.cfg\n/g' | grep '.cfg' | awk -F"/" '{print $3}'

# 3  
Old 08-13-2012
Well, that got me the first entry of multi-entry lines instead of the last, but still don't get all entries. And it chopped off the d:/cfg/ part of the string which I need to keep. Here's the output of
Code:
sed 's/\.cfg/\.cfg\n/g' | grep '.cfg' | awk -F"/" '{print $3}'

Code:
apple1.cfg, MARY, d:
apple3.cfg]
pear2.cfg]
apple4.cfg]
pear3.cfg]
orange.cfg]
orange2.cfg]
grape.cfg]
apple5.cfg, MARY, d:
apple6.cfg]


Last edited by Franklin52; 08-14-2012 at 04:45 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 08-13-2012
This works for me:

Code:
sed 's/\.cfg/\.cfg\n/g' | grep '.cfg' | sed 's/\(.*\)\(d:*.cfg\)/\2/'

Depending on the shell you are using you might have issues with the new line part in sed:

Code:
sed 's/\.cfg/\.cfg\n/g'


Also note that this assumes that "d:" will never change and that all files end with ".cfg".

Last edited by victorbrca; 08-13-2012 at 03:14 PM.. Reason: Additional notes
# 5  
Old 08-13-2012
This is close to victorbrca's proposal, works under the assumptions he mentioned, and may benefit from some polishing:
Code:
sed  's/\.cfg/\.cfg\n/g' inputfile |sed -n '/d:.*\.cfg/ s/.*\(d:.*cfg\)/\1/p'

This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-13-2012
Thanks RudiC - I don't know how this works, but it works!!
# 7  
Old 08-13-2012
Another approach (using awk instead of sed) that seems to work is:
Code:
awk ' { for(i=1; i<= NF; i++) {
                if (sub("[.]cfg.*",".cfg",$i) == 1)
                        print $i
        }
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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: inbox/EBS/Client_GL_Detail_PBCS_112517_SEP2017.txt inbox/EBS/Client_GL_Detail_PBCS_112617_NOV2017.txt ... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

2. 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

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

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

5. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

6. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

7. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

8. 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

9. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question