Compare strings and deleting strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
flexbuffchest
Posts: 6
Joined: 26 Jan 2011 16:19

Compare strings and deleting strings

#1 Post by flexbuffchest » 05 Apr 2011 07:16

Hey guys,

I've tried over the past few days to come up with a solution to this and I haven't found a way to do it. What I am trying to do is

1) Parse through one file and cut out any strings that are exactly the same and have them redirected to a new file. So, for example, if I had three "test" in the file it would strip away all three of them and redirect to a new file.
2) Compare two files and delete any strings that are exactly the same.

I feel bad that I couldn't figure this out and would love some help. Maybe using batch files isn't the right job for this, but I need to complete this in the next couple of days. It's either script this or manually go through 360 values :oops:

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Compare strings and deleting strings

#2 Post by ghostmachine4 » 05 Apr 2011 07:20

flexbuffchest wrote:1) Parse through one file and cut out any strings that are exactly the same and have them redirected to a new file. So, for example, if I had three "test" in the file it would strip away all three of them and redirect to a new file.

An example of your input and your expected output speaks more than a thousand words you could write down

2) Compare two files and delete any strings that are exactly the same.

same. Show examples.

Maybe using batch files isn't the right job for this,

yes, you are right. Batch is not for this job. You need a tool with good string/text processing capabilities.

flexbuffchest
Posts: 6
Joined: 26 Jan 2011 16:19

Re: Compare strings and deleting strings

#3 Post by flexbuffchest » 05 Apr 2011 07:45

ghostmachine4 wrote:
flexbuffchest wrote:1) Parse through one file and cut out any strings that are exactly the same and have them redirected to a new file. So, for example, if I had three "test" in the file it would strip away all three of them and redirect to a new file.

An example of your input and your expected output speaks more than a thousand words you could write down

2) Compare two files and delete any strings that are exactly the same.

same. Show examples.

Maybe using batch files isn't the right job for this,

yes, you are right. Batch is not for this job. You need a tool with good string/text processing capabilities.

Alright.
1) Input:
test
test
1234
nothing
apple

In this example I need something that would strip out any matching strings. In this case the output to the new file would be

test
test

while the original file would be

1234
nothing
apple

2) File 1:

1234
nothing
apple

File 2:

4321
nothing
orange

In this example I need something that would compare file 1 to file 2 and delete any matching string from file 2. In this case File 1 would remain the same,

1234
nothing
apple

while file 2 would now look like this

4321
orange


BTW, thanks for helping me out with my other problem a few days ago. I am only a begineer when it comes to writting scripts of any kind. Learning to script in Gawk though is like asking me to talk in a different language at this point in time. It's completely different from what I am used to.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Compare strings and deleting strings

#4 Post by ghostmachine4 » 05 Apr 2011 08:10

flexbuffchest wrote:Alright.
1) Input:
test
test
1234
nothing
apple

In this example I need something that would strip out any matching strings. In this case the output to the new file would be

test
test

while the original file would be

1234
nothing
apple



So here it goes, with gawk for windows..just one line of course

Code: Select all

C:\work>type file
test
test
1234
nothing
apple
test

C:\work>gawk "/test/{print $0 > \"new.file\"} !/test/ " file > retain.file

C:\work>more retain.file
1234
nothing
apple

C:\work>more new.file
test
test
test



2) File 1:

1234
nothing
apple

File 2:

4321
nothing
orange

In this example I need something that would compare file 1 to file 2 and delete any matching string from file 2. In this case File 1 would remain the same,

1234
nothing
apple

while file 2 would now look like this

4321
orange



Code: Select all

C:\work>gawk "FNR==NR{a[$1];next} (!($1 in a)) " file1 file2
4321
orange




BTW, thanks for helping me out with my other problem a few days ago. I am only a begineer when it comes to writting scripts of any kind. Learning to script in Gawk though is like asking me to talk in a different language at this point in time. It's completely different from what I am used to.


Nobody is born to be good at something from the start. All it takes is to learn and practice it.

Post Reply