Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

You have indicated in a commentindicated in a comment that awk is also allowed. So I'm basing my answer on this.

Assuming your STARTs and ENDs are balanced, if you split the line on either word, you find that you want to remove backslashes and double quotes from all even-numbered fields. To this end:

awk -F 'START|END' '{
                      for(i=2;i<=NF;i+=2){ # For each even-numbered field
                        gsub(/["\\]/,"",$i) # Remove " and \ from it
                        $i="START"$i"END" # Put START and END back around it
                      }
                    }' your_file

This assumes your implementation of awk has the gsub function which I can't vouch for.

As a side note, your sed doesn't work because it's basically saying "apply the substitution to the range of lines that starts with a line matching START and ends with a line matching END".

You have indicated in a comment that awk is also allowed. So I'm basing my answer on this.

Assuming your STARTs and ENDs are balanced, if you split the line on either word, you find that you want to remove backslashes and double quotes from all even-numbered fields. To this end:

awk -F 'START|END' '{
                      for(i=2;i<=NF;i+=2){ # For each even-numbered field
                        gsub(/["\\]/,"",$i) # Remove " and \ from it
                        $i="START"$i"END" # Put START and END back around it
                      }
                    }' your_file

This assumes your implementation of awk has the gsub function which I can't vouch for.

As a side note, your sed doesn't work because it's basically saying "apply the substitution to the range of lines that starts with a line matching START and ends with a line matching END".

You have indicated in a comment that awk is also allowed. So I'm basing my answer on this.

Assuming your STARTs and ENDs are balanced, if you split the line on either word, you find that you want to remove backslashes and double quotes from all even-numbered fields. To this end:

awk -F 'START|END' '{
                      for(i=2;i<=NF;i+=2){ # For each even-numbered field
                        gsub(/["\\]/,"",$i) # Remove " and \ from it
                        $i="START"$i"END" # Put START and END back around it
                      }
                    }' your_file

This assumes your implementation of awk has the gsub function which I can't vouch for.

As a side note, your sed doesn't work because it's basically saying "apply the substitution to the range of lines that starts with a line matching START and ends with a line matching END".

Source Link
Joseph R.
  • 40.6k
  • 8
  • 115
  • 146

You have indicated in a comment that awk is also allowed. So I'm basing my answer on this.

Assuming your STARTs and ENDs are balanced, if you split the line on either word, you find that you want to remove backslashes and double quotes from all even-numbered fields. To this end:

awk -F 'START|END' '{
                      for(i=2;i<=NF;i+=2){ # For each even-numbered field
                        gsub(/["\\]/,"",$i) # Remove " and \ from it
                        $i="START"$i"END" # Put START and END back around it
                      }
                    }' your_file

This assumes your implementation of awk has the gsub function which I can't vouch for.

As a side note, your sed doesn't work because it's basically saying "apply the substitution to the range of lines that starts with a line matching START and ends with a line matching END".