Visit Our UNIX and Linux User Community


extract string from file name between two underscores


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract string from file name between two underscores
# 1  
Old 07-19-2012
extract string from file name between two underscores

Hi,

Here is my question,

I need to extract string between two underscores from the filename

for example, filename is

atmos_8xdaily_instant_300x300_1_12.nc

what I want to extract is 300x300.

There are many such files in my directory, so I guess the code should be like:

Code:
for file in *1_12;do
NAME= commend to extract string from($file)
NAME1=${NAME}.slp.nc
done

Thanks!
# 2  
Old 07-19-2012
one way:
Code:
for fname in *1_12
do
  tmp=$(echo "$fname" | awk -F '_' '{print $4}' )
  newfname=${tmp}.slp.nc
  echo $newfname
done

# 3  
Old 07-19-2012
Thanks. Could you explain a little bit about this line
Code:
tmp=$(echo "$fname" | awk -F '_' '{print $4}' )


Last edited by Franklin52; 07-19-2012 at 04:16 PM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 07-19-2012
awk parses the filename - it uses the underscore character to separate fields. A few shells can also do this, like bash. Field $4 is the one you want, so awk prints the the $4 field. $[field number] is the awk syntax for each field in a file - the default field separator is tab/space. The -F '_' tells awk to use underscore.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 07-19-2012
Yet another way of doing the same thing...
Code:
echo "atmos_8xdaily_instant_300x300_1_12.nc" | sed 's/.*_\([0-9][0-9]*x[0-9][0-9]*\)_.*/\1/'

This User Gave Thanks to shamrock For This Post:
# 6  
Old 07-19-2012
could you also explain a little bit about this line? Thanks!
# 7  
Old 07-19-2012
The sed command looks at a line and filters out only the part that has this pattern "(number)x(number)" punctuated by underscores. So in this pattern
Code:
.*_\([0-9][0-9]*x[0-9][0-9]*\)_.*

the regex ".*_" is the entire line to the left of the first 300 as is the "_.*" to the right of the second 300. The [0-9][0-9]* filters out only numbers and the "x" and "_" are taken literally...

Previous Thread | Next Thread
Test Your Knowledge in Computers #505
Difficulty: Medium
An executable statement may not invoke (or call or execute) another procedure (also called subroutine, function, method, etc.),
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a string from a file

I have a file with below contents. INCLUDE INCLUDE SYSLIB(SANJ) INCLUDE SYSLIB(BIS) NAME BQTFL(R) dfdg fgbb NAME B i want to grep for "INCLUDE SYSLIB" in the file and do some operation so that my output will be in the bracketed value as below. SANJ BIS Pls let me know how can i... (7 Replies)
Discussion started by: millan
7 Replies

2. Shell Programming and Scripting

No. of underscores in a file name

Hi I want to filter my files which has more than 1 underscores in it.how can i achieve this. Ex: if my file name is a_b_c, a_b. my command should result only a_b_c Thanks Pracheth (10 Replies)
Discussion started by: pracheth
10 Replies

3. Shell Programming and Scripting

Need to extract a String from log file

Hi i am having a logfile which contain lot of entires, but i need extract a word after if i found a line that contains a particular string as "ENROLLMENT_EXCEPTION - Exception". please help me in getting a script to do this. Regards C. Suresh (5 Replies)
Discussion started by: sumeeva1907
5 Replies

4. Shell Programming and Scripting

Extract a specific string from a file

Hi, I have a file whose contents are as follows. 2013-03-08/15:09:20.134 INFO 00000000-00000000 0034 09700400 CON_IN SessionID:ED5E1400-4805-85E2-17B2-5BE45684886A Connection ID:ED5E1400-4805-68F1-BB1D-F06496BCF910 TO:<sip:51234999@10.239.94.146:5060 FROM:<sip:9302280716@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies

5. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

6. Shell Programming and Scripting

awk extract a string from a file

Hi, I have a file which has thousand of lines with lines starting with And I want to extract and show to user only the below string from all the lines Please note note that the above string is a time stamp and it would be different on all the lines. Please tell me how to extract... (8 Replies)
Discussion started by: jredx
8 Replies

7. Shell Programming and Scripting

Extract a string from file

Below are the content of my file and i need to extract the 6 digit numbers after the word barcode, how can i do this? for example i need to extract 004119,004275,004030 to a new file. Logically move media ID 004119 (barcode 004119) from standalone to slot 18. Logically move media ID 004275... (9 Replies)
Discussion started by: shehzad_m
9 Replies

8. Shell Programming and Scripting

Find a string and extract a value from a file

I have a file where a line has the following form: n0=7.00 !Central density and I want to extract the value 7.00. I used to do this with the order below, which finds the string "n0" and take the rest of the line parting from the separator "=", but the comment "Central density..."... (7 Replies)
Discussion started by: josegr
7 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

Extract a string from a file

Input File: ===================================== "Server1" srvgrp="group1" srvid=10 CLOPT="-A -r -e /path/logfile -o /path/stdout" VAR1=0666 VAR2=N VAR3=0666 MIN=3 VAR4=4 VAR5=N VAR6=FASTPATH VAR7=5 VAR8=86400 VAR9=Y ... (20 Replies)
Discussion started by: kaustubh137
20 Replies

Featured Tech Videos