用特定字符串替换文本文件中的整数

时间:2018-05-22 04:26:45

标签: c file text

我有一个填充字符串和数字的大文本文件。我想替换引号中的数字。例如,如果文本文件中有256个数字,则在修改后的文本文件中,它将为"256"。怎么做?

4 个答案:

答案 0 :(得分:0)

好的,不确定我是否正确阅读此内容但您是否正在尝试将其转换为字符串?

如果我误解了这个问题,请告诉我。 :)

希望这有帮助!

本文详细介绍:https://www.techwalla.com/articles/how-to-convert-int-to-string-in-python

本文快速回答:https://guide.freecodecamp.org/python/converting-integer-to-string-in-python/

答案 1 :(得分:0)

我认为您可以使用sed命令来实现此目的。像这样使用它:

sed 's/\([0-9][0-9]*\)/"&"/g'

这会将任何具有1位或更多位数的数字转换为"数字"格式。

答案 2 :(得分:0)

您已在ubuntufile中标记了问题。您也可以使用Perl one-liner获得此答案

perl -pe 's/(\d+)/"$1"/g' input

答案 3 :(得分:0)

与Neeraj Wadhwa的答案类似,但这只会匹配数字(例如与[[[[[FIRDatabase database] referenceWithPath:@"users"] queryOrderedByChild:@"usernames"] queryEqualToValue:@"your User Name"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { if ([snapshot.valueInExportFormat isKindOfClass:[NSDictionary class]]) { // User is exits } else { } }]; 不匹配):

text123

示例输入:

sed 's/\<[0-9]\+\>/"&"/g' inputfile

输出:

324 34 3214 31 324 fdsfds14323423 4324fdsfsda                          
432                                                                    
fdsfa 1234
相关问题