LinuxQuestions.org
Help answer threads with 0 replies.
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 05-17-2012, 04:19 AM   #1
bayanaa
LQ Newbie
 
Registered: May 2012
Posts: 13

Rep: Reputation: Disabled
checking a string


Hello for all

How can I check if a string has a separator among the characters like backspace ' ' or ','

My best regards
 
Old 05-17-2012, 04:32 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
please specify at least a language, what is the separator...
Otherwise you will get an answer like this: just read it!
 
Old 05-17-2012, 04:39 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!

If you want to check for punctuation or spaces characters in general, you can use grep with the -E option and specify the proper character classes, e.g.
Code:
$ echo string | grep -E -q '[[:punct:]]|[[:space:]]' && echo yes || echo no
no
$ echo str,ing | grep -E -q '[[:punct:]]|[[:space:]]' && echo yes || echo no
yes
just to give you an idea. If you want to check for specific characters only, include them in a character list and eventually use the shell octal notation for special or control characters. For example to match a single backspace or space or comma:
Code:
$ echo string | grep -E $'\010''|[ ,]'
The part in blue is the shell notation for octal codes and it must be left outside the quotes that embed the rest of the grep pattern. Hope this helps.
 
Old 05-17-2012, 08:20 AM   #4
bayanaa
LQ Newbie
 
Registered: May 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Amazing
Thanks a lot

You've mentioned punct as a name of ,
But, what is the name of : symbol

again thank you a lot

Quote:
Originally Posted by colucix View Post
Hi and welcome to LinuxQuestions!

If you want to check for punctuation or spaces characters in general, you can use grep with the -E option and specify the proper character classes, e.g.
Code:
$ echo string | grep -E -q '[[:punct:]]|[[:space:]]' && echo yes || echo no
no
$ echo str,ing | grep -E -q '[[:punct:]]|[[:space:]]' && echo yes || echo no
yes
just to give you an idea. If you want to check for specific characters only, include them in a character list and eventually use the shell octal notation for special or control characters. For example to match a single backspace or space or comma:
Code:
$ echo string | grep -E $'\010''|[ ,]'
The part in blue is the shell notation for octal codes and it must be left outside the quotes that embed the rest of the grep pattern. Hope this helps.
 
Old 05-17-2012, 08:35 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually [:punct:] is the name of a character class in the POSIX standard. It comprises all the punctuation characters. Here is a complete list.
 
Old 05-18-2012, 08:02 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I noticed you never answered pan64's questions. If you would provide more details about what you are doing, there may be better options than grep. The shell itself can easily check for the existence of characters in a string, for example:

Code:
string='foo,bar,baz'

# if bash or ksh:

if [[ $string == *,* ]]; then
	echo 'contains commas'
else
	echo "doesn't contain commas"
fi

# fully portable version

case $string in

	*,*) echo 'contains commas' ;;

	  *) echo "doesn't contain commas" ;;

 esac

Last edited by David the H.; 05-18-2012 at 08:03 AM. Reason: wayward code tag
 
  


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] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
SH Prg: Checking if a string exists in a file ignignokt Programming 6 12-14-2006 01:31 PM

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

All times are GMT -5. The time now is 06:10 PM.

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