Avinash Gawade
Avinash Gawade

Reputation: 1

Sed - How to replace first character based on next 2 characters

I want to remove ";" from line if it is not followed by "number" + "=" sign. e.g.

echo "32=rtys;54tyu;45_fd;62=xyz;fdg"
output should be "32=rtys54tyu45_fd;62=xyzfdg"

I tried below but it worked only for letters.

sed 's/;\($\|[^[:digit:]=]\)/\1/g' 

but it only works for ;+letters and not others.

Upvotes: 0

Views: 469

Answers (3)

Sundeep
Sundeep

Reputation: 23667

Easy to do with negative lookahead:

$ echo "32=rtys;54tyu;45_fd;62=xyz;fdg" | perl -pe 's/;(?!\d+=)//g'
32=rtys54tyu45_fd;62=xyzfdg
  • ;(?!\d+=) means ; not followed by one or more digits and =

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203169

$ sed -E 's/a/aA/g; s/;([0-9]+=)/aB\1/g; s/;//g; s/aB/;/g; s/aA/a/g' file
32=rtys54tyu45_fd;62=xyzfdg

This is a common, idiomatic approach - see https://stackoverflow.com/a/38153467/1745001 for an explanation.

Upvotes: 0

John1024
John1024

Reputation: 113814

Using sed

$ s="32=rtys;54tyu;45_fd;62=xyz;fdg"
$ sed -E 's/;/\n/g; s/\n[0-9]+=/;&/g; s/\n//g' <<<"$s"
32=rtys54tyu45_fd;62=xyzfdg

This works in three steps:

  1. s/;/\n/g replaces all semicolons with newlines. Because, by default, sed takes input one line at a time, the pattern space will never have a newline to start. Thus will be no confusion.

  2. s/\n[0-9]+=/;&/g puts a semicolon in front of any newline followed by a number followed by an equal sign.

  3. s/\n//g removes all newlines.

The above was tested on GNU sed. For BSD/OSX, some changes will likely be needed. The following might work:

sed -E $'s/;/\n/g; s/\n[0-9]+=/;&/g; s/\n//g' <<<"$s"

Using awk

$ s="32=rtys;54tyu;45_fd;62=xyz;fdg"
$ awk -F\; '{$1=$1; for (i=2;i<=NF;i++) if ($i~/^[0-9]+=/) $i=";"$i} 1' OFS="" <<<"$s"
32=rtys54tyu45_fd;62=xyzfdg

This works by using ; as the field separator on input and the empty string as a field separator on output. This would remove all ; except that we check each field: if a field begins with number+=, we add a semicolon to the beginning of that field.

Upvotes: 2

Related Questions