在bash中的某个String之后打印字符串行

时间:2015-12-22 21:11:47

标签: bash shell

我在bash中有这个代码,它获取fileA.txt中的最后一个字符串,并将其与if语句中的fileB.txt进行比较。

fileA.txt包含: <登记/> var mHeaderLogin = angular.module('app.directives.mHeaderLogin', ['mMain'])// mMain-dependent due to factory call .directive('dHeaderLogin', fHeaderLogin); function fHeaderLogin() { return { restrict: 'A', //replace: true, scope: { username: '=' }, //isolate scope templateUrl: 'app/directives/header-Login/header-Login.html', controller: function ($scope, simpleFactory) { $scope.users = simpleFactory.getUsers(); $scope.username = "test"; }, link: function (scope, element, attrs) { scope.login = { opLogin: false }; scope.submit = function () { console.log(scope.username); console.log("fSubmit"); } } } }
[GRP2.0] 10221 ==== 2013-03-10 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10222 ==== 2013-03-11 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10223 ==== 2013-03-12 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10224 ==== 2013-03-13 10:55:26-Custom field deleted,fields,admin

fileB.txt包含:

[GRP2.0] 10225 ==== 2013-03-14 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10223 ==== 2013-03-12 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10224 ==== 2013-03-13 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10225 ==== 2013-03-14 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10226 ==== 2013-03-15 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10227 ==== 2013-03-16 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10228 ==== 2013-03-17 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10229 ==== 2013-03-18 10:55:26-Custom field deleted,fields,admin

问题:
[GRP2.0] 10230 ==== 2013-03-19 10:55:26-Custom field deleted,fields,admin$file

有没有办法在fileB.txt中的$ stringCompared之后回显所有内容?

fileA.txt有这个:
 $stringCompared

fileC.txt中的预期输出:
[GRP2.0] 10225 ==== 2013-03-14 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10226 ==== 2013-03-15 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10227 ==== 2013-03-16 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10228 ==== 2013-03-17 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10229 ==== 2013-03-18 10:55:26-Custom field deleted,fields,admin

[GRP2.0] 10230 ==== 2013-03-19 10:55:26-Custom field deleted,fields,admin

我也尝试过使用其他途径:

lastString=$(tac "$file" |egrep -m1 '.')
        stringCompared=$lastString
        if grep -Fxq "$lastString" /folder/fileB.txt; then

        tac /folder/fileB.txt | awk -v savedString="$stringCompared" '1; $0 ~ savedString {exit}' | tac

        printf "$savedString\n"
        echo "$savedString" | tee /folder/temp.txt  

但它没有用,有什么提示吗?

3 个答案:

答案 0 :(得分:1)

使用tac从最后处理文件,打印到匹配项,然后tac返回:

tac file | awk '/match/ {exit} 1' | tac

测试

$ seq 10 | tac | awk '/5/ {exit}1' | tac
6
7
8
9
10

$ seq 10 | tac | awk '1; /5/ {exit}' | tac
5
6
7
8
9
10

如果您还要打印匹配的行。

答案 1 :(得分:0)

您可以使用awk

awk 'FNR==NR{lastStr=$0; next} !p && p=index($0, lastStr){next} p' fileA.txt fileB.txt
[GRP2.0] 10226 ==== 2013-03-15 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10227 ==== 2013-03-16 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10228 ==== 2013-03-17 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10229 ==== 2013-03-18 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10230 ==== 2013-03-19 10:55:26-Custom field deleted,fields,admin

答案 2 :(得分:0)

awk救援!

$ awk -v s="$(tail -1 fileA.txt)" 'f; s==$0{f=1}' fileB.txt

[GRP2.0] 10226 ==== 2013-03-15 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10227 ==== 2013-03-16 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10228 ==== 2013-03-17 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10229 ==== 2013-03-18 10:55:26-Custom field deleted,fields,admin
[GRP2.0] 10230 ==== 2013-03-19 10:55:26-Custom field deleted,fields,admin

提取fileA的最后一行并分配给awk变量s,当一行等于fileB中的s时设置标志f,打印行为set flag。