Re: Script to strip lines from files



Christian Hansen wrote:

> Kevin Collins <spamtotrash@xxxxxxxxxxxxxxxxxx> wrote in
> news:slrndrr006.du0.spamtotrash@xxxxxxxxxxxxxxxxx:
>
>> for file in *
>> do
>> lines="$(grep ^IP $file | tail -5)"
>> echo "$lines" > $file
>> done
>>
>
> Thanks. It works, but a parameter I forgot to add was that the files have
> other information also that is not to be touched. The IP lines are at the
> bottom of the file;
>
>
> Name
> Adress
> Phone
> (etc)
> IP 1
> IP 2
> IP 2
>
>
> Now it removes everything but the (5) IP lines.

The following should work if
the IP lines indeed start with
IP and no other lines follows
after the IP lines, what is
what you wanted as I gather.

for file in *
do
# untouched lines, match lines not starting with IP
non_ip_lines="$(grep -v ^IP $file)"
# IP lines, first 5
ip_lines="$(grep -m 5 ^IP $file)"
echo "$non_ip_lines" > $file
echo "$ip_lines" >> $file
done


--
Etienne Marais
Cosmic Link
South Africa

.