LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regarding Shell script (http://www.linuxquestions.org/questions/linux-newbie-8/regarding-shell-script-939565/)

sharp859 04-12-2012 10:26 PM

Regarding Shell script
 
Dear Friends,

Could some one help me to resolve this using shell script may be grep command

For example, one line in the file3.txt looks as below

<path="soft/xxxxx/STRING1/yyyy" name="x/mmm/soft/xxxxx/STRING1/yyyy"/>

You would need to match this line or String1 to one of the 2 files,
file1.txt: contains below
<name="x/mmm/soft/xxxxx/STRING1/yyyy" path="soft/xxxxx/STRING1/yyyy" revision="123456789"/>
And file2.txt:

file2.txt:
<name="x/mmm/soft/xxxxx/STRING2/yyyy" path="soft/xxxxx/STRING1/yyyy" revision="0987654321"/>
So now I need to get the revision printed 123456789 as STRING1 is available in file1.txt.

Could any body help me.

grail 04-13-2012 12:12 AM

Must have my thick ears on ... could you try explaining further as I am not following the current logic?

sharp859 04-13-2012 02:28 AM

I put it in this way,The first file contains STRING1 only instead of STRING2. The 2nd file contains STRING2 only . The 3rd file contains some of both (STRING1 and STRING2) so I need the lines from each file that match a line in the 3rd file, I need to print whole line, then I know what to grep and use sed to tailor it.:)

grail 04-13-2012 03:11 AM

So I think I have it, we are reading each line from file 3 and then grepping / sedding files 1 and 2 to see if the line exists?

If above assumption is correct, would look at something like:
Code:

while read -r line
do
    if grep -q "$line" file1
    then
        <do stuff for match in file1>
    elif grep -q "$line" file2
    then
        <do stuff for match in file2>
    fi
done<file3


sharp859 04-13-2012 11:39 PM

Okay, I say this way, file3 has lot of lines with a keyword which may be in file1 or file2, if found in file2/file3 print whole line and then I need to append @ to file3 possibly end of the string in file3, ?. File may be in different directory.

grail 04-14-2012 12:18 AM

Right . . . how is this different to the example I have suggested?

yoK0 04-14-2012 01:04 AM

If files may be in different directory you may want to provide them
as a parameters to your script. Like:
./myScript /home/James/Misc/some_file /home/James/ZIP/some_other_file

First given parameter is accessed by variable $1 in your script, second $2, and so on...
Code:

file1=$1
file2=$2

and then what grail wrote.

sharp859 04-14-2012 11:23 AM

What do you mean by do stuff for match?,, there i got issue. :( could you give me a full script, then I add my logic as I need to do some more stuff, for me that was the question.. how you use regular expression.. 100s of lines will be common , it should automatically search for particular pattern in 3rd file and match with other and print the whole lines of other2.

sharp859 04-15-2012 02:09 AM

Here is the correct input of file.
file1:sample1.xml and its content <project name="a/vgi/hard/internal/STRING1/csr" path="software/internal/STRING1/ckr" revision="9zzzzzzdddd667778cc21905857589a"/>
file2:sample2.xml and its content <project name="j/vgi/hard/internal/STRING2/ckr" path="software/internal/STRING1/ckr" revision="xxxxyyy122yyycccmmmmccccccc"/>
The sample2,xml contains all STRING2 projects names instead of STRING1. The sample1.xml contains STRING1 projects.
The 3rd sample.xml contains some of both (STRING1 and STRING2) so I need the lines from each xml that match a line in the 3rd manifest.
For example, one of line in the 3rd manifest is <project path="software/internal/STRING1/csr" name="j/vgi/hard/internal/STRING2/ckr"/> and you may have many such line and it might be in file1 or file2
You would need to match this project to one of the 2 sample.xml:
Script should traverse to each line in 3rd file and if found a match in sample1/sample2 (this is common "j/vgi/hard/internal/STRING2/ckr", in file3 and sample2.xml) it is in sample2.xml and shouldprint the whole line and append in file3.xml,

whizje 04-15-2012 02:26 AM

This compares file3 with file1 and file2 and prints the lines which match.
Code:

#!/bin/sh
while read -r line
do
    grep "$line" file1
    grep "$line" file2
done<file3


sharp859 04-15-2012 10:34 AM

@Whizje, Does not work, I tried to execute, not doing anything :(. instead of file1 I put my file1.date, it does not print any match., how to run,? Could you please help me?, It should read 3rd file each line and look for a string if it is file1 or file2 print and append it end of each line.

whizje 04-16-2012 05:00 PM

try this
Code:

#!/bin/bash
grep -o 'path..[^\"]*[\"]\|name..[^\"]*[\"]' file3 | while read line
do
    grep $line file1
    grep $line file2 
done

Grep reads file3 and extracts the path part and the name part and the greps in the while loop checks if the path part or the name part exists in file 1 or 2.

whizje 04-16-2012 05:07 PM

I didn't test it but it seems you can in the while loop use one line
Code:

#!/bin/bash
grep -o 'path..[^\"]*[\"]\|name..[^\"]*[\"]' file3 | while read line
do
    grep $line file1 file2
done

I know it's not complete but if this is what you want then we can finish it.


All times are GMT -5. The time now is 04:47 AM.