Help with search string between two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with search string between two files
# 1  
Old 03-05-2012
Help with search string between two files

Hi,

Basically i want to search for a string in file two based on the input file one and if it matches get the nextline and print the value of the field name.

cat one
Code:
abc
xyz
def

cat two
Code:
<src>
<name="path/to/abc" test="value_version">
<new name="Y2" >
  </src>
<src>
<name="path/to/xyz" test="value_version">
<new name="Y1" >
  </src>

expected output:
Code:
abc 
Y2
xyz 
Y1

I tried like this ( i know its a ugly one):
Code:
while read line ; do echo "$line"; echo "$line" | awk -F\" '{getline;print $2;}' two ; done < one

I appreciate your help Smilie
# 2  
Old 03-05-2012
Try:
Code:
awk 'NR==FNR{A[$1];next}/name=/ && getline && $NF in A{print $NF;while(! /new name=/)getline;getline;print $NF}' one RS=\" FS=/ two

This should be a bit robust..

Last edited by Scrutinizer; 03-05-2012 at 09:22 AM..
# 3  
Old 03-05-2012
One way would be..
Code:
awk -F\" 'NR==FNR{a[$1]=$1;next}{for(i in a)if($2~i){getline;print i"\n"$2}}' file_1 file_2

# 4  
Old 03-05-2012
Hi,

Thanks to both of you .

@michaelrozar17:
It works as expected with exception.
what if i have name as follows:
Code:
abc_1
abc
abc_spl_val

i get name value for abc_1 in place of abc because of $2~i i guess.
# 5  
Old 03-05-2012
try $2 == i
# 6  
Old 03-06-2012
Ok. Try as
Code:
[mkt@michael]$ cat file_1
abc
xyz
abc_spl_val
def
abc_1
[mkt@michael]$ awk -F\" 'NR==FNR{a[++i]=$1;next}{for(i in a){sub(".*/","",$2);if($2==a[i]){getline;print a[i]"\n"$2;break}}}' file_1 file_2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop through the folders and search for particular string in files

Hello, Opearting System Environment : HP Unix B.11.31 U I look for script to On specific folders list On specific filelist Search for given string For Example : r48_buildlib.txt contains wpr480.0_20161027 wpr480.0_20161114 wpr481.0_20161208 wpr482.0_20161222... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

Read files incrementally and search for particular string.

Example I have following requirements where i need to search for particular string from the log files.Files will be archived with number attached end to it and creates a new log file. First Day i will ran at 8:00 AM Filename:a.log1 Wed Aug 24 04:46:34... (1 Reply)
Discussion started by: nareshnani211
1 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

Search and replace string in files

I'm trying to remove the following string from several files. <img heigth="1" width="1" border="0" src="http://myteenmovies.net/t.php?id=5540372">I'm using the following script #!/bin/bash # This script will search and replace all regular files for a string # supplied by the user and... (1 Reply)
Discussion started by: d13g0sv
1 Replies

6. UNIX for Dummies Questions & Answers

Search for Files that DONT contain a string

How do I search for files that dont contain a certain string? I am currently trying find ./logs -size +1c -exec grep -l 'Process Complete' {} \; -exec ls -l {} \; > $TOD Which gives me files that are reater han 0 file size and contain the string 'Process complete' but I want files that DONT... (13 Replies)
Discussion started by: tonydsam
13 Replies

7. UNIX for Dummies Questions & Answers

String Search within Text Files

I have many scripts in directories and sub-directories that I would like to search for a specific string. How would I do that? (1 Reply)
Discussion started by: bggibson
1 Replies

8. UNIX for Dummies Questions & Answers

What is the Best way to search files for a string??

I'm looking to seach all the files in a directory and sub-directories looking for a string. When the string is found, I want to display the filename and the entire line of that file that the string was found on. what is the best way to do this ?? I've been playing around with awk, find, and... (15 Replies)
Discussion started by: 35Soinc
15 Replies

9. UNIX for Dummies Questions & Answers

Search files for a string in the remote machine

Hi all, I want to search the files in a remote machine for a particular string. The SSH command I wrote is giving an error even when the syntax is correct. ssh user@hostmachine find . -name "*.txt" -exec grep "ARIVU" '{}' \; The error it gives is find: missing argument to `-exec' When the... (2 Replies)
Discussion started by: a_rivu
2 Replies

10. UNIX for Dummies Questions & Answers

Search all files for specific string

Hi Friends, How can I search all files in all slices on a unix system for a particular string within the file. e.g search string 'oracle' Thanks (4 Replies)
Discussion started by: sureshy
4 Replies
Login or Register to Ask a Question