I want to trim lines less than 4 characters, except if the line begins with # or !.
Sample input:
aa
bbb
dasasdsad
! f
#
!
# s
a
&B@*!
Output:
dasasdsad
! f
#
!
# s
&B@*!
With grep:
< file.in grep -E '^[#!]|.{4}' > file.out
That is, select lines that either start with # or ! or contain a sequence of 4 characters.
Or with awk:
< file.in awk '/^[#!]/ || length >= 4' > file.out
Or with sed:
< file.in sed -e '/^[#!]/b' -e '/.\{4\}/!d' > file.out
You can try something like this, code not tested:
while read line;
do
var=$(echo $line | awk '{ print length }')
if [ $var -le 4 ]; then
echo $line >> tmp.file
fi
done < your.file
This are 2 steps, while read, it writes the lines of less than 4 characters to tmp.file.
With comm -23 your.file tmp.file you compare both files, suppress common lines and > you.new.file is the final saved version
awk once for each line in the file, which will be slow.