I have to split

[X] ||| you owns the [X,1] ||| you own the [X,1] ||| 1 0.02020 0.07847 0.36788 3 -0.00000 -0.00000

at the pipes and output those line whose 2nd and 3rd argument are different.

  • 2
    A quick start: sed -n '/^[^|]\+|||\(.\+\?|||\)\1/p', grep '^[^|]\+|||\(.\+\?|||\)\1' or awk -F'\\|\\|\\|' '$2==$3'. (Anybody is free to expand it into an answer.)
    – manatwork
    Commented May 8, 2013 at 6:45

2 Answers 2


Some explanations of the @manatwork comment/answer:

  • grep version: grep '^[^|]\+|||\(.\+\?|||\)\1'

    • ^: match the beginning of the line
    • [^|]: match any character excepted |
    • \+: match one or more time the preceding pattern
    • |||: match your separator
    • \( and \): save the value matched between parenthesis in \1
    • .: match any character
    • \+\?: one or more time, but non greedy
    • |||: match your separator again
    • \1: match the previously text seen between parenthesis

    The idea here is to skip the beginning of the line until the first separator, then to save the value found until the end of second separator, and then match only line having exactly the same value as \1 after the second operator (meaning the 3rd field)

  • sed version: sed -n '/^[^|]\+|||\(.\+\?|||\)\1/p'

    Same meaning as grep, with an additional / at begin and end of line to indicate the pattern, plus an extra command character p at the end to print matching lines.

  • awk version: awk -F'\\|\\|\\|' '$2==$3'

    • -F'\\|\\|\\|': indicate the field separator: ||| escaped in this case
    • '$2==$3': filter input only where second and third field are equals

I prefer the awk solution, but if you want to use Bash only, here is my answer:

    local filename="$1";
    [[ $filename ]] || return 1

    while read -r line; do
        l="${line#*|||}"; a2="${l%%|||*}"
        l="${l#*|||}"; a3="${l%%|||*}"
        [[ $a2 = $a3 ]] && echo "$line"
    done < "$filename"

Usage: foo filename.txt

Sample output:

rany$ cat > filename.txt

rany$ foo filename.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .