Search and print in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and print in Unix
# 1  
Old 05-16-2012
Search and print in Unix

Code:
Hi All
I have two files name file1 and file2

The date present in file1 is
aaaa
ssss
dddd

ffff
qqqq
wwww

eeee

and the data present in file2 is 
aaaa 1 2 3 4
ssss 3 5 6 7
dddd 1 2
qqqq 1 6 y 9 


I want to compare two files file1 and file2
The data present in file1 should be searched in file2 and if found it should be dispalyed

The output file wanted is

aaaa 1 2 3 4
ssss 3 5 6 7
dddd 1 2

ffff
qqqq 1 6 y 9 
wwww

eeee

# 2  
Old 05-16-2012
Does your system have the "join" command? If not, there is a fairly simple solution with awk.
# 3  
Old 05-16-2012
No Join working
# 4  
Old 05-16-2012
Quote:
The output file wanted is

aaaa 1 2 3 4
ssss 3 5 6 7
dddd 1 2

ffff
qqqq 1 6 y 9
wwww

eeee
Please check your example thoroughly. Where do the blank lines and the ffff, eeee and wwww lines come from ?

Bearing in mind you previous history of abstract posts, please post what Operating System and version you are running, and what Shell or other language you are using today.

Please post your attempt at the solution.
# 5  
Old 05-16-2012
The Blank lines where present should be consider because it is part of data
My Operating sysyem is ubuntu 12.10
And using bash shell
# 6  
Old 05-16-2012
Quote:
The data present in file1 should be searched in file2 and if found it should be dispalyed
In that case, I can't see the rule to get from input data to output data.
Please show your attempt . It might tell us what you are trying to achieve. The join command has been suggested, but it will not deal with the blank lines because they make the input file non-sorted.
# 7  
Old 05-16-2012
Code:
awk -F" ", 'NR==FNR{a[$1]++;next} (a[$1])' file2 file1

and also
trying to do
Code:
cat file2 | while read line
do
#echo $line
grep -c $line file1
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search file and print everything except multiple search terms

I'm trying to find a way to search a range of similar words in a file. I tried using sed but can't get it right:sed 's/\(ca01\)*//'It only removes "ca01" but leaves the rest of the word. I still want the rest of the information on the lines just not these specific words listed below. Any... (3 Replies)
Discussion started by: seekryts15
3 Replies

2. Shell Programming and Scripting

Search between two search strings and print the value

Based on the forums i have tried with grep command but i am unable to get the required output. search this value /*------ If that is found then search for temp_vul and print and also search until /*------- and print new_vul Input file contains: ... (5 Replies)
Discussion started by: onesuri
5 Replies

3. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

4. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

5. Shell Programming and Scripting

pattern search and print using sh

Hi All, Have a file contains multiple line with the following file.txt insert: akdkas job:ksdjf command: aldfasdf asdfsdfa asdfas.sh machine: asdfa need to grep for insert and machine and print only "akdkas,asdfa" cat file.txt | egrep "insert|machine" | awk -F: '{print $2}' output ... (5 Replies)
Discussion started by: uniqme
5 Replies

6. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

7. Shell Programming and Scripting

awk search and print

I have a script where I need to use awk, go through some output, which is stored in a variable, and find a string Xms and Xmx and print the results, including the rest of that string. Example of string: ... (3 Replies)
Discussion started by: cbo0485
3 Replies

8. Shell Programming and Scripting

Perl: Search and print help

Hi all, I need a bit of help with a perl script, I have a file containing lines that look like: M1 (Agnd Agnd ibias_gnP Agnd) nch l=250.0n w=10u m=1 ad=2.5e-12 \ as=2.5e-12 pd=20.5u ps=20.5u nrd=0.025 nrs=0.025 sa=2.5e-07 \ sb=2.5e-07 M21 (Agnd VSSabc Agnd Agnd) nch... (3 Replies)
Discussion started by: Crypto
3 Replies

9. HP-UX

Print Problem in UNIX. Need to know the option to specify the print paper size

Hi, Could any one please let me know what is the option available in UNIX to print by specifying the paper size? We are using Unix11i. I could n't see any option specified in the 'lp' command to print the report by specifying the size of the paper. It would be of great help to me, if... (1 Reply)
Discussion started by: ukarthik
1 Replies

10. UNIX for Dummies Questions & Answers

Unix find command to print directory and search string

Hi i need to print pathname in which the string present using 'find' command sample output like this Pathname String to be searched ---------- -------------------- /usr/test/myfile get /opt/test/somefile get Thanks in... (4 Replies)
Discussion started by: princein
4 Replies
Login or Register to Ask a Question