Ernst
Ernst

Reputation: 45

Can I set the LANG variable from within sed?

I wrote a script to replace certain expressions and remove others, but it failed in cleaning up special characters. Setting LANG=C solved that, but is it possible to set that environment variable from within sed?

Upvotes: 2

Views: 943

Answers (3)

Sdaz MacSkibbons
Sdaz MacSkibbons

Reputation: 28676

If your script is a sed script, you can just bump that part down and put it into a standard shell script. e.g.

#!/usr/bin/sed -f

1 {
    x
    s/^$/          /
    s/^.*$/&&&&&&&&/
    x
}

into

#!/bin/sh

export LANG=C

/usr/bin/sed '
1 {
    x
    s/^$/          /
    s/^.*$/&&&&&&&&/
    x
}
' "$@"

(from an example in the sed info document.)

With the trailing "$@", it should pass arguments and handle stdin the same way.

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 359875

You can set the environment variable so that it's active only for the single invocation of a command or script:

LANG=C sed ...

or

LANG=C sedscript

Upvotes: 1

akond
akond

Reputation: 16035

No, but you don't have to. You can set the envirinment variable before executing your sed command.

Upvotes: 2

Related Questions