Prefix, Replace duplicate text in a line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rwalker1072
Posts: 1
Joined: 06 Nov 2010 00:09

Prefix, Replace duplicate text in a line

#1 Post by rwalker1072 » 06 Nov 2010 00:37

Hi,

I'm looking to perform multiple operations on a text file. At a minimum, I want to know how to replace duplicate text within a row. Below is source.txt file contents.

"117981" "YOC AG"<tab>
"118127" "118127"<tab>
"702337" "Carlson Marketing Worldwide, I"<tab>

Ideally, the output file target.txt will:

1.) Replace the single space between column one and two.
2.) Remove the trailing tab character in each row.
3.) If the second column is a duplicate of the first column, it should be replace with empty double quotes.
4.) Add a prefix text "Col1", text to each row.

"Col1","117981","YOC AG"
"Col1","118127",""
"Col1","702337","Carlson Marketing Worldwide, I"

Looking for a purely dos batch based solution for this.

Thanks!

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Prefix, Replace duplicate text in a line

#2 Post by amel27 » 08 Nov 2010 20:25

by AWK for Windows:

Code: Select all

>result.txt awk -F\" -v "C=," "{if ($2==$4) $4 = \"\";print FS\"Col1\"FS C FS$2FS C FS$4FS}" source.txt

Post Reply