Armin
Armin

Reputation: 134

sed substitute dash with underscore until character is reached

Here is a sample text

enable-threads       = yes
enable-shared        = yes
enable-parser        = yes
DAL-II-VERSION     = 7.1.0
DAL-II-MAJOR       = -L -f -g
DAL-II-MINOR         = 1

I want to substitute all dashs (-) with underscores in everyline until = sign is reached.

Any idea?

Upvotes: 3

Views: 777

Answers (3)

potong
potong

Reputation: 58351

This might work for you (GNU sed):

sed 'h;s/=.*//;y/-/_/;G;s/\n[^=]*//' file

Upvotes: 0

Steve
Steve

Reputation: 54392

Unfortunately, sed doesn't support look-behinds or look-aheads. But perl and ssed do (see Rob's answer). If perl is unavailable, here's one way using awk:

awk -F= 'OFS=FS { gsub("-", "_", $1); }1' file.txt

Output:

enable_threads       = yes
enable_shared        = yes
enable_parser        = yes
DAL_II_VERSION     = 7.1.0
DAL_II_MAJOR       = -L -f -g
DAL_II_MINOR         = 1

HTH

Upvotes: 1

Rob Davis
Rob Davis

Reputation: 15772

The easiest way to do something like that is using a look-ahead assertion. sed doesn't support them, but perl does:

perl -pe 's/-(?=.*=)/_/g'

Upvotes: 4

Related Questions