How to count number of occurrences of a "|" from a variable?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to count number of occurrences of a "|" from a variable?
# 1  
Old 05-06-2005
How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3"

I want to know how many "|" are in $var.

When I say echo $var | grep -c '|'

I am getting only 1 Smilie Smilie Smilie ?
# 2  
Old 05-06-2005
From the man page for grep
Quote:
-c Only a count of matching lines is printed.
It returns a line count not a character count.
# 3  
Old 05-06-2005
This seems to work:

Code:
echo $var | awk -F\| '{print NF-1}'

# 4  
Old 05-06-2005
voila Smilie It works
# 5  
Old 05-06-2005
Code:
echo "some1|some2|some3" | tr "|" "\n" | wc -l

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Capture the last record number using "awk" NR variable

Hi Team. I am trying to capture the last record number from a file using the below command ( assuming abc.txt has 21 records and I want 21 as output ) awk'{c=NR;print c}'abc.txt But it is printing all the record number. Can someone please help modify the above command? (8 Replies)
Discussion started by: chatwithsaurav
8 Replies

3. Shell Programming and Scripting

sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that? In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in... (11 Replies)
Discussion started by: p1ne
11 Replies

4. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

6. Shell Programming and Scripting

Show "uniq -c" results only for more than X occurrences

Say I have test.txt with the following data: user1 mailbox11 IP1 user1 mailbox12 IP2 user2 mailbox21 IP1 user3 mailbox31 IP1 user1 mailbox11 IP1 user1 mailbox11 IP1 user1 mailbox11 IP1 user1 mailbox12 IP2 user2 mailbox21 IP1 user2 mailbox21 IP1 user2 mailbox21 IP1 user2 mailbox21 IP1... (4 Replies)
Discussion started by: striker4o
4 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Sed - How to escape variable number of "/" (slash) ?

Hi, Can you tell me how to escape a variable number of slash characters in sed "/" ? In the script the code looks like this: cat $file_to_update | sed s/^$param/$param=$tab2*\#\*/1 And the $tab2 value is a path so it will have a number of "/" charracters. # cat db.cfg | sed... (4 Replies)
Discussion started by: majormark
4 Replies

9. Shell Programming and Scripting

How do "if [ $variable -eq "a number" ] ?

In particular I'm trying to check if a line contains an IP address and then do something with it. The file contains some lines that have no numbers, some blank lines, and some lines with only IP addresses. #!/bin/bash touch file2.txt cat file1.txt | \ while read line do if *.*.*.* ];... (4 Replies)
Discussion started by: earnstaf
4 Replies
Login or Register to Ask a Question