LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-12-2012, 11:26 PM   #1
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Rep: Reputation: 0
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.

Last edited by sharp859; 04-12-2012 at 11:28 PM. Reason: Clean up
 
Old 04-13-2012, 01:12 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Must have my thick ears on ... could you try explaining further as I am not following the current logic?
 
Old 04-13-2012, 03:28 AM   #3
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
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.

Last edited by sharp859; 04-13-2012 at 03:38 AM.
 
Old 04-13-2012, 04:11 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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
 
Old 04-14-2012, 12:39 AM   #5
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
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.
 
Old 04-14-2012, 01:18 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Right . . . how is this different to the example I have suggested?
 
Old 04-14-2012, 02:04 AM   #7
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
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.
 
Old 04-14-2012, 12:23 PM   #8
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
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.
 
Old 04-15-2012, 03:09 AM   #9
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
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,
 
Old 04-15-2012, 03:26 AM   #10
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 583

Rep: Reputation: 129Reputation: 129
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
 
Old 04-15-2012, 11:34 AM   #11
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
@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.
 
Old 04-16-2012, 06:00 PM   #12
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 583

Rep: Reputation: 129Reputation: 129
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.

Last edited by whizje; 04-16-2012 at 06:02 PM.
 
Old 04-16-2012, 06:07 PM   #13
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 583

Rep: Reputation: 129Reputation: 129
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.

Last edited by whizje; 04-16-2012 at 06:14 PM.
 
  


Reply

Tags
grep, perl, shell script, unix


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 05:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration