Search and print in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and print in Unix
# 8  
Old 05-16-2012
A Shell approach. The big issue is the blank lines:

Code:
(
cat file1|while read line1
do
        # Avoid feeding blank to grep
        if [ "${line1}""X" = "X" ]
        then
                echo "${line1}"
                continue
        fi
        # Does key exist in file2 ? 
        found=""
        found=$(grep \^"${line1}" file2)
        if [ ! "${found}""X" = "X" ]
        then
                echo "${found}"
        else
                echo "${line1}"
        fi
done
) > file3

./scriptname
cat file3
aaaa 1 2 3 4
ssss 3 5 6 7
dddd 1 2

ffff
qqqq 1 6 y 9 
wwww

eeee



Quote:
The data present in file1 should be searched in file2 and if found it should be dispalyed
This does not describe the process correctly.
Code:
Update file1 with the data from file2 using each file1 record as the matching key and output the results to file3.


Last edited by methyl; 05-16-2012 at 09:25 AM..
# 9  
Old 05-16-2012
Can you please explain the line5


Code:
if [ "${line1}""X" = "X" ]

# 10  
Old 05-16-2012
It's just avoiding the Shell syntax error if the input is blank by appending a string (in this case the character "X") to the input string.
There are other ways of achieving the same effect, but this technique works in every Bourne-type Shell.

Last edited by methyl; 05-16-2012 at 09:48 AM..
# 11  
Old 05-16-2012
I think you need an awk solution like this:
Code:
awk 'NR==FNR { a[$1]=$0;next} { if (a[$1]) print a[$1]; else print $0; }' file2 file1


Last edited by otheus; 05-16-2012 at 02:38 PM.. Reason: syntax errors fixed
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