需要使用Unix shell脚本在每行末尾添加空格

问题描述 投票:-2回答:2

我需要在每行的末尾添加空格,除了标题行.Below是我文件的示例:

13120000005000002100000000000000000000081D000
231200000000000    000     00XY018710V000000000                        
231200000000000    000     00XY018710V000000000
13120000012000007000000000000000000000081D000
231200000000000   000     00XY057119V000000000                        

所以第1和第4行(从131200开始)是我的标题行...除了我的标题我想在每行的末尾有7-8个空格。

请找到我目前使用的代码:

find_list=`find *.dat -type f`
Filename='*.dat'
filename='xyz'
for file in $find_list

do
 sed -i -e 's/\r$/         /' "$file"
  n=1
   loopcounterpre=""

newfile=$(echo "$filename" | sed -e 's/\.[^.]*$//')".dat"

while read line
do
        if [[ $line != *[[:space:]]* ]]

        then
                rowdetail=$line

                loopcounter=$( echo "$rowdetail" | cut -b 1-6) 
        if [[ "$loopcounterpre" == "$loopcounter" ]]
        then 
         loopcounterpre=$loopcounter
         #Increases the counter for  in the order of 001,002 and so on until the Pay entity is changed
         n=$((n+1))
        #Resets the Counter to 1 when the pay entity changes
        else
         loopcounterpre=$loopcounter
         n=1
        fi
        printf -v m "%03d" $n
        llen=$(echo ${#rowdetail})

        rowdetailT=$(echo "$rowdetail" | cut -b 1-$((llen-3)))
                ip=$rowdetailT$m
                echo "$ip" >> $newfile

        else
                rowdetail=$line
        echo "$rowdetail" >> $newfile
        fi

done < $file

bye
EOF

done
shell unix add space using
2个回答
1
投票

整个脚本可以用一行GNU sed替换:

sed -is '/^131200\|^1351000/!s/$/       /' $(find *.dat -type f)

0
投票

使用awk:

$ awk '{print $0 ($0~/^(131200|1351000)/?"":"        ")}' file

print当前记录$0,如果它开始与$0~/^(131200|1351000)/打印""其他:打印" "

© www.soinside.com 2019 - 2024. All rights reserved.