shell遍历读取文件写入新文件

 1 #!/bin/sh
 2 
 3 #系统简称
 4 SYST="HVPS"
 5 #发送行号
 6 SEND1234SEND="660000000066"
 7 #接收行号
 8 RECV1234RECV="880000000088"
 9 
10 cd /home/was/test
11 list=(`ls hvps*`)
12 
13 for file in ${list[*]}
14 do
15 
16 cd /home/was/test
17 #清算SAPS
18 #MSGTYPE0MSGTYPE=$(cat $file |grep -m 1 'xmlns='|cut -d ':' -f 9|awk -F '"' '{print $1}'|awk -F ' ' '{print $1}')
19 MSGTYPE0MSGTYPE=$(cat $file |grep 'xmlns='|cut -d ':' -f 8|awk -F '"' '{print $1}')
20 msg=`cat $file`
21 
22 MSGRID0000MSGRID=$(cat $file |grep -m 1 '<MsgId>'|sed 's/<MsgId>//g'|sed 's/</MsgId>//g'|sed 's/s//g'|awk '{print $1}')
23 echo "Msgty:[${MSGTYPE0MSGTYPE}]"
24 echo "Msgid:[${MSGRID0000MSGRID}]"
25 
26 cd /home/was/new_test/
27 
28 if ! [ -f $file ];then
29    touch $file
30 else
31   rm -f $file
32   touch $file
33 fi
34 
35 
36 echo "{H:02${SEND1234SEND}  ${SYST}${RECV1234RECV}  ${SYST}000000000001XML${MSGTYPE0MSGTYPE[0]}     ${MSGRID0000MSGRID}    ${MSGRID0000MSGRID}    3D         }"|sed -e 's/}/}
/' >>$file
37 echo "$msg" >>$file
38 
39 sed -i '/XMLSpy v20/d' $file
40 
41 echo "${file}转换完成!"
42 head -n 3 $file 
43 echo " "
44 
45 done
View Code

shell打开读取一个文件内容,然后写入一个新的文件中,并对某些字段进行提取,写入新的字段的脚本。这是在工作中,需拼装大量的8543报文,原报文只读,不可写入,且也要对原报文进行提取某字段、新增报文头字段,删除某字段等操作,希望大家有用的着的地方,与大家共同学习进步!

Python方法:Python学习笔记五(读取提取写入文件)

原文地址:https://www.cnblogs.com/zwh-Seeking/p/11712467.html