SOLVED: Text file compare using perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers SOLVED: Text file compare using perl
# 1  
Old 08-19-2010
SOLVED: Text file compare using perl

I have two text file....
One text file contain in this format......
keyvalue.txt
Code:
\SUM\SUM_cam.c  
\SUM\SUM_cam.h  
\SUM\SUM_command.c  
\SUM\SUM_command.h  
\SUM\SUM_dab.c  
\SUM\SUM_dmb.c  
\SUM\SUM_eventHandler.h  
\SUM\SUM_eventHandler_dab.c  
\SUM\SUM_eventHandler_dmb.c  
\SUM\SUM_fap.c  
\SUM\SUM_fap.h  
\SUM\SUM_private.h  
\SUM\SUM_task_dab.c  
\SUM\SUM_task_dmb.c  
\SUM\Makefile  
\SUM\SUM_cam.c

second text file looks like this......
samplesource.txt
Code:
\SUM\SUM_cam.c@@\main\base2ucm_source\600_int\1
\NEW\NEW_calc.c@@\main\base2ucm_source\600_int\1
\NEW\NEW_FAP.c@@\main\base2ucm_source\600_dev_dmb\1
\SIN\COM\SIN_COMconnection.c@@\main\base2ucm_source\600_int\1
\SIN\COM\COMconnection.sm@@\main\base2ucm_source\600_int\1
\SIN\IN\SIN_INdevice.c@@\main\base2ucm_source\600_int\1
\SUM\SUM_cam.c@@\main\base2ucm_source\600_int\1

Now i want to compare each element of key.txt with samplesource.txt.

Ex:
\SUM\SUM_cam.c exist in samplesource.txt.
If it is found then put a (%) symbol before \SUM\SUM_cam.c in key.txt and
cut the element after second @ and put it in key.txt

Mean Now Key.txt should look like this:
Code:
%  \SUM\SUM_cam.c                      main\base2ucm_source\600_int\1
     \SUM\SUM_cam.h  
     \SUM\SUM_command.c  
     \SUM\SUM_command.h  
     \SUM\SUM_dab.c  
     \SUM\SUM_dmb.c  
     \SUM\SUM_eventHandler.h  
     \SUM\SUM_eventHandler_dab.c  
     \SUM\SUM_eventHandler_dmb.c  
     \SUM\SUM_fap.c  
     \SUM\SUM_fap.h  
     \SUM\SUM_private.h  
     \SUM\SUM_task_dab.c  
     \SUM\SUM_task_dmb.c  
     \SUM\Makefile  
     \SUM\SUM_cam.c

NOTE:
samplesource.txt may conatin \SUM\SUM_cam.c so many times.
But we should concentrate our search to the first occurance...........

Hope you got my point guys..................

Last edited by Yogesh Sawant; 08-27-2010 at 09:37 AM..
# 2  
Old 08-20-2010
Code:
awk -F "@" '
BEGIN{s=0}
NR==FNR{a[$1]=$3;b[$1]=0;next}
{
   gsub(/ *$/,"")
   {
      if (a[$1]&&b[$1]==0) {gsub(/^\\/,"",a[$i]);print "%\t" $1 "\t\t\t" a[$1];b[$1]++}
      else print "\t" $1
   }
}' samplesource.txt keyvalue.txt

# 3  
Old 08-20-2010
Quote:
Originally Posted by rdcwayx
Code:
awk -F "@" '
BEGIN{s=0}
NR==FNR{a[$1]=$3;b[$1]=0;next}
{
   gsub(/ *$/,"")
   {
      if (a[$1]&&b[$1]==0) {gsub(/^\\/,"",a[$i]);print "%\t" $1 "\t\t\t" a[$1];b[$1]++}
      else print "\t" $1
   }
}' samplesource.txt keyvalue.txt

I think you can just cast b[$1] for the check:

Code:
awk -F "@" '
BEGIN{s=0}
NR==FNR{a[$1]=$3;b[$1]=1;next}
{
   gsub(/ *$/,"")
   {
      if (b[$1]) {gsub(/^\\/,"",a[$i]);print "%\t" $1 "\t\t\t" a[$1];b[$1]++}
      else print "\t" $1
   }
}' samplesource.txt keyvalue.txt

