Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a file with three columns, which has pipe as a delimiter. Now some lines in the file can have a "," instead of "|", due to some error. I want to output all such erroneous rows.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
402 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can also use grep, it is more complicated:

egrep "|.*|.*|" input
echo No pipe
egrep "^[^|]*$" input
echo One pipe
egrep "^[^|]*|[^|]*$" input
echo 3+ pipe
egrep "|[^|]*|[^|]*|" input

Before combining the greps, first introduce new variables p (pipe) and n (no pipe)

p="|"
n="[^|]*"
echo "p=$p, n=$n"
echo No pipe
egrep "^$n$" input
echo One pipe
egrep "^$n$p$n$" input
echo 3+ pipe
egrep "$p$n$p$n$p" input

Now bring all together

egrep "^$n$|^$n$p$n$|$p$n$p$n$p" input

Edit: The comments and variable names were about "slashes", but they are pipes (with backslashes). That was a bit confusing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...