Match string from two file input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match string from two file input
# 1  
Old 02-16-2012
Match string from two file input

Hello all,

I have file like this:

Code:
file 1:

aa
bb
cc
dd
ee

Code:
file2:

111
111
111
111
111
111
222
222
222
333
333
333
333
333
444
444
555
555
555
555
555
555
555
555

desire output:

output:

Code:
111 aa
111 aa
111 aa
111 aa
111 aa
111 aa
222 bb
222 bb
222 bb
333 cc
333 cc
333 cc
333 cc
333 cc
444 dd
444 dd
555 ee
555 ee
555 ee
555 ee
555 ee
555 ee
555 ee
555 ee


thanks in advance

-attila
# 2  
Old 02-17-2012
Code:
perl -ne 'BEGIN{open I,"< file1"; chomp(@x=<I>); close I; $i=-1}; chomp;
if (defined $y{$_}) { print "$_ $x[$i]\n" }
else { $y{$_} = $x[$i++]; print "$_ $x[$i]\n" }' file2

# 3  
Old 02-17-2012
Code:
awk 'NR==FNR{a[i++]=$0;next}{if (x!=$0)j++;print $0,a[j];x=$0}' i=0 j=-1 file1 file2

Guru.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. Shell Programming and Scripting

Input file needs to match a column and print the entire line

I have a file with class c IP addresses that I need to match to a column and print the matching lines of another file. I started playing with grep -if file01.out file02.out but I am stuck as to how to match it to a column and print the matching lines; cat file01.out 10.150.140... (5 Replies)
Discussion started by: lewk
5 Replies

3. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

4. Shell Programming and Scripting

Read input and match string

#!/bin/ksh echo DB LIST ps -ef | grep pmon | grep -v grep | awk -F_ '{print $3}' | sort db_up=`ps -ef | grep pmon | grep -v grep | awk -F_ '{print $3}' | sort` echo echo "Enter database name from the above list" echo read ORACLE_SID echo echo Database selected is: $ORACLE_SID echo ... (10 Replies)
Discussion started by: crazy_max
10 Replies

5. Shell Programming and Scripting

Matching string from input to string of file

Hi, i want to know how to compare string of file with input string im trying following code: file_no=`paste -s -d "||||\n" a.txt | cut -c 1` #it will return collection number from file echo "enter number" read " curr_no" if ; then echo " current number already present" fi ... (4 Replies)
Discussion started by: a_smith
4 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

List all file that match user input perl

Hi, I want to list all file that match user input ( specified shell wildcard) but when I compile it dont list me #!/usr/bin/perl -w print "Enter Advance Search Function: "; chomp ($func = <STDIN>); my @files = glob("$func"); foreach my $file (@files) { print "$file\n";... (1 Reply)
Discussion started by: guidely
1 Replies

8. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

9. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies

10. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies
Login or Register to Ask a Question