Help with finding text in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with finding text in file
# 1  
Old 02-04-2010
Help with finding text in file

Hello,

Please help me with this.

I have two files. file1.txt and file2.txt
File1 contains text as below
txt1 txt2
txt3 txt4
txt5 txt6
txt7 txt8

File2 contains text as below
$1 $2 $3 $4 $5 $6 $7 $8 $9
-----------------------------
txt1 txt2 - - - 0 2 8 -*-
txt0 txt7 - - - 1 4 8 -*-
txt3 txt4 - - - 9 7 6 -**
txta txtb - - - 5 0 8 -*-
txta txtb - - - 0 0 -1 -*-
txt5 txt6 - - - 8 3 9 **-
txt7 txt8 - - - 1 4 5 -*-

Is there a way I can print all the content in file2 matching with file1 as follows.
$1 $2 $8
txt1 txt2 8
txt3 txt4 6
txt5 txt6 9
txt7 txt8 5

Thanks so much.
# 2  
Old 02-04-2010
Use nawk or /usr/xpg4/bin/awk on Solaris:
Code:
awk 'NR==FNR{a[$1$2];next} $1$2 in a {print$1, $2, $8}' file1 file2

# 3  
Old 02-04-2010
Code:
$ sort file1 > file1.srt
$ sort file2 > file2.srt
$ join -1 1,2 -2 1,2 -o 2.1,2.2,2.8 file1.srt file2.srt
txt1 txt2 8
txt3 txt4 6
txt5 txt6 9
txt7 txt8 5

# 4  
Old 02-05-2010
Code:
grep -f txt1 txt2 | awk '{print $1,$2,$8}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding pattern in a text file and returning a part of the word

Dear All, assume that we have a text file or a folder of files, I want to find this pattern followers*.csv in the text file , and get * as the output. There are different matches and * means every character. Thank you in advance. Best, David (1 Reply)
Discussion started by: davidfreed
1 Replies

2. Shell Programming and Scripting

Finding a text file from a group of zip files without unzipping

HI , There are more than 100 zip files in a directory and i wanted to see if there is a max1157.txt file in any of the zip files without actually unzipping them. Could you please help. Thanks in Advance. Karthik. (6 Replies)
Discussion started by: karthikk0508
6 Replies

3. Shell Programming and Scripting

Finding the second last column value from a text file

Can any one tell me how to get the second last column value from the text file, which has different record size for each record. I know how to get the last column using awk and print statements, but I am unable to get the second last column value from the file. (4 Replies)
Discussion started by: naveen_sangam
4 Replies

4. Shell Programming and Scripting

Finding the last column value from a text file

Hi, I need to find out the last column value from a text file which is delimited by a tab. The issue here is the last column# for each record can be different i.,e, 1st record can have the last column as 15 and the second record can have the last column as "17". I have to search a string... (3 Replies)
Discussion started by: naveen_sangam
3 Replies

5. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

6. Shell Programming and Scripting

add newline in file after finding specific text

Hi All, I am tring to insert a newline with "/" in a text file whenever there is the text "end;" right now I have inside file: . . end; I want to have: . . end; / I tried doing the following within the file :g/^end;/s//end; \/ / (4 Replies)
Discussion started by: jxh461
4 Replies

7. UNIX for Dummies Questions & Answers

finding text in a file

How do i find text string in a file if i have no idea where the file is? What I am trying to do is find an email address in a file and have no idea where the file is. Thanks (5 Replies)
Discussion started by: §ynic
5 Replies

8. Shell Programming and Scripting

Finding out if there is text in a file.

I have a script that when run creates 4 other text files. Sometimes a couple of these text files are empty. So what I am trying to do is find out if there is text in the file and if not delete the file. What I have so far is the following script. I am just not sure how to return a true value... (9 Replies)
Discussion started by: iCONAN
9 Replies

9. UNIX for Dummies Questions & Answers

finding text inside file

Hi everyone I have a small problem i cant find a soloution to... I'm using digital unix and have to find out all the files which have a certain string inside them and i dont know how to do it. *This search is done as root. *All files from '/' to the last directory should be searched for... (3 Replies)
Discussion started by: dindan100
3 Replies

10. Filesystems, Disks and Memory

finding out the pid for a busy text file

Can some one please tell me how to find out the proccess ID that is holding up a file. I am attempting to remove a file and I am getting a message stating that it is busy. i.e rm filename filename: 777 mode ? (y/n) y rm: filename not removed. Text file busy Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies
Login or Register to Ask a Question