LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2012, 09:19 AM   #1
mirzasoltan
LQ Newbie
 
Registered: Oct 2010
Posts: 28

Rep: Reputation: 0
count characters


hi all,
i have a problem with my script, i want to know count a special character per line, for example in this line> 1/2/3/4/5 i want to know how many "/" is. in other way i have a text file that include just one line:

#cat mytextfile
1/2/3/4/5

i want to find a way to count how many / is in that text file.
tanx everyone.
 
Old 10-24-2012, 09:38 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,832
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
At first I was going to do it in awk. But then I kept it simple and used sed to strip all characters that aren't the special character you're looking for.

Code:
echo "1/2/3/4/5" | sed 's/[^/]//g' | wc -m
 
1 members found this post helpful.
Old 10-24-2012, 09:48 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Using grep and wc:
Code:
grep -o "/" mytextfile | wc -l
Code:
$ cat mytextfile
1/2/3/4/5
$ grep -o "/" mytextfile | wc -l
4
 
1 members found this post helpful.
Old 10-24-2012, 10:55 AM   #4
mirzasoltan
LQ Newbie
 
Registered: Oct 2010
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sag47 View Post
At first I was going to do it in awk. But then I kept it simple and used sed to strip all characters that aren't the special character you're looking for.

Code:
echo "1/2/3/4/5" | sed 's/[^/]//g' | wc -m
tanx for the answer, but i find a better way,

cat mytextfile | tr -cd "/" | wc -c
 
Old 10-24-2012, 10:57 AM   #5
mirzasoltan
LQ Newbie
 
Registered: Oct 2010
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Using grep and wc:
Code:
grep -o "/" mytextfile | wc -l
Code:
$ cat mytextfile
1/2/3/4/5
$ grep -o "/" mytextfile | wc -l
4
tanx, your way is useful too.
 
Old 10-24-2012, 11:15 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Code:
awk -F/ '{print NF-1}' file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk count characters of a row tomasdepomas Linux - General 7 07-16-2012 08:23 AM
[SOLVED] find, print and count specific characters after string criteria Alkass Linux - Newbie 2 05-03-2011 10:36 AM
bash debuging how do I count characters monsteriname Programming 6 10-06-2010 09:15 AM
Command to count all characters in file. guif Linux - Newbie 4 03-26-2010 09:44 AM
characters count in a string powah Programming 3 07-19-2007 03:03 PM


All times are GMT -5. The time now is 04:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration