在分隔的文件中搜索字符串,然后打印字符串,直到在linux中到达下一个分隔符

时间:2014-10-24 14:25:59

标签: linux shell scripting

我在文件中有以下文字

1|2|SID1=/some/path|SID2=/some/path|4|5
1|2|SID1=/some/path|tel|path|SID2=/some/path|6|5|ord|til
1|2|SID1=/some/path|id1|id2|id3|SID2=/some/path|4|8|dea

在Linux中,如何在每行中搜索SID1和SID2并仅打印到下一个分隔符,因此输出应为

SID1=/some/path SID2=/some/path
SID1=/some/path SID2=/some/path
SID1=/some/path SID2=/some/path

2 个答案:

答案 0 :(得分:0)

Perl救援:

perl -lne 'print join " ", /SID[12]=[^|]*/g' file.txt

说明:Perl逐行读取文件(-n)。包含SID的行的所有部分后跟1或2,后跟=后跟除|之外的任何部分在它们之间印有空格。

答案 1 :(得分:0)

我觉得我错过了一个更好的解决方案,但这有效

ONELINE:

awk -F'|' '{a=0; for (i=1; i<=NF; i++) {if ($i ~ /^SID[[:digit:]]*=/) { printf "%s%s", a?OFS:(NR>1)?ORS:"", $i; a++ }}} END {print ""}' file

说明:

awk -F'|' '{
    # Reset our field tracking.
    a=0

    # Loop over all the fields in the line.
    for (i=1; i<=NF; i++) {
        # If the current field starts with 'SID#=' then
        if ($i ~ /^SID[[:digit:]]*=/) {
            # Print out the field with the appropriate separator.
            # When we have 'a' set we are in a line and want to print out a
            # leading OFS. Otherwise if this is not the first line we want to
            # print out a leading ORS. Otherise do nothing.
            printf "%s%s", a?OFS:(NR>1)?ORS:"", $i
            # Set our field tracking.
            a=1
        }
    }
}

END {
    # Print out the final newline.
    print ""
}' file