Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Compare 2 files print the lines of file 2 that contain a string from file 1 Post 302537724 by KevinRidley on Saturday 9th of July 2011 02:33:47 PM
Old 07-09-2011
Question Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help

File 1

123 232 W343
321 323 W454
999 666 W777

File 2

333 W343 989
577 W323 979
888 W777 555

Result =

333 W343 989
888 W777 555
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to print lines contains particular string format in a file

Hi, I want to print the lines in a file that matches particular string format using shell scripting. (4 Replies)
Discussion started by: sudhakaryadav
4 Replies

2. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

3. Shell Programming and Scripting

print string at the end of lines in text file

hello, I go text file like this E:/DDD/Dyndede/wwww E:/DDD/sss.com/ffffg/fff E:/DDD/vvvvvv/dd E:/DDD/sss.com/bbbbbb E:/DDD/sss.com/nnnn/xxI want to print /alpha.jpg at the end of every lines like that E:/DDD/Dyndede/wwww/alpha.jpg E:/DDD/sss.com/ffffg/fff/alpha.jpg... (8 Replies)
Discussion started by: davidkhan
8 Replies

4. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

5. Shell Programming and Scripting

Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=10 s="STRING1" FILE The file is 5 gig big. It works great and prints 10 lines after the lines which contains search string in... (8 Replies)
Discussion started by: prash184u
8 Replies

6. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

7. Shell Programming and Scripting

How to print the lines in a file for the given string?

Hi, I have a file with contents test id text day test sah dh dhs yeay fg jsh jsjk my need: I give a string as a input, it check the file and display the lines with the given string e.g input : test output: test id text day test sah dh dhs (1 Reply)
Discussion started by: nanthagopal
1 Replies

8. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

9. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

10. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies
TabularDisplay(3pm)					User Contributed Perl Documentation				       TabularDisplay(3pm)

