shell 字元處理

pentium發表於2011-06-17

用awk替換檔案指定位(如第7位)

用awk替換檔案指定列(如第3列)

[@more@]

用SHELL替換檔案指定位數字符(把第7位替換):

123456789 -> 123456X89

awk '{print substr($0,1,6)"X"substr($0,8,80)}' filename

用awk替換檔案指定列(如第3列):

position=3

new_value=newvalue

line=ddd sss ggg www

echo "${line}" | awk '{$'$position'='$new_value'; print}'

To determine if the string is number type:

set number_ind=`echo $2|sed 's/[0-9]//g'`
if ( $number_ind == '' ) then
set file_type="DELIMITER" #Try to convert delimiter file
else
set file_type="XML" #Try to convert xml file
set tag_name=$2 #the tag name we want to convert in XML
endif

Populate XML to get the value between tag:

echo "99999" | sed 's///g'
99999

Go through file:

set n=`wc -l set i=1
while ($i <= $n)

set line="`awk '{if (NR == $i) print}' $file_tmp`"

echo "${line}"

end

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14377/viewspace-1051290/,如需轉載,請註明出處,否則將追究法律責任。

相關文章