Extracting a portion of the filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a portion of the filename
# 1  
Old 06-18-2013
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
# 2  
Old 06-18-2013
Hi,

What have you tried so far ?

Thanks
Pravin
# 3  
Old 06-18-2013
Hi Pravin

I am thinking of using the sed command, but dont know how to go about it.

I have a script

Code:
for i in `ls *TEXTPATTERN*`;do grep String $i | wc -l | grep -v 0 | $i >> matchedfiles.txt;done

on the last $i I would like to just output a portion filenames for matched file.

Last edited by radoulov; 06-18-2013 at 07:53 AM..
# 4  
Old 06-18-2013
Code:
$ a="123456789_TEXT_TEXT_TEXT_.csv"
$ echo ${a%%_*}
123456789
$ echo $a | sed 's/_.*//'
123456789

Code:
for i in *TEXT*
do echo ${i%%_*}
done >matchedfiles.txt


Last edited by ctsgnb; 06-18-2013 at 07:28 AM..
# 5  
Old 06-18-2013
Could this help you ?
This will print the first part of the file (string seperated by "_")

Code:
 
awk -F"_" '{print $1}' ListFiles > newFile.txt

# 6  
Old 06-18-2013
Code:
cut -d"_" -f1 filename


Last edited by radoulov; 06-18-2013 at 05:55 PM..
# 7  
Old 06-18-2013
Linux

This is easieds and simple you can do

Code:
filename="123456789_TEXT_TEXT_TEXT_.csv"
ext="${filename:0:9}"
echo $ext

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting filename

I am using bash and have a filename with a path and extension and want to extract just the filename Have used the following code, oflna gives the file name with extension, but now neet to remove the .texi at the end. oflna=${flnm##*/} oflnb=${${flnm##*/}%.*} echo "flnm: $flnm" echo... (1 Reply)
Discussion started by: Danette
1 Replies

2. UNIX for Beginners Questions & Answers

Extracting directory portion.

Dear Experts, I have some directory structure something like follows. I would like to cut portion of it. Would you please help me? I have to run this on several sql's. The directory path is dynamic. I have cut what comes after first "sql" string. Input:... (3 Replies)
Discussion started by: srikanth38
3 Replies

3. Shell Programming and Scripting

Extracting a portion of the string and comparing

I have 2 text files say file1.txt and file2.txt . Some of the sample records for file1.txt were shown below: XXXXX12345XXXXXXX12 3456789YYYYY XXXXXXXXXX12345XX123457485YYYYY XX12345XXXXXXXXXX123454658YYYYY for file2.txt, some of the sample records were shown below: ... (5 Replies)
Discussion started by: bobby1015
5 Replies

4. Shell Programming and Scripting

Help on extracting portion of string

Hi Gurus, I've some sample of my log information as shown below. -> Processing ABCD123456 This is tp version 372.04.57 (release 700, unicode enabled) This is R3trans version 6.14 (release 700 - 05.03.09 - 08:28:00). unicode enabled version R3trans finished (0000). Warning: Parameter... (1 Reply)
Discussion started by: superHonda123
1 Replies

5. Shell Programming and Scripting

Cutting out text from specific portion on filename

Hi, how do I go about cutting out the first numeric characters after the word "access"? access1005101228.merged-00.15.17.86.d8.b8.log.gz (16 Replies)
Discussion started by: GermanJulian
16 Replies

6. Shell Programming and Scripting

Extracting a portion of data from a very large tab delimited text file

Hi All I wanted to know how to effectively delete some columns in a large tab delimited file. I have a file that contains 5 columns and almost 100,000 rows 3456 f g t t 3456 g h 456 f h 4567 f g h z 345 f g 567 h j k lThis is a very large data file and tab delimited. I need... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

7. Shell Programming and Scripting

Extracting a portion of a data file with identifier

Hi, I do have a TAB delimted text file with the following format. 1 (- identifier of each group. this text is not present in the file only number) 1 3 4 65 56 WERTF 2 3 4 56 56 GHTYHU 3 3 5 64 23 VMFKLG 2 1 3 4 65 56 DGTEYDH 2 3 4 56 56 FJJJCKC 3 3 5 64 23 FNNNCHD 3 1 3 4 65 56 JDHJDH... (9 Replies)
Discussion started by: Lucky Ali
9 Replies

8. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

9. Shell Programming and Scripting

File ordering by portion of filename

Hi, Lets say I have a few xml files: 1234567894.xml abc_1234567895.xml abc_000_1234567890.xml abc_0000000_1234567893.xml 684_abc_000_1234567899.xml The naming convention of the files is: xxxxx_timestamp.xml OR timestamp.xml where x can be anything and I would like to order them by... (4 Replies)
Discussion started by: Leion
4 Replies

10. 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
Login or Register to Ask a Question