Sponsored Content
Top Forums UNIX for Advanced & Expert Users print contents of file2 for matching pattern in file1 - AWK Post 302351245 by Vi-Curious on Monday 7th of September 2009 08:35:33 PM
Old 09-07-2009
Quote:
Originally Posted by i.scientist
this worked but ...can u please explain ?
Quick/dirty and slightly inefficient but ok....

Code:
perl -nle '<perl expression>' file1 file2

Read each line from file1 and file2 and perform <perl expression> on each line.

Code:
if (/^(\d+)$/) {$x .= "|$1";}

If the line consists of a single integer number, append it to variable x using | as a separator. The ^ is beginning of line, the $ is end of line and (\d+) represents one or more decimal digits. After your file1 example is processed, $x will equal |100|103|104|108.

Code:
else { $y = substr($x,1);print $_ if /[|]$y[|]/ }

If the line is not a single integer (then it will be your entries from file2), strip the first character off of variable x and save what remains in y. This is inefficient because it has to be done for each line that is processed. This places 100|103|104|108 in y. Print the line if it contains any of the strings in variable y located between two | characters.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies

2. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

4. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

5. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

6. Shell Programming and Scripting

awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with... (6 Replies)
Discussion started by: msmehaffey
6 Replies

7. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

8. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

9. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies
cmp(1)							      General Commands Manual							    cmp(1)

NAME
cmp - compare two files SYNOPSIS
file1 file2 [skip1 [skip2]] DESCRIPTION
compares two files (if file1 or file2 is the standard input is used). Under default options, makes no comment if the files are the same; if they differ, it announces the byte and line number at which the difference occurred. If one file is an initial subsequence of the other, that fact is noted. skip1 and skip2 are initial byte offsets into file1 and file2, respectively; and maybe octal or decimal; the form of the number is determined by the environment variable (in the C locale, a leading 0 denotes an octal number. See on environ(5) and strtol(3C)). recognizes the following options: Print the byte number (decimal) and the differing bytes (octal) for each difference (byte numbering begins at 1 rather than 0). Print nothing for differing files; return codes only. EXTERNAL INFLUENCES
Environment Variables determines the language in which messages are displayed. If is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of If any internationalization variable contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5). International Code Set Support Single- and multi-byte character code sets are supported. DIAGNOSTICS
returns the following exit values: Files are identical. Files are not identical. Inaccessible or missing argument. prints the following warning if the comparison succeeds till the end of file of file1(file2) is reached. SEE ALSO comm(1), diff(1). STANDARDS CONFORMANCE
cmp(1)
All times are GMT -4. The time now is 12:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy