Skip to main content
fixed with OP sample input
Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107

you sould use awk to parse usernames.csv (where field 2 and 4 are different), and generate sed file.

 tr -d \" username.csv |
 awk -F\; '$2 != $4 { printf "s/^(.*ri:username=%c)%s(%c.*)$/\\1%s\\2/g\n",34,$2,34,$4 ; }' |
 sed -i -f - confluence/entities_converted.xml

some trick

  • use printf "..%c..",34 to generate quotes.
  • you can skip the sed line in debug part to make sure all sed instructions are properly generated.
  • do you need /g in substitution ?

on my test file

;foo;;foo;;
;fubar;;mr X;;
;bar;;bistro;;
    "Full name";"Username";"Email";"New username"
    "Sune Mølgaard";"sune.molgaard";"[email protected]";"smo"

this generate

s/^(.*ri:username=")fubar(".*)$/\1mr X\2/g
s/^(.*ri:username=")bar(".*)$/\1bistro\2/g
s/^(.*ri:username=")Username(".*)$/\1New username\2/g
s/^(.*ri:username=")sune.molgaard(".*)$/\1smo\2/g

I don't bother with quotesto remove Username's line, if not found, no substitute.

you sould use awk to parse usernames.csv (where field 2 and 4 are different), and generate sed file.

 tr -d \" username.csv |
 awk -F\; '$2 != $4 { printf "s/^(.*ri:username=%c)%s(%c.*)$/\\1%s\\2/g\n",34,$2,34,$4 ; }' |
 sed -i -f - confluence/entities_converted.xml

some trick

  • use printf "..%c..",34 to generate quotes.
  • you can skip the sed line in debug part to make sure all sed instructions are properly generated.
  • do you need /g in substitution ?

on my test file

;foo;;foo;;
;fubar;;mr X;;
;bar;;bistro;;

this generate

s/^(.*ri:username=")fubar(".*)$/\1mr X\2/g
s/^(.*ri:username=")bar(".*)$/\1bistro\2/g

I don't bother with quotes.

you sould use awk to parse usernames.csv (where field 2 and 4 are different), and generate sed file.

 tr -d \" username.csv |
 awk -F\; '$2 != $4 { printf "s/^(.*ri:username=%c)%s(%c.*)$/\\1%s\\2/g\n",34,$2,34,$4 ; }' |
 sed -i -f - confluence/entities_converted.xml

some trick

  • use printf "..%c..",34 to generate quotes.
  • you can skip the sed line in debug part to make sure all sed instructions are properly generated.
  • do you need /g in substitution ?

on my test file

;foo;;foo;;
;fubar;;mr X;;
;bar;;bistro;;
    "Full name";"Username";"Email";"New username"
    "Sune Mølgaard";"sune.molgaard";"[email protected]";"smo"

this generate

s/^(.*ri:username=")fubar(".*)$/\1mr X\2/g
s/^(.*ri:username=")bar(".*)$/\1bistro\2/g
s/^(.*ri:username=")Username(".*)$/\1New username\2/g
s/^(.*ri:username=")sune.molgaard(".*)$/\1smo\2/g

don't bother to remove Username's line, if not found, no substitute.

Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107

you sould use awk to parse usernames.csv (where field 2 and 4 are different), and generate sed file.

 tr -d \" username.csv |
 awk -F\; '$2 != $4 { printf "s/^(.*ri:username=%c)%s(%c.*)$/\\1%s\\2/g\n",34,$2,34,$4 ; }' |
 sed -i -f - confluence/entities_converted.xml

some trick

  • use printf "..%c..",34 to generate quotes.
  • you can skip the sed line in debug part to make sure all sed instructions are properly generated.
  • do you need /g in substitution ?

on my test file

;foo;;foo;;
;fubar;;mr X;;
;bar;;bistro;;

this generate

s/^(.*ri:username=")fubar(".*)$/\1mr X\2/g
s/^(.*ri:username=")bar(".*)$/\1bistro\2/g

I don't bother with quotes.