Delete all lines in a file that contains a specific character

I want to delete all lines / lines in a file that has a specific character, '?' in my case. I hope there is one line of command in Bash or AWK or Perl. thank

+3
source share
5 answers

Here are grep, sed and perl solutions - just for fun, pure bash one:

pattern='?'
while read line
do
    [[ "$line" =~ "$pattern" ]] || echo "$line"
done

translated

  • for each line on STDIN
  • matches the pattern =~
  • and if the match is not fulfilled ||, print the line
+2
source

You can use sedto modify the file "in place":

sed -i "/?/d" file

Alternatively, use grep:

grep -v "?" file > newfile.txt
+9
source

, sed

sed '/?/d' input

-i .

+6
perl -i -ne'/\?/ or print' file

perl -i -pe's/^.*?\?.*//s' file
+3
awk '!($0~/?/){print $0}' file_name
+1

All Articles