Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a file (/usr/test.log) with a bunch of lines, of the form

[enable/disable] [name] [stuff]

For example:

enable telnet max_pkt 0 max_payload 0 min_payload 1 disable_function 0 inherit_max_packets 0
enable telnets max_pkt 0 max_payload 0 min_payload 0 disable_function 0 inherit_max_packets 0
disable teredo max_pkt 0 max_payload 0 min_payload 40 disable_function 0 inherit_max_packets 0

Given a name and a status ("enable" or "disable"), what is a sed command to replace the line containing the given name with the given status? For example, given "telnet" and "disable", it would change the above first line to

disable telnet max_pkt 0 max_payload 0 min_payload 1 disable_function 0 inherit_max_packets 0

If there are simpler ways to programatically do those operations from the command line without using sed, that is fine also.

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

This does it. Annoyingly, |, (, and ) need escaping.

sed -i 's/^\(enable\|disable\) telnet/disable telnet/g' /usr/test.log
share|improve this answer
    
You need to enable extended regular expressions if you don't want to escape everything. In GNU sed, that's -r. –  slhck Feb 22 '13 at 12:16
add comment

If you know what you want to replace it is just

sed -i 's/^enable telnet /disable telnet /g' filename 

As long as now payload is in between [enable/disable] [name] this should be fine. The "^" character is to match the begin of the line.

If you want to exchange the value regardless of the current state then you can use branching in sed

sed 's/^enable telnet /disable telnet /g 
     b end                                    #go to end if first rule has matched
     s/^disable telnet /enable telnet /g
     :end'  filename
share|improve this answer
    
Thanks. A priori I don't know the status of telnet. It could be either "enable" or "disable". What's the syntax for "or"? –  Randomblue Feb 22 '13 at 11:56
    
I've tried sed -i 's/^(enable|disable) telnet/disable telnet/g' filename but that doesn't work. –  Randomblue Feb 22 '13 at 12:00
    
oh now i got your question, or maybe i got it not. The disable lines are allready disable why do you want to change them again? They are just feed through. –  85fce Feb 22 '13 at 12:15
add comment

With AWK:

tmp=$(mktemp); awk '$2=="telnet" {$1="disable"} {print}' test.log > $tmp && mv $tmp test.log

The above will write disable in the telnet line.

Scripting it:

#!/bin/sh
file=/path/to/test.log

usage() {
    cat <<-USAGE
    Enable/disable services in $file

    usage: ${0##*/} SERVICE [enable|disable]

    positional arguments:
      SERVICE   service name
    USAGE
    exitstatus=${1:-1}
    exit $exitstatus
}

error() {
    printf '%s: error: %s\n' "${0##*/}" "$1" 1>&2
    [ -z "$2" ] || exit $2
}

success() {
    printf '%s: Service `%s` %sd\n' "${0##*/}" "$service" "$status"
}

[ -f "$file" ] || error 'File not found.' 2

tmp=$(mktemp)

[ $# -eq 0 ] && usage

if [ $# -ne 2 ] || ( [ "$2" != "enable" ] && [ "$2" != "disable" ] ); then
    error 'Argument error.'
    usage 3
fi

service=$1
status=$2

awk -v status="$status" -v service="$service" -v ok=99 \
    '$2==service {$1=status; ok=0} {print} END{exit ok}' "$file" > $tmp
awkexit=$?
case $awkexit in
    0 )  mv $tmp "$file" && success;;
    99)  error 'Service not found.' 4;;
    * )  error 'Unkown error. Status change failed.' 5;;
esac

Change file to be the real path to the file in question. Save the script as e.g. myservice, make it executable, put it in your PATH, and then it can be executed like:

$ myservice telnet disable
myservice: Service `telnet` disabled.

The script will tell you if the status change was successful or if the service did not exist in the given file. I "abuse" the exit code of AWK to check status of the change.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.