spite
Code: Select all
cat <<EOF > text.txt
#NEWLINE#
#DELETE#
EOF
sed "s/$/#NEWLINE#/" text.txt | sed "s/^Rec\./#DELETE#Rec\./" | tr -d "\n" | sed "s/#NEWLINE##DELETE#/ /g" | sed "s/#NEWLINE#/\n/g"
gives 2 empty lines
I know that usually, there is not such line involving
#NEWLINE#
nor such line starting with
#DELETE#
Another candidate is:
test.sh
Code: Select all
#!/bin/bash
echo -n "input file? :"
read a
echo -n "output file? :"
read b
rm -f $b
cat $a|while read line
do if [ -n "$(echo $line|grep "^Rec. ")" ]
then line2="$line2 $line"
else echo $line2 >> $b
line2=$line
fi
done
sed -i -n 2,/end/p $b
But,
Code: Select all
cat <<EOF > test2
one test
#NEWLINE#
#NEWLINE#
test two
#DELETE#
Rec. something
two test three
yes or not
another lines
Rec. something else
Rec. another
and another
and another2
EOF
bash ./test.sh
test2
test3
more test3
gives
one test
#NEWLINE#
#NEWLINE#
test two
#DELETE# Rec. something
two test three
yes or not
another lines Rec. something else Rec. another
and another
It does not output the last line
Thus,
Improved test.sh
Code: Select all
#!/bin/bash
echo -n "input file? :"
read a
echo -n "output file? :"
read b
rm -f $b
echo "" >> $a
cat $a|while read line
do if [ -n "$(echo $line|grep "^Rec. ")" ]
then line2="$line2 $line"
else echo $line2 >> $b
line2=$line
fi
done
sed -i -n 2,/end/p $b
sed -i '$d' $a
I want gaijin to pay me for doing his/her homework
But, learning and thinking and making is interesting. Enjoy yourself by making scripts by yourself.
EDIT
The improved test.sh is not perfect. If the first line(s) start with "Rec. ", the first line(s) vanishes. How to overcome the problem? There are many solutions. Enjoy thinking.