文件备份与排序的SHELL 脚本请教

文件备份与排序的SHELL 脚本请教

现在有两个文本文件,举例如下:

new.txt 文件:  

[VERSION]
VERSION=1.0
[Info]
Type=1
[Devices]
Count=1
1=034
[Data]
01=09b7090a010f0308FF800000 3 0100008301ff 2 0 5 64
02=07d7090a010f0308FF800000 3 0100008301ff 3 0 2 -2 0a
03=08d7090a010f0308FF800000 3 0100008302ff 6 0 2 57


old.txt 文件:
[VERSION]
VERSION=1.0
[Info]
Type=1
[Devices]
Count=1
1=034
[Data]
01=06d7090a010e3b1dFF800000 3 0100008301ff 2 0 5 68
02=02d7090a010e3b1dFF800000 3 0100008301ff 3 0 2 -1 0a


现在要实现将两个文件合并(合并到new.txt 文件中):
要求:
1。[Data] 这一行之前的东西均保持不变;
2。合并原则:将old.txt 中的数据内容追加在new.txt 文件的最后,顺序不变;
3。合并后的数据要标上序号(即:等号之前的序号应该是递增顺序)。

最后形成的new.txt 内容如下:

[VERSION]
VERSION=1.0
[Info]
Type=1
[Devices]
Count=1
1=034
[Data]
01=09b7090a010f0308FF800000 3 0100008301ff 2 0 5 64
02=07d7090a010f0308FF800000 3 0100008301ff 3 0 2 -2 0a
03=08d7090a010f0308FF800000 3 0100008302ff 6 0 2 57
04=06d7090a010e3b1dFF800000 3 0100008301ff 2 0 5 68
05=02d7090a010e3b1dFF800000 3 0100008301ff 3 0 2 -1 0a


求SHELL 实现,谢谢!      
FYI:
引用:
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; cat old.txt
[VERSION]
VERSION=1.0
[Info]
Type=1
[Devices]
Count=1
1=034
[Data]
01=06d7090a010e3b1dFF800000 3 0100008301ff 2 0 5 68
02=02d7090a010e3b1dFF800000 3 0100008301ff 3 0 2 -1 0a
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; cat new.txt
[VERSION]
VERSION=1.0
[Info]
Type=1
[Devices]
Count=1
1=034
[Data]
01=09b7090a010f0308FF800000 3 0100008301ff 2 0 5 64
02=07d7090a010f0308FF800000 3 0100008301ff 3 0 2 -2 0a
03=08d7090a010f0308FF800000 3 0100008302ff 6 0 2 57
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; nLast=$(tail -n 1 new.txt | awk -F= '{ print $1; }')                                          -(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; echo $nLast
03
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; sed -e '1,/\[Data]/d' old.txt | awk -v nLast=$nLast -F= '{ $1 += nLast; printf("%#o=%s\n", $1, $2); }'
04=06d7090a010e3b1dFF800000 3 0100008301ff 2 0 5 68
05=02d7090a010e3b1dFF800000 3 0100008301ff 3 0 2 -1 0a
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; sed -e '1,/\[Data]/d' old.txt | awk -v nLast=$nLast -F= '{ $1 += nLast; printf("%#o=%s\n", $1, $2); }' >> new.txt
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; cat new.txt
[VERSION]
VERSION=1.0
[Info]
Type=1
[Devices]
Count=1
1=034
[Data]
01=09b7090a010f0308FF800000 3 0100008301ff 2 0 5 64
02=07d7090a010f0308FF800000 3 0100008301ff 3 0 2 -2 0a
03=08d7090a010f0308FF800000 3 0100008302ff 6 0 2 57
04=06d7090a010e3b1dFF800000 3 0100008301ff 2 0 5 68
05=02d7090a010e3b1dFF800000 3 0100008301ff 3 0 2 -1 0a
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[9594 0] ; bye
      
谢谢dearvoid,另一种解如下:

#!/bin/sh
RECORDS=`sed -e '/^[ \t]*$/d' new.txt | sed -n '$ { s/\([0..9]*\)=.*/\1/p }'
START=`expr $RECORDS + 1`
sed -n '12,$  p' old.txt  | awk -F= '{printf "%02d=%s\n", COUNT, $2 ; COUNT ++ } '  COUNT="$START" >> new.txt

      
looks good       
偶也发一个:
引用:
#/bin/bash
#get the line number of "[Data]" in old.txt
ln_o=`grep Data old.txt -n | cut -d':' -f1`

#get the max line number of new.txt
mln_n=`tail -1 new.txt | cut -d'=' -f1`

#get the strings in old.txt after line $ln_o
put_str=`tail -n +$((ln_o+1)) old.txt`

#put the str to the tail of new.txt
for str_t in $put_str
do
        #get the value after "="
        str=`echo $str_t | cut -d'=' -f2`

        #put the line number,"=" and string to the last line of new.txt
        echo $((mln_n+1))"="$str >> new.txt
        mln_n=$((mln_n+1));
done;
bash version 3.1.17(1)

      
fitren 难得在 shell 版露面哈       
不好意思,踩了你的地盘!      
小弟也发一个

num=$(tail -1 new.txt | awk -F'='  '{print $1}')
v=$(awk '/\[Data\]/{print ++NR}' old.txt)
p=$(awk 'END{print NR}'  old.txt)
let m=$p-$v+1
i=1
while [ $i -le $m ]
do
   num=$(echo  $num | sed -n 's/^.//p')
   let num=$num+1
   num=0$num
   num2=$(sed -n "${v}p" old.txt  | awk -F'='  '{print $1}')
   sed -n "s/^${num2}/${num}/p"  old.txt >> new.txt
   let v=$v+1
   let i=$i+1
done      
都是高手哇!都是高手哇!