Page 1 of 1

Prefix, Replace duplicate text in a line

Posted: 06 Nov 2010 00:37
by rwalker1072
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!

Re: Prefix, Replace duplicate text in a line

Posted: 08 Nov 2010 20:25
by amel27
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