nnani
1
Hello All,
I want to check if a delimiter is existing twice in a line of a text file.
Suppose flat file is like this
234 | 123
123 | 345
456 | 563 |
234 | 548
So the the 3rd line has two delimiters,
How can we find the lines in such a file having more then one delimiters
I tried with
cat sample.txt | grep -c '|'
but this gives me the count of total number of delimiters.
Any help appreciated
millan
2
You can use below code.
awk '/.*\|.*\|/ {print}' sample.txt
awk -F"|" '{print "Line "NR" Contains "NF-1" | Character(s)"}' file
Line 1 Contains 1 | Character(s)
Line 2 Contains 1 | Character(s)
Line 3 Contains 2 | Character(s)
Line 4 Contains 1 | Character(s)
nnani
5
That works good,
Thanks a lot.
How can I count number of pipes '|' on each line in my text file.
Yoda
6
To get count of pipes:
awk '{n=gsub(/\|/,"&"); print n}' file
To print records with more than one pipe:
awk '{n=gsub(/\|/,"&"); if (n > 1) print $0}' file