discipulus
discipulus

Reputation: 2725

Delete all the lines in a file that contains a specific character

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

Upvotes: 3

Views: 11784

Answers (5)

Vijay
Vijay

Reputation: 67291

awk '!($0~/?/){print $0}' file_name

Upvotes: 1

clt60
clt60

Reputation: 63952

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

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

translated

  • for every line on the STDIN
  • match it for the pattern =~
  • and if the match is not successful || - print out the line

Upvotes: 2

ysth
ysth

Reputation: 98398

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

or

perl -i -pe's/^.*?\?.*//s' file

Upvotes: 3

Fredrik Pihl
Fredrik Pihl

Reputation: 45672

Even better, just a single line using sed

sed '/?/d' input

use -i to edit file in place.

Upvotes: 6

dogbane
dogbane

Reputation: 274738

You can use sed to modify the file "in-place":

sed -i "/?/d" file

Alternatively, use grep:

grep -v "?" file > newfile.txt

Upvotes: 9

Related Questions