Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing some words in a file with words from another file.

Status
Not open for further replies.

geirendre

Vendor
Aug 13, 2001
603
NO
Hi, need some pointers on a short AWK-script please.

I have 2 files, one large file looking something like this:
# File header bla bla
# more headertext here
firstword * 5
secondword *
anotherword 0 15
hello
and *
so-on *


The second file looks like this:
anewword
another-word
goodbye
test

I need to put the words from the second file who is not in the
first file into the file, and replace those who exist in
the file but are whitout the - dash.
So "anotherword 0 15" should replace "another-word 0 15",
and anewword, goodbye and test should be appended to the file.
I not able to get both the replaced lines, added lines and
unchanged lines together into a new file.
I only get the changed/added lines... help please ;-)

BTW the header should not be touched.
 
Try this:

Code:
awk '
        BEGIN { i=0 }
        # fill up the second array, indexed by the names
        # without dashes
        NR==FNR {
                dashless=$1
                gsub("-","",dashless)
                second[dashless]=$1
                next
        }
        # print any comment lines
        /^#/ { print ; next }
        # if the index exists in the second file
        $1 in second {
                found=$1
                # print the version with dashes
                $1=second[$1]
                # delete it from the second array
                delete second[found]
                print
        }
        END {
                # second array now only contains entries
                # that we did not find in the first file
                # so print them all
                for (found in second) print found
        }
' second first

Annihilannic.
 
Thank's Annihilannic, almost there :)
But, I'm afraid I mistyped my question...
->So "anotherword 0 15" should replace "another-word 0 15"<-
should have read:
->So "anotherword 0 15" should be replaced with "another-word 0 15"

The words in the second file are new, and shall replace the correspondig word in first file.
Those words that are in second file but not in first must be added to the first file.
 
Have you tried to figure out what changes would be required to my solution to make it do what you want? It shouldn't be too difficult.

Annihilannic.
 
Nope, Have'nt got the time yet to look into the code.
Hope I can spend some time with it in the Easter,
need to learning more awk :)
I'm totaly blank on the use of arrays.
Sorry if I sounded like wanting you to solve the whole ting.
 
hmmm...... this starts sounding very subliminal.
Not that I'm implying anything or putting words in your mouth, but...
Oh, wait.... I guess I am! ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Annihilannic,
no, not really. it seems like geirendre cannot wait to do 'man awk' and/or to do some Googlin' around and/or to look in the FAQs OR what's even more impressive try a couple of things on his/her/it own.

It really starts sounding like he/she/it is trying to stop us and convince us all NOT to lend a helping hand in 'solving the whole thing'.

That's what it's sounding to me.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Actually... geirendre, I think my solution already does what you wanted anyway?

The output I get is:

[tt]# File header bla bla
# more headertext here
another-word 0 15
newword
goodbye
test[/tt]

Isn't that correct.... i.e. anotherword has been replaced by another-word?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top