Magix
Magix

Reputation: 5339

Comment/uncomment a line where a word is matched without using sed/awk

How can I comment out lines where a certain word can be found in a bash script, using piped UNIX commands (no sed/awk) ?

The comment character is # .

Here is how It could start :

cat $file | grep $word |  ...

Upvotes: 2

Views: 177

Answers (1)

Cyrus
Cyrus

Reputation: 88646

With GNU bash.

#!/bin/bash

keyword="foo"

while IFS= read -r line; do
  [[ "$line" =~ $keyword ]] && line="${line#*#}"
  printf "%s\n" "$line"
done < /etc/network/interfaces > /tmp/interfaces_modified

Upvotes: 1

Related Questions