将每行中的 14 个字符长度的数字替换为新数字
Replacing 14 character length of number in each line to a new number
我有一个每行 45 个字符的文件。
例如:
123456789123456789123456789123456789123456789
我需要删除每行第 10 到第 15 个字符之间的数字,并将相同的数字加上 1000。现在我需要将结果放在相同的字符长度(比如第 10 到第 15 个字符之间)。
如果12345是一行中第10到第15个字符之间的5个数字,我需要给它加1000,所以这个值就是13345.
所以我需要替换同一个位置的ans。
最终结果:
123456789133456789123456789
请建议使用 unix。
这里有一个方法可以用 Shell Parameter Expansion:
while read line; do
echo "${line:0:9}$((${line:9:5} + 1000))${line:14}"
done < file
它只是获取行的相关部分并进行处理。它使用子字符串扩展:
${parameter:offset}
${parameter:offset:length}
Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions (see Shell Arithmetic). This is referred to as Substring Expansion.
扩展到新要求
引用:
I need to get the character values from 16th to 20th character of each line. I need to add 10000 to that number and place the result in two place in the same line, 1. from 16th to 20th character. 2. from 6th to 10th character.
eg:
i/p 1111122222333334444455555
o/p 1111154444333345444455555.
process: 16th to 20th character:44444
adding 10000: 54444(result)
(have to place this result in 2 location as mentioned above).
rest of the values in the lines should be as it is.
while read line; do
A1=${line:0:5}
#A2=${line:5:5}
A3=${line:10:5}
A4=${line:15:5}
A5=${line:20}
result=$((A4+10000))
echo "$A1$result$A3$result$A5"
done < file
说明
它只是保存行中的子字符串,然后相应地处理它们:
1111122222333334444455555
<---><---><---><---><--->
A1 A2 A3 A4 A5
注意你不需要 $A2
,所以,我刚刚评论了它。
希望对您有所帮助。
我有一个每行 45 个字符的文件。
例如:
123456789123456789123456789123456789123456789
我需要删除每行第 10 到第 15 个字符之间的数字,并将相同的数字加上 1000。现在我需要将结果放在相同的字符长度(比如第 10 到第 15 个字符之间)。
如果12345是一行中第10到第15个字符之间的5个数字,我需要给它加1000,所以这个值就是13345.
所以我需要替换同一个位置的ans。
最终结果:
123456789133456789123456789
请建议使用 unix。
这里有一个方法可以用 Shell Parameter Expansion:
while read line; do
echo "${line:0:9}$((${line:9:5} + 1000))${line:14}"
done < file
它只是获取行的相关部分并进行处理。它使用子字符串扩展:
${parameter:offset}
${parameter:offset:length}
Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions (see Shell Arithmetic). This is referred to as Substring Expansion.
扩展到新要求
引用:
I need to get the character values from 16th to 20th character of each line. I need to add 10000 to that number and place the result in two place in the same line, 1. from 16th to 20th character. 2. from 6th to 10th character.
eg:
i/p 1111122222333334444455555
o/p 1111154444333345444455555.
process: 16th to 20th character:44444
adding 10000: 54444(result)
(have to place this result in 2 location as mentioned above).
rest of the values in the lines should be as it is.
while read line; do
A1=${line:0:5}
#A2=${line:5:5}
A3=${line:10:5}
A4=${line:15:5}
A5=${line:20}
result=$((A4+10000))
echo "$A1$result$A3$result$A5"
done < file
说明
它只是保存行中的子字符串,然后相应地处理它们:
1111122222333334444455555
<---><---><---><---><--->
A1 A2 A3 A4 A5
注意你不需要 $A2
,所以,我刚刚评论了它。
希望对您有所帮助。