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?
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?
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
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
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.
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!