Assigning matched pattern within filename to variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Assigning matched pattern within filename to variable
# 1  
Old 04-28-2014
Assigning matched pattern within filename to variable

I am writing a bash script where I use two types of files that both contain a numerical pattern of the type 123.4567 (always groups of three and four digits separated by period) within their filenames. I need to assign the numerical patterns of these filenames to variables (inside a for loop), so that I can perform an action only if two files match. I have been trying to use grep, find or sed, but can't seem to find a way to search the filenames only (these are big files), and to match/export this pattern only. Any help would be greatly appreciated by this bash scripting newbie!

Example filenames:
Scan_562.2416.faa.txt
Anno_562.2416.txt
# 2  
Old 04-28-2014
What have you tried so far? Give us the code that you have written and show us the error you are getting. We can not help you if we dont know where you are stuck at.
# 3  
Old 04-28-2014
try
Code:
ls -ltr | awk '{print $9}' | grep [[0-9]]*.[[0-9]]* | while read file
do
echo $file
#Do whatever you want with these files
done

# 4  
Old 04-28-2014
Code:
ls -1 | awk -F "_" '{n=$2+0; split(n,a,"."); printf "%.*f\n", length(a[2]), n}'

If you think the decimal size is fixed to 4
Code:
ls -1 | awk -F "_" '{printf "%.4f\n", $2+0}'

# 5  
Old 04-28-2014
I tested the suggestions of SriniShoo, but this seems to output the numerical patterns of all filenames, while I need to recursively read the pattern from each filename into the variable (in a for loop).

So, I am looking for something more along the line of this:
Code:
for f in Scan_**
do
var1=$(#expression to output numerical pattern of single filename)
for g in Anno_**
do
var2=$(#expression to output numerical pattern of single filename)
if $var1=$Var2 
then 
#perform action on f and g
done
done

# 6  
Old 04-29-2014
For anyone that might be facing similar problems, I ended up with this solution:

Code:
for k in Anno_**
do
Var1=$(find $k -name "Anno*" | grep -o '[0-9]\{3,\}[.][0-9]\{4,\}')
for l in Scan**
do
Var2=$(find $l -name "Scan*" | grep -o '[0-9]\{3,\}[.][0-9]\{4,\}')
if [ "$Var1" == "$Var2" ]
Then
#perform action on k and l

# 7  
Old 04-29-2014
What about - in lieu of running find umpteen times - lsing your directory, sort by those numerical part of the file names:
Code:
ls Anno* Scan*| sort -nk1.6,1.14

, and then read that output file and do the actions?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files when pattern matched

I have two files say FILE1 and FILE2. FILE1 contains 80,000 filename in sorted order and another file FILE2 contains 6,000 filenames is also in sorted order. I want to compare the filename for each file and copy them in to a folder when filename is matched. File1.txt contain 80,000... (8 Replies)
Discussion started by: imranrasheedamu
8 Replies

2. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

3. Shell Programming and Scripting

Putting together substrings if pattern is matched

What I would like to do is if the lines with % have the same name, then combine the last 9 letters of the string underneath the last occurrence of that ID with the first 9 letters of the string underneath the first occurrence of that ID. I have a file that looks like this: %GOGG... (12 Replies)
Discussion started by: verse123
12 Replies

4. Shell Programming and Scripting

Matched a pattern from multiple columns

Hi, I need to extract an info in $1 based on a matched pattern in $2,$3,$4, and $5. The sample input file as follows:- ID Pat1 Pat2 Pro1 use1 add41 M M M add87 M M M M add32 ... (16 Replies)
Discussion started by: redse171
16 Replies

5. Shell Programming and Scripting

How to search a filename stored in a variable using a pattern?

hi, i have a variable which contains some file names delimited by a single space. FNAME="s1.txt s2.lst s3.cvs s4.lst" i have another variable that contains a pattern FILE_PATTERN="*.lst" i want to take the filenames from FNAME variable and assign each file name in to an array say for... (8 Replies)
Discussion started by: Little
8 Replies

6. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

7. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

8. Shell Programming and Scripting

Grep word between matched pattern

would like to print word between matched patterns using sed for example : create INDEX SCOTT.OR_PK ON table_name(....) would like to print between SCOTT. and ON which is OR_PK Please help me out Thanks (4 Replies)
Discussion started by: jhonnyrip
4 Replies

9. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

10. Shell Programming and Scripting

Replacing a word after a matched pattern

Hello, Actually i want to replace the word after a matched pattern. For Ex: lets say that i am reading a file line by line while read line do echo $line # i need to search whether a pattern exists in the file and replace the word after if the pattern exist. # for example :... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question