Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 10:28 PM.
Reason: Clean up
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.
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
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.
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...
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.
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, 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.
#!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.