# 4  
Old 08-20-2010
Quote:
Originally Posted by konsolebox
I think you can just cast b[$1] for the check:

Code:
awk -F "@" '
BEGIN{s=0}
NR==FNR{a[$1]=$3;b[$1]=1;next}
{
   gsub(/ *$/,"")
   {
      if (b[$1]) {gsub(/^\\/,"",a[$i]);print "%\t" $1 "\t\t\t" a[$1];b[$1]++}
      else print "\t" $1
   }
}' samplesource.txt keyvalue.txt

the owner only need the first occurance.

Your updatd code will give two outputs. That's why I need b[$1] to control it.
# 5  
Old 08-20-2010
Hello Guys,

Thanks for your nice suggestion.......
Its working out ......
superbbbbbbbbbbbbbb.....................
# 6  
Old 08-20-2010
Quote:
Originally Posted by rdcwayx
the owner only need the first occurance.
Sorry I didn't read the note about that.
# 7  
Old 08-26-2010
Nice suggestions.
No where a perl solution as per the request(Text file compare using perl) :P
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] My script executes poorly as a text file.

Hello again, I have put together a shell script using sed and some shell commands, and it runs pretty well when I am in terminal, but when I save it as a text file and invoke it through the terminal by typing its path, all I get are errors. Can some one give me some hints as to what I am doing... (13 Replies)
Discussion started by: Paul Walker
13 Replies

2. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

3. Shell Programming and Scripting

[Solved] Replacing line of text while file is closed

Is it possible to replace a line of text within a file while it's closed with a single command or a script? Please show me an example or point me to a webpage that shows an example. The file has this line of text: LoginGraceTime 100 I want to replace it with the following: ... (2 Replies)
Discussion started by: wdg74
2 Replies

4. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

5. Shell Programming and Scripting

[Solved] How to print specific text from a file?

Hi All, I have the below text file from which I have to cut particular section starting from PTR_Security_Rpeorting.cpf to PTR_Security_Reporting_Env93_export. Report Model............: "D:\Cognos_Publishing\tmp.a2R94KLQec"\PTR_Security_Reporting.cpf Report Output Script....:... (4 Replies)
Discussion started by: Vikram_Tanwar12
4 Replies

6. Shell Programming and Scripting

[Solved] Formatting the text file

Hi All, I ahve requirement where I want to put the text file in into proper format. I am wondering how can i achieve that:- Host/Alias Name IP Address Resolved sinuiy01.infra.go2uti.com 10.240.8.158 N sinuid20.devtst.go2uti.com 10.240.8.230 N sinuid21.devtst.go2uti.com... (6 Replies)
Discussion started by: sharsour
6 Replies

7. Shell Programming and Scripting

[Solved] I need help with a text file.

Hello everyone, I need to write a shell script for a file consisting of 3 columns, first column is frequency the second one is power and the last one is number of occurence. I basically need to get the power and the frequency corresponding to the highest number of occurrence number. Below is the... (6 Replies)
Discussion started by: johankor
6 Replies

8. Shell Programming and Scripting

[Solved] perl and grep: get a script to look at more then one file

hi guys i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works how can i get it to look at more then one file, i.e /var/log/message.all * so it looks in old logs as well thanks exit 0 if (isRenderNode(hostname)); my... (4 Replies)
Discussion started by: ab52
4 Replies

9. Shell Programming and Scripting

SOLVED: reading config file in a perl script

Hi! I have a need to do this in Perl. script.pl -config file The script would be doing a wget/LWP on a URL which is defined in the config file. So when I run the script it should return either one of these conditions - 1) OK with exit status 0. Should also print "wget URL" 2)... (6 Replies)
Discussion started by: jacki
6 Replies

10. Shell Programming and Scripting

File Compare in PERL

I need to look at a log file every half hour or so to make sure that some activity is going on there. I thought I would write a quick PERL script that would copy the current log file to the temp directory, compare it to the previous log file (from 30 minutes ago) and exit with an error status if... (1 Reply)
Discussion started by: Cbish68
1 Replies
Login or Register to Ask a Question