LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 08: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, 08:38 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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, 08:48 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09: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, 09: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, 10:15 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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


Reply



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 07:23 AM
[SOLVED] find, print and count specific characters after string criteria Alkass Linux - Newbie 2 05-03-2011 09:36 AM
bash debuging how do I count characters monsteriname Programming 6 10-06-2010 08:15 AM
Command to count all characters in file. guif Linux - Newbie 4 03-26-2010 08:44 AM
characters count in a string powah Programming 3 07-19-2007 02:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:18 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
Open Source Consulting | Domain Registration