NAME
Text::TabularDisplay - Display text in formatted table output SYNOPSIS
use Text::TabularDisplay; my $table = Text::TabularDisplay->new(@columns); $table->add(@row) while (@row = $sth->fetchrow); print $table->render; +----+--------------+ | id | name | +----+--------------+ | 1 | Tom | | 2 | Dick | | 3 | Barry | | | (aka Bazza) | | 4 | Harry | +----+--------------+ DESCRIPTION
Text::TabularDisplay simplifies displaying textual data in a table. The output is identical to the columnar display of query results in the mysql text monitor. For example, this data: 1, "Tom Jones", "(666) 555-1212" 2, "Barnaby Jones", "(666) 555-1213" 3, "Bridget Jones", "(666) 555-1214" Used like so: my $t = Text::TabularDisplay->new(qw(id name phone)); $t->add(1, "Tom Jones", "(666) 555-1212"); $t->add(2, "Barnaby Jones", "(666) 555-1213"); $t->add(3, "Bridget Jones", "(666) 555-1214"); print $t->render; Produces: +----+---------------+----------------+ | id | name | phone | +----+---------------+----------------+ | 1 | Tom Jones | (666) 555-1212 | | 2 | Barnaby Jones | (666) 555-1213 | | 3 | Bridget Jones | (666) 555-1214 | +----+---------------+----------------+ METHODS
Text::TabularDisplay has four primary methods: new(), columns(), add(), and render(). new() creates a new Text::TabularDisplay instance; columns() sets the column headers in the output table; add() adds data to the instance; and render() returns a formatted string representation of the instance. There are also a few auxiliary convenience methods: clone(), items(), reset(), populate(), and paginate(). new A Text::TabularDisplay instance can be created with column names passed as constructor args, so these two calls produce similar objects: my $t1 = Text::TabularDisplay->new; $t1->columns(qw< one two >); my $t2 = Text::TabularDisplay->new(qw< one two >); Calling new() on a Text::TabularDisplay instance returns a clone of the object. See "clone" in Text::TabularDisplay. columns Gets or sets the column names for an instance. This method is called automatically by the constructor with any parameters that are passed to the constructor (if any are passed). When called in scalar context, columns() returns the number of columns in the instance, rather than the columns themselves. In list context, copies of the columns names are returned; the names of the columns cannot be modified this way. add Takes a list of items and appends it to the list of items to be displayed. add() can also take a reference to an array, so that large arrays don't need to be copied. As elements are processed, add() maintains the width of each column so that the resulting table has the correct dimensions. add() returns $self, so that calls to add() can be chained: $t->add(@one)->add(@two)->add(@three); render render() does most of the actual work. It returns a string containing the data added via add(), formatted as a table, with a header containing the column names. render() does not change the state of the object; it can be called multiple times, with identical output (including identical running time: the output of render is not cached). If there are no columns defined, then the output table does not contains a row of column names. Compare these two sequences: my $t = Text::TabularDisplay->new; $t->add(qw< 1 2 3 4 >); $t->add(qw< 5 6 7 8 >); print $t->render; $t->columns(qw< one two three four >); print $t->render; # Example 1 output +---+---+---+---+ | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | +---+---+---+---+ # Example 2 output +-----+-----+-------+------+ | one | two | three | four | +-----+-----+-------+------+ | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | +-----+-----+-------+------+ render() takes optional $start and $end arguments; these indicate the start and end indexes for the data to be rendered. This can be used for paging and the like: $t->add(1, 2, 3)->add(4, 5, 6)->add(7, 8, 9)->add(10, 11, 12); print $t->render(0, 1), " "; print $t->render(2, 3), " "; Produces: +-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 1 | 2 | 3 | | 4 | 5 | 6 | +-------+--------+-------+ +-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 7 | 8 | 9 | | 10 | 11 | 12 | +-------+--------+-------+ As an aside, note the chaining of calls to add(). The elements in the table are padded such that there is the same number of items in each row, including the header. Thus: $t->columns(qw< One Two >); print $t->render; +-----+-----+----+ | One | Two | | +-----+-----+----+ | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | | 10 | 11 | 12 | +-----+-----+----+ And: $t->columns(qw< One Two Three Four>); print $t->render; +-----+-----+-------+------+ | One | Two | Three | Four | +-----+-----+-------+------+ | 1 | 2 | 3 | | | 4 | 5 | 6 | | | 7 | 8 | 9 | | | 10 | 11 | 12 | | +-----+-----+-------+------+ OTHER METHODS
clone() The clone() method returns an identical copy of a Text::TabularDisplay instance, completely separate from the cloned instance. items() The items() method returns the number of elements currently stored in the data structure: printf "There are %d elements in $t. ", $t->items; reset() Reset deletes the data from the instance, including columns. If passed arguments, it passes them to columns(), just like new(). populate() populate() as a special case of add(); populate() expects a reference to an array of references to arrays, such as returned by DBI's selectall_arrayref method: $sql = "SELECT " . join(", ", @c) . " FROM mytable"; $t->columns(@c); $t->populate($dbh->selectall_arrayref($sql)); This is for convenience only; the implementation maps this to multiple calls to add(). NOTES
/ ISSUES Text::TabularDisplay assumes it is handling strings, and does stringy things with the data, like length() and sprintf(). Non-character data can be passed in, of course, but will be treated as strings; this may have ramifications for objects that implement overloading. The biggest issue, though, is that this module duplicates a some of the functionality of Data::ShowTable. Of course, Data::ShowTable is a large, complex monolithic tool that does a lot of things, while Text::TabularDisplay is small and fast. AUTHOR
darren chamberlain <darren@cpan.org> CREDITS
The following people have contributed patches, suggestions, tests, feedback, or good karma: David N. Blank-Edelman Eric Cholet Ken Youens-Clark Michael Fowler Paul Cameron Prakash Kailasa Slaven Rezic Harlan Lieberman-Berg Patrick Kuijvenhoven VERSION
This documentation describes "Text::TabularDisplay" version 1.33. perl v5.14.2 2012-07-05 TabularDisplay(3pm)
All times are GMT -4. The time now is 03:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy