简体   繁体   English

sed字符串操作

[英]Sed String manipulation

I need help From a list I would like to get the addition of characters like the example below: Start: 我需要帮助从列表中,我想获得其他字符,例如以下示例:开始:

1
1
13
5
14
4
1
5
12
7
8
9
4
18
3
20
11
17
13

=============================================== Final results : ==========================================最终结果:

9001
9001
9013
9005
9014
9004
9001
9005
9012
9007
9008
9009
9004
9018
9003
9020
9011
9017
9013

this command does not work: 该命令不起作用:

sed "s/^/9000/g" file.txt

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed -r 's/^/0000/;s/^0*(.{3})$/9\1/' file

Prepend zeroes to the front of the number. 在数字前加零。 Prepend a 9 and remove excess zeroes. 在数字9前面加上多余的零。

You can do it like this: 您可以这样做:

 for num in 1 1 13 5 14 4 1 5 12 7 8 9 4 18 3 20 11 17; do echo "$(($num + 9000))"; done

If you also have numbers in the list, which you don't want to process, because they are already in the 90XX format you can throw in an if statement: 如果列表中还有不想处理的数字,因为它们已经是90XX格式,则可以在if语句中输入:

for num in 1 1 13 5 14 4 1 5 12 7 8 9 4 18 3 20 11 17 9005; do if [ $(($num)) -le 9000 ]; then echo "$(($num + 9000))"; else echo $num; fi; done

For loop in bash - for; bash中的 for 循环-for ; do; 做; done; 完成

Bash arithmetic expression to add the numbers - $((EXPR)) Bash算术表达式以添加数字- $((EXPR))

这应该为您工作。

for num in `cat file.txt`; do if [ $num -le 9000 ]; then echo "$(($num + 9000))"; else echo $num; fi; done

您可以尝试(GNU sed):

sed 's/.*/echo $((9000+&))/e' infile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM