how to display only line that contain in 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to display only line that contain in 2 files
# 1  
Old 01-31-2012
how to display only line that contain in 2 files

Dear all, I have 2 file, as bellow. what I want is to display only number that show in both 2 files, that I marked blue color.
first_file.txt :
510289880773910
510289880773965
510289880774004
510289880774036
510289880774067
510289880774080
510289880774085
second_file.txt:
510289880774639
510289880774679
510289880774004
510289880774693
510289880774772
510289880774080

result is :
third.txt
510289880774004
510289880774080

I try using diff but the result is not as I expected. maybe sed, awk or any linux command can do this. thanks

# 2  
Old 01-31-2012
May be google.
Code:
grep -f file1 file2

# 3  
Old 01-31-2012
Code:
awk 'NR==FNR{a[$1];next} $i in a' first_file.txt second_file.txt

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 01-31-2012
I try using awk and it working great.
when use grep result is none maybe because file is very big, contain 1.5 million record per file.
thanks for all your help. very appreciate.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read the line from the last and display the one which is non-zero

Hi All, My requirement is this. Am trying to find a one liner unix command, which will check the last line of the file for 0. If it is zero, it would pick the previous line, else it should display the last line. Eg: abcd efgh ijhk 0 0 Output expected -- ijhk Please share your... (8 Replies)
Discussion started by: hariniiyer300
8 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

Print line display

Hi, i have problem saying there are two files 1. inputfile 2. outputfile input file contains some lines, my problem is i need to write a shell script to copy contains on input file to output file and also it need to display each line copying from input file to output file. i copied contains... (1 Reply)
Discussion started by: inshafccna
1 Replies

4. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

5. Shell Programming and Scripting

Display value from line

Hi I need Your help with easy script (i think ;) )... I grep one of line from file. This line looks like this: var ppp_info = How can I only display: value4 and value6. Without ""? (2 Replies)
Discussion started by: johnyb
2 Replies

6. UNIX for Dummies Questions & Answers

write a program in c in unix that display the files(includ sub-direc and files within) in a sorted

the sorting is based on name of file, file size modification time stamps o f file it should dislay the output in the following format "." and ".." enteries should be ignored please give some idea how to do it (1 Reply)
Discussion started by: pappu kumar jha
1 Replies

7. UNIX for Dummies Questions & Answers

Find files and display only directory list containing those files

I have a directory (and many sub dirs beneath) on AIX system, containing thousands of file. I'm looking to get a list of all directory containing "*.pdf" file. I know basic syntax of find command, but it gives me list of all pdf files, which numbers in thousands. All I need to know is, which... (4 Replies)
Discussion started by: r7p
4 Replies

8. Shell Programming and Scripting

Display Line numbers

Hi all experts, I am getting error in my shell scripts and i want to find out which lines is in errors. How i can display the line numbers . Is it NU command? Please give me some suggestions. (4 Replies)
Discussion started by: ma466
4 Replies

9. Shell Programming and Scripting

Display mutiple line in single line

Hi All, I had a file called Input.txt, i need to group up in a single line as 1=ttt and the no of lines may vary bewteen the 1=ttt cat Input.txt 1=ttt,2=xxxxxx, 3=4545 44545, 4=66667 7777, 5=77723 1=ttt, 2=xxxxxx, 3=34436 66 3545, 4=66666, 5=ffffff, 6=uuuuuuu 1=ttt, 2=xxxxxx,... (4 Replies)
Discussion started by: manosubsulo
4 Replies

10. Shell Programming and Scripting

Display a particular line from a file

Hi, What is the command i can use iof i want to display a particular line from a file, i have the line number with me. (5 Replies)
Discussion started by: Rohini Vijay
5 Replies
Login or Register to Ask a Question