Compare files with regular expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare files with regular expression
# 1  
Old 06-24-2012
Compare files with regular expression

Readers,

Reading a previous post about comparing files using awk ('awk-compare-2-columns-2-files-output-whole-line', https://www.unix.com/shell-programming-scripting/168432-awk-compare-2-columns-2-files-output-whole-line.html), it is possible to adjust this, so that regular expression can be used as the criterion?

Thanks.
# 2  
Old 06-24-2012
Could you post input, output and what you tried so far?
# 3  
Old 06-24-2012
Not tried much yet. So far I have tried to understand the meaning of the command 'idx' but search results have not described this command. Example below:

file1:
Code:
text1 10
text2 20

file2:
Code:
text1textabc 10
text2textdef 20
text3textghi 30

awktest:
Code:
#! /bin/awk -f
BEGIN {FS=","}
{idx=$1, [0-9]} FNR==NR{f1[idx];next} idx in f1 file1 file2

Code:
chmod u+x awktest
./awktest file1 file2 > file 3

awk: ./awktest:3: {idx=$1, [0-9]} FNR==NR{f1[idx];next} idx in f1 file1 file2
awk: ./awktest:3:        ^ syntax error
awk: ./awktest:3: {idx=$1, [0-9]} FNR==NR{f1[idx];next} idx in f1 file1 file2
awk: ./awktest:3:              ^ syntax error
awk: ./awktest:3: {idx=$1, [0-9]} FNR==NR{f1[idx];next} idx in f1 file1 file2
awk: ./awktest:3:                                                 ^ syntax error

file3 is empty!

Would like to be able to use regular expression as a criterion to select records that match the criterion and then create a new file in the format of file 2.

Last edited by Scrutinizer; 06-24-2012 at 03:56 PM.. Reason: code tags
# 4  
Old 06-25-2012
idx is a mere variable in that script, not a command.. The script is giving syntax errors, therefore file 3 remains empty...

What is your desired output with these two input files?
# 5  
Old 06-25-2012
In summary "where a regular expression (e.g. [0-9]) within field 1 matches both file1 and file2, export that line into file3, but in the format of file2".
# 6  
Old 06-25-2012
Could you give an example?
# 7  
Old 06-25-2012
The previous post showed the example script and the errors?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find regular expression for two files?

I have files: sum_<INPUT FILENAME>.YYYYMMDDhhmmss.csv and sum_details_<INPUT FILENAME>.YYYYMMDDhhmmss.csv I have no idea, what is input filename, but in the code I would like to catch them in case I process them in the loop above case statement for *.${Today}.*.txt... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Homework & Coursework Questions

Regular Expression to match files in Perl

Hi Everybody! I need some help with a regular expression in Perl that will match files named messages, but also files named message.1, message.2 and so on. So really I need one that will find messages and messages that might be followed by a period and a digit without matching other files like... (2 Replies)
Discussion started by: Hax0rc1ph3r
2 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

How to compare a file name with a regular expression !!

Hi, I need to compare file names in a folder with several strings(which are in regular expression format): For example: there is a file "objectMyHistoryBook" and there are several strings to compare this file name with: objectMyMaths*, objectMyEnglish*, objectMyHistory*,... (2 Replies)
Discussion started by: Lucifer_123
2 Replies

5. Shell Programming and Scripting

copying files using regular expression

I have 3 files names as HU123.IHS ,SU345.IHS DU567.IHS I have written a script to copy the files to the destination server but I am getting the error. /bin/cp '/dun/homes/11.3.7/packages/HU*.IHS' /dun/homes/11.3.7/Target But I am getting one error :- /bin/cp: cannot stat... (1 Reply)
Discussion started by: maitree
1 Replies

6. Shell Programming and Scripting

search a regular expression and match in two (or more files) using bash

Dear all, I have a specific problem that I don't quite understand how to solve. I have two files, both of the same format: XXXXXX_FIND1 bla bla bla bla bla bla bla bla bla bla bla bla ======== (return) XXXXXX_FIND2 bla bla bla bla bla bla (10 Replies)
Discussion started by: TheTransporter
10 Replies

7. Shell Programming and Scripting

Regular Expression to copy files

Hi All, I have a file with the contents as below. Current_path/file1 Current_path/file2 Current_path/file3 Now i want a regular expression to copy this files to cp Current_path/file1 HOME_DIR/Current_path/file1 cp Current_path/file2 HOME_DIR/Current_path/file2 cp Current_path/file3 ... (2 Replies)
Discussion started by: girish.raos
2 Replies

8. Shell Programming and Scripting

How to apply a regular expression in all the files in a directory

I have say 100 text files (with .txt extension) in a directory. An example of the content in the file is given below "NAME" "cgd1_200" "cgd1_3210" "cgd1_560" "cgd2_2760" "cgd2_290" "cgd3_3210" "cgd3_3310" "cgd3_660" "cgd5_2130" "cgd5_4080" "cgd6_3690" "cgd6_4480" "cgd8_1540"... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

9. Shell Programming and Scripting

AWK - compare $0 to regular expression + variable

Hi, I have this script: awk -v va=45 '$0~va{print}' flo2 That returns: "4526745 1234 " (this is the only line of the file "flo2". However, I would like to get "va" to match the begining of the line, so that is "va" is different than 45 (eg. 67, 12 ...) I would not have any output. That... (3 Replies)
Discussion started by: jolecanard
3 Replies

10. Shell Programming and Scripting

compare variable against regular expression?

is it possible? if so, how? i want to check a variable whether is it a number or letter in an if-else statement (6 Replies)
Discussion started by: finalight
6 Replies
Login or Register to Ask a Question