redhat - Bash script to remove lines containing any of a list of words -
i have large config file use define variables script pull it, each defined on single line. looks this:
var val foo bar foo1 bar1 foo2 bar2
i have gathered list of out of date variables want remove list. go through manually, script, @ least more stimulating. file contains vlaues may contain multiple instances. idea find value, , if it's found, remove entire line.
does know if possible? know sed
not know how make use file input.
#!/bin/bash shopt -s extglob remove=(foo1 foo2) ifs='|' eval 'pattern="@(${remove[*]})"' while read -r line; read b <<< "$line" [[ $a != $pattern ]] && echo "$line" done < input_file.txt > output_file.txt
or (use copy first)
#!/bin/bash shopt -s extglob file=$1 remove=("${@:2}") ifs='|' eval 'pattern="@(${remove[*]})"' save=() while read -r line; read b <<< "$line" [[ $a != $pattern ]] && save+=("$line") done < "$file" printf '%s\n' "${save[@]}" > "$file"
running with
bash script.sh your_config_file pattern1 pattern2 ...
or
#!/bin/bash shopt -s extglob file=$1 patterns_file=$2 readarray -t remove < "$patterns_file" ifs='|' eval 'pattern="@(${remove[*]})"' save=() while read -r line; read b <<< "$line" [[ $a != $pattern ]] && save+=("$line") done < "$file" printf '%s\n' "${save[@]}" > "$file"
running with
bash script.sh your_config_file patterns_file
Comments
Post a Comment