grep命令不能满足我的期望

时间:2015-09-25 18:21:14

标签: linux shell grep sh cut

我有一个如下所述的文本文件,随之而来的是我将传递一个输入,我想要一个相应的输出。

输入文件:test.txt

abc:abc_1
abcd:abcd_1
1_abcd:1_abcd_bkp
xyz:xyz_2

因此,如果我将abc与上述test.txt文件一起使用,我希望abc_1;如果我通过abcd,我需要abcd_1作为输出。

我试过了cat text.txt | grep abc | cut -d":" -f2,2,但我得到了输出

abc_1
abcd_1
1_abcd_bkp

当我只想要abc_1时。

5 个答案:

答案 0 :(得分:2)

使用GNU grep:

grep -Po "^abc:\K.*" file

输出:

abc_1
  

\K使文本与整个正则表达式匹配保持匹配。

答案 1 :(得分:1)

您希望将regular expression-e开关一起使用。 特别是,正则表达式允许您使用插入符号(^)来表示行的开头。

由于您只关心abc在行开头的时间,然后是:,您需要:

cat test.txt | grep -e "^abc:" | cut -d":" -f2,2

输出:

abc_1

答案 2 :(得分:1)

要求救援!

awk -F: -v key="abc" '$1==key{print $2}'

使用:作为分隔符,在字段1上查找键以返回字段2.

或者,通过在脚本中移动键

awk -F: '$1=="abc"{print $2}'

答案 3 :(得分:1)

您可以尝试排除-v:

cat text.txt | grep abc | grep -vi abc [a-z]

不确定这是否可行,尝试使用这种想法

答案 4 :(得分:1)

如果不指定要打印的第二个字段,整行将是或在其他情况下为行。

#include<iostream>
#include<sstream>
#include<string>

using std::cout;
using std::cin;
using std::endl;
using std::stringstream;
using std::string;

int strategy1(int player, int previous_result, int previous_play, int opponent_previous_play);
int strategy2(int player, int previous_result, int previous_play, int opponent_previous_play);
int strategy3(int player, int previous_result, int previous_play, int opponent_previous_play);
int score(int p1, int p2);
void print_result(int round, int p1, int p2, int winner);





int main (){
    int result, p1, new_p1, p2, new_p2, rounds;
    p1 = 1; // start with rock
    p2 = 1; // start with rock
    cout << "How many rounds:";
    cin >> rounds;

    for(int i=0; i<rounds; i++){
        result = score(p1,p2);
        print_result(i+1, p1, p2, result);
        new_p1 = strategy1(1, result, p1, p2);
        new_p2 = strategy3(2, result, p2, p1);
        p1 = new_p1;
        p2 = new_p2;
    }
}

int strategy1(int player, int previous_result, int previous_play, int opponent_previous_play){

    if(previous_play == 1)
        previous_play = 2;
    else if(previous_play == 2)
        previous_play = 3;
    else if(previous_play == 3)
        previous_play = 1;


    return previous_play;


}

int strategy2(int player, int previous_result, int previous_play, int opponent_previous_play){

    if(player == 1){

        if(previous_result == 2)
            previous_play = opponent_previous_play;
        else
            previous_play = previous_play;
    }

    if(player == 2){
        if(previous_result == 1)
            previous_play = opponent_previous_play;
        else
            previous_play = previous_play;

    }
}

int strategy3(int player, int previous_result, int previous_play, int opponent_previous_play){

     if(player == 1){

        if(previous_result == 2){

            if(previous_play == 1 && opponent_previous_play == 2)
                previous_play = 3;
            else if(previous_play == 2 && opponent_previous_play == 3)
                previous_play = 1;
            else
                previous_play = 2;
        }
    }


    if(player == 2){

        if(previous_result == 1){

            if(previous_play == 1 && opponent_previous_play == 2)
                previous_play = 3;
            else if(previous_play == 2 && opponent_previous_play == 3)
                previous_play = 1;
            else
                previous_play = 2;
        }
    }


    return previous_play;
}

int score(int p1, int p2){
    long result = 0;

    if( ((p1 == 1) && (p2 == 1)) || ((p1 == 2) && (p2 == 2)) || ((p1 == 3) && (p2 == 3)) )
        result = 0; 

    else if( ((p1 == 1) && (p2 == 3 )) || ((p1 == 2) && (p2 == 1)) || ((p1 == 3) && (p2 == 2)) )
        result = 1;

    else if(( (p1 == 1) && (p2 == 2) ) || ((p1 == 2) && (p2 == 3)) || ((p1 == 3 ) && (p2 == 1)) ) 
        result = 2;


        return result;
}




void print_result(int round, int p1, int p2, int winner){


    //ERROR WON'T LET ME CHANGE THE INT INTO A STRING
    if(p1 == 1)
        p1 = "rock";
    else if(p1 == 2)
        p1 = "paper";
    else
        p1 = "scissors";

    //ERROR WON'T LET ME CHANGE THE INT INTO A STRING
    if(p2 == 1)
        p2 = "rock";
    else if(p2 == 2)
        p2 = "paper";
    else
        p2 = "scissors";



    if(winner == 0)
        cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": tie" << endl;
    else if(winner == 1)
        cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": p1" << endl;
    else if(winner == 2)
      cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": p2" << endl;

}