Pattern matchin Between Two Files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pattern matchin Between Two Files
# 1  
Old 04-05-2010
Pattern matchin Between Two Files

Hi All,
I have two files as below:

Code:
file1                 file2
AAAA                CCCC,1234,0909
BBBBB               AAAA,1234
AAAA                DDDD,23536,9090 
CCCC                
DDDD                 
EEEEE

I want a out file as below

AAAA,1234
BBBB
AAAA,1234
CCCC,1234,0909
DDDD,23536,9090 
EEEE

Can any one please help me..

Cheers...
# 2  
Old 04-05-2010
Code:
awk -F, 'NR==FNR{for(i=1;++i<=NF;) {arr[$1]=arr[$1]","$i}next}$1 in arr{print $1,arr[$1];next}1' file2 file1

# 3  
Old 04-05-2010
Some kind of error. I am not able to find it.

awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 04-05-2010
Use nawk inplace of awk.
# 5  
Old 04-05-2010
It was a magic Smilie

---------- Post updated at 07:05 PM ---------- Previous update was at 07:03 PM ----------

Could you please tell me the "1" in the code below
Code:
....next}1'

# 6  
Old 04-05-2010
Quote:
Originally Posted by thana
It was a magic Smilie

---------- Post updated at 07:05 PM ---------- Previous update was at 07:03 PM ----------

Could you please tell me the "1" in the code below
Code:
....next}1'

1 represents true, so if $1 doesn't exist in arr, it prints the value of the current file which is file1 or else it prints the value which is stored in arr array and go to next record.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

3. Shell Programming and Scripting

Pattern Matchin Huge File

Hi Experts, I've issue with the huge file. My requirement is I need to search a pattern between the 155-156 position and if its match's to 31 or 36 then need to route that to a new separate files. The main file has around 1459328 line and 2 GB in size. I tired with the below code which take... (9 Replies)
Discussion started by: senthil.ak
9 Replies

4. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

5. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

6. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

7. Shell Programming and Scripting

Searching all files that contain pattern

Hello All, i have to search a pattern in all the files in all subfolders that are present in current directory. suppose i am in d1 directory and in that sd1,sd2,sd3 are subdirectories. in sd1 i have files f1,f2 sd2 i have files f3,f4 sd3 i have file f5 i have to list out all those... (4 Replies)
Discussion started by: ravi.sadani19
4 Replies

8. UNIX for Dummies Questions & Answers

looking for files not containing a pattern

Hi All, I would like to look for all the files in a directory (and subdirectory) NOT containging a given string (the string is "LOST 100 100"). Any idea how to proceed? Thanks a lot, Gino (9 Replies)
Discussion started by: f_o_555
9 Replies

9. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies

10. Shell Programming and Scripting

Simple to you not simple to me pattern matchin help

hey all, im new and my first question is: say i have a word "blahblah" how do i get and replace the last letter of the word with say k, so replace the h with a k. However you cant just replace the h it has to change the LAST LETTER of the word. Cheers In advance. :b: (0 Replies)
Discussion started by: aleks001
0 Replies
Login or Register to Ask a Question