1834814 Members
2805 Online
110070 Solutions
New Discussion

Re: text processing

 
SOLVED
Go to solution
T G Manikandan
Honored Contributor

text processing

File 1:

NWDEMSGTYPESEQ
NWDEMSGCTRYTIME

File 2:
NWDE-MSG-TYPE-SEQ
NWDE-MSG-CTRY-TIME

Now I need a output like

echo else if (name.equalsIgnoreCase(""))
{
setNWDEMSGTYPESEQ(strBuff);
}

I would like to loop to get a output somewhat like

echo else if (name.equalsIgnoreCase("NWDE-MSG-TYPE-SEQ"))
{
set(strBuff);
}


quick 10 points!!
11 REPLIES 11
T G Manikandan
Honored Contributor

Re: text processing

like

echo else if (name.equalsIgnoreCase("NWDE-MSG-TYPE-SEQ"))
{
setNWDEMSGTYPESEQ(strBuff);
}
Muthukumar_5
Honored Contributor

Re: text processing

We can print it with echo or printf on command line to do simply ?! as like

echo "echo else if(name.equalsIgnoreCase(\"$(tail -1 file2)\"))\n{\nset$(head -1 file1)(strBuff);\n}"

printf "echo else if(name.equalsIgnoreCase(\"$(tail -1 file2)\"))\n{\nset$(head -1 file1)(strBuff);\n}"

Delemit " character with \

Easy to suggest when don't know about the problem!
T G Manikandan
Honored Contributor

Re: text processing

I have file with 10000 rows similarly
H.Merijn Brand (procura
Honored Contributor

Re: text processing

I don't recognize that scripting language. Is it Python?

If you just have plain text files, and rephrase the question, you might have more luck here with people knowing to do what you want in awk/sed/perl/sh

Enjoy, Have FUN! H.Merijn [ who honoustly has no idea what to answer to the original question ]
Enjoy, Have FUN! H.Merijn
Fred Ruffet
Honored Contributor

Re: text processing

Try this (if I understand the question, it might work) :

$>perl
$fd1="file1";
$fd2="file2";
open fd1;
open fd2;
while () {
$File1Line=$_;
;
$File2Line=$_;
$File1Line=~ s/\n//g;
$File2Line=~ s/\n//g;
print "echo else if (name.equalsIgnoreCase(\"$File2Line\"))\n";
print "{\n";
print "set$File1Line(strBuff);\n";
print "}\n";
}

It outputs this from your original files :

echo else if (name.equalsIgnoreCase("NWDEMSGTYPESEQ"))
{
setNWDEMSGTYPESEQ(strBuff);
}
echo else if (name.equalsIgnoreCase("NWDEMSGCTRYTIME"))
{
setNWDEMSGCTRYTIME(strBuff);
}

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Muthukumar_5
Honored Contributor

Re: text processing

I have confused of multiple output's of set and setNWDEMSGTYPESEQ.

It is as simple as,

awk '{ print "else if(name.equalsIgnoreCase(\""$0"))\n{\nsetNWDEMSGTYPESEQ(strBuff);" }'

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: text processing

I missed out } at the end,

modified as,

awk '{ print "else if(name.equalsIgnoreCase(\""$0"))\n{\nsetNWDEMSGTYPESEQ(strBuff);\n}" }' file1

Output:
=======
else if(name.equalsIgnoreCase("NWDEMSGTYPESEQ))
{
setNWDEMSGTYPESEQ(strBuff);
}
else if(name.equalsIgnoreCase("NWDEMSGCTRYTIME))
{
setNWDEMSGTYPESEQ(strBuff);
}
Easy to suggest when don't know about the problem!
Fred Ruffet
Honored Contributor

Re: text processing

Output for my script is not good cause I put the same content in the 2 files. Try it anyway :)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Rodney Hills
Honored Contributor
Solution

Re: text processing

Here is a one liner. By using "pr" to merge the 2 files and "perl" to reformat, I think this is what you are looking for-

pr -mt file1 file2 | perl -n -e 'chomp; ($a,$b)=split "\\s+"; print "echo else if (name.equalsIgnoreCase(\"${b}\"))\n{\nset$
{a}(strBuff);\n}\n"'

HTH

-- Rod Hills
There be dragons...
Muthukumar_5
Honored Contributor

Re: text processing

Another try with perl and sed as,

Perl:
perl -p -e 's/[^.]/else if(name.equalsIgnoreCase(\"/;s/$/\"))\n{\nsetNWDEMSGTYPESEQ(strBuff);\n}/' file*

To put the modified contents in the same file then,
perl -p -i -e 's/[^.]/else if(name.equalsIgnoreCase(\"/;s/$/\"))\n{\nsetNWDEMSGTYPESEQ(strBuff);\n}/' file*

Using sed:
sed -e 's/[^.]/else if(name.equalsIgnoreCase(\"/;s/$/\"))\
{\
setNWDEMSGTYPESEQ(strBuff);\
}/p' file*

Regards
Muthu

Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: text processing


So you are trying to generate a long shell script driven by two list of words that are garantueed to remain in sync?
If the first file always a simple transformation of the second by removing all dashes? In that case I'd just use the second file with a 'one liner' like:

perl -pe 'chomp;$x=$_;s/-//g;$y=$_;$_="echo else if name.equalsIgnoreCase(\"$x\")\n{\n\set$y(strBuff);\n}\n"' file_2

(save word in x, remove dashes; save new word in y, create print buffer, let perl do the loop - read - print. )



If you need the two files, then I like the 'pr' solution as long as they remain in sync. If not you'll just need a doubel read loop. Some variation on the following awk 'one liner':

awk '{getline y < "file_1";print "echo else if name.equalsIgnoreCase(\"" $1 "\")\n{\nset" y "(strBuff);\n}\n"}' file_2

Cheers,
Hein.