extract characters from file name - script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract characters from file name - script
# 1  
Old 12-08-2010
extract characters from file name - script

trying to extract the numbers in this file name:
Code:
fname="ebcdic.f0633.cmp_ebcdic.f0633.bin"
fnametmp=${fname#*[_.](V|v|F|f)}
parse=${fnametmp%%[_.](ENC|enc|CMP|cmp|BIN|bin)}}
echo FLRECL=$parse

result is FLRECL=0633.cmp_ebcdic.f0633

expected result FLRECL=0633

my guru is on holiday and i need to get this running today. any help is appreciated.

Last edited by Franklin52; 12-08-2010 at 01:29 PM.. Reason: Please use code tags
# 2  
Old 12-08-2010
I see two numbers in file name. If you need to extract only 1st no then:
Code:
 
parse=$(echo $fname | sed 's/[^0-9]*\([0-9][0-9]*\)[^ ]*/\1/')
echo FLRECL=$parse

# 3  
Old 12-08-2010
that works, but it also picks up the first number it encounters.
Code:
fname="reddogssamsfile1.f0633.bin"
fnametmp=${fname#*[_.](V|v|F|f)}
parse=$(echo $fname | sed 's/[^0-9]*\([0-9][0-9]*\)[^ ]*/\1/')
echo FLRECL=$parse

result:FLRECL=1

essentially, i need to grab 4 numbers after f and before BIN/CMP/ENC extensions.

appreciated

Last edited by Franklin52; 12-08-2010 at 01:30 PM.. Reason: Please use code tags
# 4  
Old 12-08-2010
YMMV
Code:
echo "reddogssamsfile1.f0633.bin" | sed 's/.*f\([^.][^.]*\)[.].*/\1/'

# 5  
Old 12-08-2010
worked...thank u
# 6  
Old 12-09-2010
hi,
just curious ,
Code:
echo "samplefile1.f0633.bin" | sed 's/.*\.[a-z]*\([0-9][0-9]*\)\.[bin|cmp|enc]/\1/'

output :
Code:
0633in

i couldn't find the reason why i didn't get 0633 Smilie

Last edited by dragon.1431; 12-09-2010 at 06:20 AM.. Reason: corrected typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract 8 characters from a large file.

Hi All!! I have a large file containing millions of records. My purpose is to extract 8 characters immediately from the given file. 222222222|ZRF|2008.pdf|2008|01/29/2009|001|B|C|C 222222222|ZRF|2009.pdf|2009|01/29/2010|001|B|C|C 222222222|ZRF|2010.pdf|2010|01/29/2011|001|B|C|C... (5 Replies)
Discussion started by: pavand
5 Replies

2. UNIX for Beginners Questions & Answers

Extract characters from a string name

Hi All, I am trying to extract only characters from a string value eg: abcdedg1234.cnf How can I extract only characters abcdedg and assign to a variable. Please help. Thanks (2 Replies)
Discussion started by: abhi_123
2 Replies

3. Shell Programming and Scripting

Need to extract characters between two search words in a script!!

Hi, I have a log file which is the output from a xml script : <?xml version="1.0" ?> <!DOCTYPE svc_result SYSTEM "MLP_SVC_RESULT_320.DTD"> <svc_result ver="3.2.0"> <slia ver="3.0.0"> <pos> <msid type="MSISDN" enc="ASC">8093078040</msid> <poserr> ... (4 Replies)
Discussion started by: arjunstarz
4 Replies

4. Shell Programming and Scripting

extract last 8 characters from a file name

Dear gurus I have several files with the following format filenameCCYYMMDD , that is the last 8 characters will be the date in CCYYMMDD format. eg FILENAME20110523 . Could anyone please put me through on how to extract only the last 8 characters from the files. I am thinking of using awk,sed... (2 Replies)
Discussion started by: erinlomo
2 Replies

5. Shell Programming and Scripting

Extract data between two characters

Hi, how can i extract a data between two characters input file is abc efg hig - 99.4% (16337276 kB) used efg klm - 47.1% (7734968 kB) used hij - 99.4% (16341464 kB) used klm -93.7% (15394516 kB) used output should be 99.4 (5 Replies)
Discussion started by: jobycxa
5 Replies

6. Shell Programming and Scripting

Need to extract 7 characters immediately after text '19' from a large file.

Hi All!! I have a large file containing millions of record. My purpose is to extract 7 characters immediately after text '19' from this file (including text '19') and save the result in new file. So, my OUTPUT would be as under : 191234561 194567894 192789005 198839408 and so on..... ... (7 Replies)
Discussion started by: parshant_bvcoe
7 Replies

7. UNIX for Dummies Questions & Answers

extract characters from a variable

Hi All, I have scenario as below, I want to cut the characters and store it variable2.. and want to display the variable2.. as shown below... eg: the size may differs , i am show just an example..... numbers are not fixed in variable1..i want to extract characters and... (1 Reply)
Discussion started by: G.K.K
1 Replies

8. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies

9. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (2 Replies)
Discussion started by: Shilpi
2 Replies

10. UNIX for Dummies Questions & Answers

Grep and extract the characters after

Hi All, I have lines like below in a file A /u/ab/test1.dat A/u/ab/test2.dat A /u/bb/test3.dat A/u/cc/test4.dat I will need /u/ab/test1.dat /u/ab/test2.dat /u/bb/test3.dat /u/cc/test4.dat Pls help Thanks (6 Replies)
Discussion started by: thumsup9
6 Replies
Login or Register to Ask a Question