extract string portion from filename using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract string portion from filename using sed
# 8  
Old 05-22-2009
Quote:
Originally Posted by fpmurphy
BTW, there is no need to invoke sed to parse the filename. It can all be done within bash i.e.
Code:
$ fname="TOP_TABIN240_20090323.200903231830"
$ source_file_issue_date=$(tmp=${fname/*_}; echo ${tmp/\.*})
$ echo "source_file_issue_date: $source_file_issue_date"
source_file_issue_date: 20090323
$

well, you would still need a loop to go through many such files. if its a while loop, such as
Code:
ls TOP* | while read F
do
 ....
done

then might as well pipe these files to sed (or awk) as its faster this way.
# 9  
Old 05-22-2009
Quote:
Originally Posted by ghostdog74
well, you would still need a loop to go through many such files. if its a while loop, such as
..skip..
then might as well pipe these files to sed (or awk) as its faster this way.
Are you sure? Each iteration of a loop with a pipe (if I got what you meant) means a new child process. 20000 files = 20000 processes. Lots of overhead.

With the shell-only version it does do things in a single process.

What IS regrettable (IMO) is that bash keeps changing - you can only do xx operation with version x.y and higher. While the same thing happens elsewhere: ksh vs maybe ksh93, newbies do not have a clue what bash version they have.
# 10  
Old 05-22-2009
The OP uses bash so there is no need to loop or to use external commands (assuming no IFS characters in the filenames):

Code:
% ls TOP*
TOP_TABIN235_1_20090323.200903231830  TOP_TABIN240_20090323.200903231830
% files=(TOP*) files=(${files[@]%.*})
% printf '%s\n' "${files[@]##*_}"    
20090323
20090323

# 11  
Old 05-22-2009
hi every body
i tried something may be add value in this discussion

tmp=TOP_TABIN240_20090323.200903231830
solve1=${tmp##*_} ; echo ${solve1%%.*}
20090323
# 12  
Old 05-22-2009
Quote:
Originally Posted by jim mcnamara
Are you sure? Each iteration of a loop with a pipe (if I got what you meant) means a new child process. 20000 files = 20000 processes. Lots of overhead.
yes, you are right, i am saying that.

I see that radoulov has given a pure bash solution, well , even so, there is not much of a difference in performance whether one do it in pure bash syntax or pipe to tools like awk.

Last edited by ghostdog74; 05-22-2009 at 09:55 PM..
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 a portion of string from each line in Linux

Hi I have to extract the destination path information from each record the file is of variable length so I will not be able to use the print command.The search should start on variable "destinationPath" and it should end at immediate "," also the first field has to be printed Input File:... (7 Replies)
Discussion started by: rkakitapalli
7 Replies

2. Shell Programming and Scripting

Extract filename from a given string

I want to extract the filename from a string. This is how I have the rawdata ina file /home/sid/ftp/testing/abc.txt /home/sid/ftp/tested/testing/def.txt /home/sid/sftp/date/misc/hij.txt i want a script which would provide me an output like this Directory ... (10 Replies)
Discussion started by: sidnow
10 Replies

3. Shell Programming and Scripting

Extracting a portion of the filename

Hi I would like to extract the first portion of filename from a list of files. The filename pattern is of the form 123456789_TEXT_TEXT_TEXT_.csv. I want to extract just the numerical portion of this filename from the list of files and then output this into another text file. K (6 Replies)
Discussion started by: kamal_p_99
6 Replies

4. Shell Programming and Scripting

Extract string from filename

Hi I need to extract the string from file name filename: FILENAME_STRUT_01032013_XXXXXXX.TXT I want 01032013 from the above file name. number of characters may differ before the required string but underscores(-) are same number i.e. after second underscore. Please advise on this. ... (2 Replies)
Discussion started by: cnrj
2 Replies

5. Shell Programming and Scripting

extract every filename containing certain string in a directory and do some commend per file

Hi, Here is my question: suppose I have files like 1990_8xdaily_atmos.nc 1991_8xdaily_atmos.nc 1992_8xdaily_atmos.nc 1993_8xdaily_atmos.nc 1990_daily_atmos.nc 1991_daily_atmos.nc 1992_daily_atmos.nc 1993_daily_atmos.nc 1990_month_atmos.nc 1991_month_atmos.nc 1992_month_atmos.nc... (1 Reply)
Discussion started by: 1988PF
1 Replies

6. Shell Programming and Scripting

How to extract portion of a string?

Hi Gurus, Would like to seek some help on how to extract a portion of string from log's output as shown below. Sample of raw data: piece handle=/test123/disk_dump/test123/df0_cntrl_PCPFCI20120404_68498 tag=TAG20120404T180035 comment=NONE piece... (13 Replies)
Discussion started by: superHonda123
13 Replies

7. Shell Programming and Scripting

extract string portion using sed

Hi All I have 3 files as listed below and highlighted in bold the portions of the filenames I need to extract: TOS_TABIN218_20090323.200903231830 TOS_TABIN219_1_20090323.200903231830 TOS_TABIN219_2_20090323.200903231830 I tried source_tabin_name=`echo $fname | sed 's/_.*//'` but I... (6 Replies)
Discussion started by: santam
6 Replies

8. Shell Programming and Scripting

Match portion of the filename

hi, I have a script which accept filename and process them, however, one of the file needs 'special' handling so I need to identify it, say the filename contains the word "STOCK" (i.e. NEWYORKSTOCKLIST20060627.txt), I want to check if the filename contains the word "STOCK", how can I do that?... (1 Reply)
Discussion started by: mpang_
1 Replies

9. Shell Programming and Scripting

need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename. I have tried the following sed commands: sed "s/stringA/stringB/g" * This will print the correct results but does not actually save it with the new content of the file. when I do a cat on... (5 Replies)
Discussion started by: jjoves
5 Replies

10. UNIX for Dummies Questions & Answers

How to extract a portion of a string from the whole string

How to extract a portion of a string from a full string using unix. For example: Say source string is = "req92374923.log" I want only the numeric portion of the string say "92374923" how to do that in Unix. (2 Replies)
Discussion started by: ds_sastry
2 Replies
Login or Register to Ask a Question