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

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

bash - Shell scripting to find the delimiter

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...