Get Substring from file name.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Substring from file name.
# 1  
Old 03-16-2013
Get Substring from file name.

Hi,

In my shell script. I am reading all files in directory.

And say if the file names are as follows:

Code:
adio_idfl_201302_df.txt
dfa_201301_dll.ctl
dalkd_20130301.csv

I would like to extract the numeric piece of each file name and display as follows:
Code:
201302
201301
20130301

I think we should be able to do with sed command but could not completley get it.

Code:
 
for i in `ls -1`
do
j=`echo $i | sed 's/_[0-9]*/[0-9]/'`
echo $j
done

Help is appreciated to fix the above code.

Last edited by Scrutinizer; 03-17-2013 at 04:27 AM.. Reason: more code tags
# 2  
Old 03-16-2013
Here is one way to extract the number from those files:
Code:
 for i in *; do
  j=`echo $i | sed 's/.*_\([0-9]\+\).*/\1/'`
  echo $j
done

Code:
 for i in *; do
  j=`echo $i | sed -r 's/.*_([0-9]+).*/\1/'`
  echo $j
done

Because * is 0 or more, _[0-9]* was picking up the first '_' instead of the one before '2'. And you have to use \1 to do the replacement.
# 3  
Old 03-16-2013
You don't need sed to do such a simple task (provided your shell is capable of doing the following):
Code:
i='adio_idfl_201302_df.txt'

echo "${i//[!0-9]/}"
201302

# 4  
Old 03-16-2013
Or you can try:

Code:
ls  | tr -dc '[:digit:]'

[edit]Thanks alister [/edit]

Last edited by jim mcnamara; 03-16-2013 at 11:48 PM..
# 5  
Old 03-16-2013
Another approach using BASH_REMATCH
Code:
for file in *
do
        [[ "$file" =~ [0-9]+ ]] && printf "%s\n" "${BASH_REMATCH[0]}"
done

From BASH manual:
Code:
BASH_REMATCH

An array variable whose members are assigned by the =~ binary operator to the [[ conditional command.  The element with index  0  is  the  portion  of  the
string  matching  the entire regular expression.  The element with index n is the portion of the string matching the nth parenthesized subexpression.  This
variable is read-only.

# 6  
Old 03-16-2013
Quote:
Originally Posted by jim mcnamara
Or you can try:

Code:
ls * | tr -dc '[:digit:]'

You probably meant ls and not ls *.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 03-17-2013
Quote:
Originally Posted by jim mcnamara
Or you can try:

Code:
ls  | tr -dc '[:digit:]'

[edit]Thanks alister [/edit]
Code:
$ ls
adio_idfl_201302_df.txt  dalkd_20130301.csv  dfa_201301_dll.ctl
$ ls | tr -dc [:digit:]
20130220130301201301
$ ls | tr -dc '[\n[:digit:]]'
201302
20130301
201301

This User Gave Thanks to anbu23 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match substring from a column of the second file

I want to merge the lines by matching substring of the first file with first column of the second file. file1: S00739A_ACAGTG_L001_R1.fq.gz S00739A_ACAGTG_L001_R2.fq.gz S00739B_GCCAAT_L001_R1.fq.gz S00739B_GCCAAT_L001_R2.fq.gz S00739D_GTGAAA_L001_R1.fq.gz S00739D_GTGAAA_L001_R2.fq.gz... (14 Replies)
Discussion started by: yifangt
14 Replies

2. Shell Programming and Scripting

Extract a substring from a file

Hello, A question please. A have a file that contains a string. Ex: AAAABBCCCCCDDEEEEEEEEEEFF I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file. From position 5, 2 caracters (ex:"BB") and from position 25, 2 caracters (ex:"FF") in a file. Could anoyone help me... (3 Replies)
Discussion started by: nolo41
3 Replies

3. Shell Programming and Scripting

Extract substring in a file

Hello, A question please. A have a file that contains a string. Ex: AAAABBCCCCCDDEEEEEEEEEEFF I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file. Could anoyone help me please? Thanks in advance (3 Replies)
Discussion started by: nolo41
3 Replies

4. UNIX for Dummies Questions & Answers

Deleting file basing on the timestamp substring in the file name

Hello, I have in my backup folder, files with names convention like this : randomFileNames_13-02-2014_23h13m09+1392333189 randomFileNames_14-02-2014_02h13m09+1392343989 randomFileNames_14-02-2014_04h13m09+1392351189 etc.... Base on timestamp at end of the filename, I would to delete all the... (7 Replies)
Discussion started by: thuyetti
7 Replies

5. Shell Programming and Scripting

Substring from the file name

Hi I need substring from the file name of list of files. I mean i will have to load all the files into database which are there in the Directory. I need to make the directory with the date(which is substring of the filename Eg: Filename_Name_2012013001010101.txt, So the directory should be... (11 Replies)
Discussion started by: cnrj
11 Replies

6. Shell Programming and Scripting

Get value of substring from a file

Hi, I have a file with a long string, in the format: name1=value,name2=value2.....namen=valuen I want to query the file and extract 'value2' ONLY. name2=value2 can exist anywhere within the file. Can anyone help me please wth a suitable BASH command? Many thanks, Matt (4 Replies)
Discussion started by: mjwoodford
4 Replies

7. Shell Programming and Scripting

How do I pull a substring out of a file?

I'm new to shell scripting and am trying to write a small script that pulls a substring out of a file that has a few lines of text in it. The file will eventually have a lot of text, I just put a few lines in it for testing purposes. Anyway, this is what I have so far... #!/bin/ksh ... (4 Replies)
Discussion started by: enator45
4 Replies

8. Shell Programming and Scripting

Help on substring of file list

Hi, I have a folder list as below, I want to write a script to return a list like: CASTLE_BU_20080801 CAUSEWAY_BU_20080801 HUNGHOM_BU_20080801 : : Can anyone help? Thanks! Victor Cheung List of the folder: # ls CASTLE_BU_20080801.DMP OFFICE_MY1_BU_20080801.DMP... (1 Reply)
Discussion started by: victorcheung
1 Replies

9. Shell Programming and Scripting

extracting substring from a file name

hi i need to name a file with a substring of a another file name. i.e. if the old filename is abc.txt , the new filename should be abc_1.txt i should get the substring of the file name and then name the new one please let me know how to do it (4 Replies)
Discussion started by: adityamahi
4 Replies

10. Shell Programming and Scripting

Calling a substring from another file

I have two files: One is a list of numbers: 1 5 6 7 10 The second is a very long string abcdefghijklmno......1234567890 I'd like to print a new file with a 5 character substring that starts at each the positions listed in the 1st file: such as: abcde (4 Replies)
Discussion started by: dcfargo
4 Replies
Login or Register to Ask a Question