1

I have several huge .txt files and I need to remove a line if it is exactly 9 characters long. No more no less.

Is there a way to do this using awk / sed?

4

With GNU sed's extended regexes:

for file in ./*.txt; do
    sed -i -r '/^.{9}$/d' "${file}"
done

(Use -E instead of -r on FreeBSD/macOS (-E will also work in recent versions of GNU sed) and -i '' instead of -i)

As pointed out by don_crissti, with GNU sed you don't need the loop:

sed -s -i -r '/^.{9}$/d' ./*.txt
4

With awk:

for f in ./*.txt; do
    awk 'length($0) != 9' "$f" >"destdir/$f"
done

With sed:

for f in ./*.txt; do
    sed '/^.\{9\}$/d' "$f" >"destdir/$f"
done

With grep:

for f in ./*.txt; do
    egrep -vx '.{9}' "$f" >"destdir/$f"
done
  • Note that \{ was added to grep before { was added to egrep. Some egrep implementations (like Solaris /bin/egrep) still don't support it (as that would break backward compatibility while the introduction of \{ in BRE didn't break backward compatibility). The modern and standard equivalent of egrep is grep -E (on Solaris, you need /usr/xpg4/bin/grep or command -p grep). – Stéphane Chazelas Dec 7 '16 at 21:37
1

This may work:

grep -vE '^.{9}$' filename > new_filename

Switch the 9 to whatever characters needed.

       -v, --invert-match
          Invert the sense of matching, to select non-matching lines.
       -E, --extended-regexp
          Interpret PATTERN as an extended regular  expression  (ERE,  see
          below).

. means any character, {9} means match this pattern 9 times. ^ means start of line and $ means end of line.

1
awk length!=9 < in > out
0
for i in *.txt
do
  egrep -v '^.........$' <"$i" >destdir/"$i"
done

Or, if you insist on using sed rather than egrep:

for i in *.txt
do
  sed '^.........$/d' <"$i" >destdir/"$i"
done

To remove lines that are 235 characters long:

X=""
for i in `seq 1 235`
do
  X="$X."
done

for i in *.txt
do
  egrep -v '^'"$X"'$' <"$i" >destdir/"$i"
done

Simplicity itself!

  • What if the next guy needs to remove lines that are 235 chars long ? – don_crissti Nov 27 '16 at 19:55
  • @don_crissti Added to answer. – DepressedDaniel Nov 27 '16 at 19:58
  • You call that "simplicity" ? OK... – don_crissti Nov 27 '16 at 19:58
  • @don_crissti Admittedly, the '{}' syntax for regular expressions is a cleaner way to do it. I didn't know sed supported it until now, tbh. – DepressedDaniel Nov 27 '16 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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