Kaladhar Teja
Kaladhar Teja

Reputation: 13

Replacing a delimiter

I have a file with ^A as a delimiter:

8bf9f1897035297fb7b0767e4e5e191b2c93ceb^AAustralia^A2016-01-13 05:19:06^A
8bf9f1897035297fb7b0767e4e5e191b2c93ceb^AAustralia^A2016-01-13 05:19:06^A
8bf9f1897035297fb7b0767e4e5e191b2c93ceb^AAustralia^A2016-01-13 05:19:06^A

How can I replace the delimiters with | using awk or sed?

Desired output:

8bf9f1897035297fb7b0767e4e5e191b2c93ceb|Australia|2016-01-13 05:19:06|
8bf9f1897035297fb7b0767e4e5e191b2c93ceb|Australia|2016-01-13 05:19:06|
8bf9f1897035297fb7b0767e4e5e191b2c93ceb|Australia|2016-01-13 05:19:06|

Upvotes: 0

Views: 104

Answers (2)

peak
peak

Reputation: 116720

  1. If you are referring to the control-A character:

    • The following will work with both GNU and non-GNU sed: sed 's/\x01/|/g'
    • Possibly faster would be tr '\001' '|'
  2. If you are referring to the two-character sequence ^A:

    • sed 's/\^A/|/g'
    • in awk you could use gsub( /\^A/,"|" )

Upvotes: 2

legoscia
legoscia

Reputation: 41548

If your version of sed is GNU sed, you can use GNU Extensions for Escapes in Regular Expressions, in particular:

`\cX'
     Produces or matches `CONTROL-X', where X is any character.  The
     precise effect of `\cX' is as follows: if X is a lower case
     letter, it is converted to upper case.  Then bit 6 of the
     character (hex 40) is inverted.  Thus `\cz' becomes hex 1A, but
     `\c{' becomes hex 3B, while `\c;' becomes hex 7B.

So that would be:

sed -e "s/\ca/|/" < input.txt

Upvotes: 1

Related Questions