text file contents merge and subtraction

text file contents merge and subtraction

Post by jbkeu » Sun, 24 Aug 2003 12:13:02



Hi everyone.  Once again, I ask you experts for help.

I have three files: config.orig, config.add config.del

config.orig has its contents like
configkey1 on
configkey2 on
configkey3 off
configkey4 off
configkey5 default

config.add has its contents like
configkey1 default
configkey6 default
configkey7 default

config.del has its contents like
configkey2
configkey3
configkey7

the desired outcome needs to be the same config.orig filename having its
content like
configkey1 default    //overrided by config.add
configkey4 off          //left alone
configkey5 default    //left alone
configkey6 default    //added by config.add

I am to perform two tasks here
1) merge config.orig and config.add.
    * here like configkey1 above, content in config.add should override
config.orig
2) subtract config.del from the outcome of 1)
    * but config.del does not include the configkey value

How should I go about this with shell script?  Please help.

 
 
 

text file contents merge and subtraction

Post by Christophe Le Ga » Sun, 24 Aug 2003 20:43:33



> the desired outcome needs to be the same config.orig filename having its
> content like
> configkey1 default    //overrided by config.add
> configkey4 off          //left alone
> configkey5 default    //left alone
> configkey6 default    //added by config.add

cat config.add config.orig | sort -s -k1,1 -u | \
sed -e "$(cat config.del | sed 's#\(.*\)#/\1/d ;#')"

Not very beautiful, but short and quite efficient compared to "more natural"
n^2 shell technique.

explanations :

<<cat config.add config.org>> concatenates the orig file and the
  new values files (with config.add first).

<<sort -k1,1>> sorts using only the first field as a key
<<sort -k1,1 -s>> keeps the result stable (instead of using a "last
       resort sorting"). ie if same key, the order doesn't change.
       ie if the same key is present in both config.orig and config.add
       the the first one in `cat config.add config.key` ie the one
       of config.add is the first one in the result.
<<sort -k1,1 -s -u>> keep only the first key if two key are presents
       => if a key is present in both config.orig and config.add then
       the line from config.add is the only one kept.

<<cat config.del | sed 's#\(.*\)#/\1/d ;#'>> transform the
file config.del in a sequences of <</key/d ;>> strings

<<sed -e "$(previous command)">> apply these /key/d sed commands to the
result of our sort -u.
=> remove the line containing keys in config.del.

Could be improved by replacing /key/d by /^key /d if you suspect that
some keys could also appears in values in others fields.

--
Christophe Le Gal

 
 
 

1. renaming 500+ files based on contents of an existing text file

hello,

this is more of a curiousity question, although it would be nice if it
would be possible.

I have a collection of viruses.  I use f-prot to scan them in order to
find out their correct names. I then rename the existing zip archive to
that of the name of the virus f-prot siad it was infected with(which is
the virus itself).

Problem is i have about 1000 viruses and doing it by hand is tedious.

Is it possible to make a shell script to read the output of the text
file generated by f-prot and then search for the correct zip archive on
the hard drive and have the shell script automatically rename teh zip
file to the name of the virus f-prot found it be infected by?

Here is what the output of f-prot looks like:

C:\ZIP2\VIRII\UNIXLO~1.SH  Infection: Unix/LoveLetter
C:\ZIP2\VIRII\VBSFRE~1.VBS  Infection: VBS/FreeLinks.A
C:\ZIP2\VIRII\NEWVIRII\ASCIIV.ZIP->VIRII/GLITCH/ASCIIV.COM  Infection:
Ascii.613.unknown?
C:\ZIP2\VIRII\NEWVIRII\CEREBR~1.ZIP->cerebrus.zip->CEREBRUS.EXE
Infection: W95/Cerebrus.1482

I'd have the shell script find anything after "Infection:" and make that
the filename for the file listed on that same line.

Cerebrus.zip would be renamed to Cerebus.1482.zip

Is this possible and difficult to accomplish?
This would be a good project i can learn how to do shell scripting on if
it isnt too difficult.

Thanks

2. Iptables PREROUTING Not All Working

3. Looking for Text files comparing and merging utility

4. Resoting MBR for LILO

5. Merging text & decoding files on UNIX

6. New Linux System

7. ** Merging Two Text Files Parallel **

8. XFree 4.2/FreeBSD 4.5 and Dell C400

9. merge text from 3 files?

10. Q: Script to merge together text files

11. Merging huge text files...

12. How to merge text files?

13. Get part of content of a text file.