如何在代码树中多个文件中存在的特定函数的末尾插入一行文本。
下面的示例示例
file1 - location - /home/tmp/a/a.sh
NSArray *Array = [[NSArray alloc]initWithObjects:@"001", nil];
NSMutableDictionary *Diction = [NSMutableDictionary dictionary];
[Diction setObject:[NSString stringWithFormat:@"%@",Array]
forKey:@":val"];
AWSDynamoDBObjectMapper *dynamoDBObjectMapper =
[AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
UserDetails_Male *User = [UserDetails_Male new];
AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression
new];
scanExpression.filterExpression = @"UserID = :val";
scanExpression.expressionAttributeValues = @{@":val":Array};
[[dynamoDBObjectMapper scan:[User class]
expression:scanExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
} else {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
for (UserDetails_Male *book in paginatedOutput.items) {
//Do something with book.
NSLog(@"Data: %@",book);
}
}
return nil;
}];
-(void)BatchReq{
AWSDynamoDBKeysAndAttributes * keysAndAttributes = [
AWSDynamoDBKeysAndAttributes new ];
AWSDynamoDBAttributeValue * attributeValue2 = [
AWSDynamoDBAttributeValue new ];
attributeValue2.SS = Array;
keysAndAttributes.keys = @[ @{ @"UserId" :
attributeValue1 }, ];
keysAndAttributes.consistentRead = @YES;
AWSDynamoDBBatchGetItemInput * batchGetItemInput = [
AWSDynamoDBBatchGetItemInput new ];
batchGetItemInput.requestItems = @{ @"DynamoDB-OM-Sample"
: keysAndAttributes };
AWSDynamoDB * awsDynamoDB = [ AWSDynamoDB defaultDynamoDB
];
[ [ awsDynamoDB batchGetItem: batchGetItemInput ]
continueWithExecutor: [ AWSExecutor mainThreadExecutor ]
withBlock: ^ id ( AWSTask * task ) {
if ( task.result ) {
NSLog ( @"it's working!!" );
}
else {
NSLog ( @"not working... " );
}
return nil;
} ];
file2 - location - /home/tmp/b/b.sh
hello()
{
echo "line 1"
echo "line 2"
insert new line of text "good bye" here
}
我正在尝试使用sed命令,但上面这个场景看起来有点棘手。
答案 0 :(得分:3)
使用GNU sed:
sed '/^hello()/,/^}/s/^}/ insert new line of text "good bye" here\n&/' file
如果你想编辑你的文件"就地"使用sed的选项-i
。
答案 1 :(得分:3)
基本思想是寻找功能规范行直到函数结束行,并在该范围内,在函数结束行之前插入额外的文本:
<强> it59.sed
强>
/^hello()/,/^}/ { /^}/i\
insert new line of text "goodbye" here
}
<强>用法强>
sed -f it59.sed file1.sh file2.sh
或者,使用GNU sed
进行原位编辑:
sed -i -f it59.sed file1.sh file2.sh
或者,使用BSD进行原位编辑sed
:
sed -i '' -f it59.sed file1.sh file2.sh
等
示例输入
hello()
{
echo "line 1"
echo "line 2"
#insert new line of text "good bye" here
}
hello()
{
echo "line 1"
echo "line 2"
echo "line 3"
echo "line 4"
#insert new line of text "good bye" here
}
示例输出
hello()
{
echo "line 1"
echo "line 2"
#insert new line of text "good bye" here
insert new line of text "goodbye" here
}
hello()
{
echo "line 1"
echo "line 2"
echo "line 3"
echo "line 4"
#insert new line of text "good bye" here
insert new line of text "goodbye" here
}
请注意sed
忽略了前导空格。这是一件令人讨厌的事。修复这个问题有点困难。
<强> it61.sed
强>
/^hello()/,/^}/ { /^}/ { h; s/.*/ insert new line of text "goodbye" here/p; x; }; }
对于从函数开始到函数结束的范围,如果该行与函数的结尾匹配,则将该行复制到保留空间,用新文本替换该行并打印该行,并且交换保持空间。 BSD }
需要sed
之后的分号,而GNU sed
不需要分号。当然,也可以在命令行上写出该单行。多线版本(it59.sed
)在命令行上写入时需要一些小心 - 同样,GNU sed
具有与BSD sed
不同的规则,并允许BSD sed
无法做到的事情吨。 (POSIX sed
在这些问题上比GNU sed
更接近BSD sed
。)
示例输出
hello()
{
echo "line 1"
echo "line 2"
#insert new line of text "good bye" here
insert new line of text "goodbye" here
}
hello()
{
echo "line 1"
echo "line 2"
echo "line 3"
echo "line 4"
#insert new line of text "good bye" here
insert new line of text "goodbye" here
}