Counting number of single quotes in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting number of single quotes in a string
# 8  
Old 01-11-2015
Quote:
Originally Posted by vgersh99
Code:
echo "${STRING}" | awk -F"'" '{print NF-1}'

Yes, but a string does not necessarily consist of a single line (although it does in the example).

With awk one could try:
Code:
awk 'END{print NR-1}' RS=\'

Probably safer to add them all up:
Code:
awk -F\' '{t+=NF-1} END{print t}'


Last edited by Scrutinizer; 01-11-2015 at 01:33 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 01-11-2015
Hi bakunin...

;oD

In my defence I was refering to the builtin commands not transient ones from the external storage source, (HDD).

As in my post very recently about the limitations of the default Android problem(s) how would I pipe to grep, awk, sed or anything else for the same problem?

Anyhow your info is much appreciated and logged inside the OLD grey matter... ;oD
This User Gave Thanks to wisecracker For This Post:
# 10  
Old 01-11-2015
Quote:
Originally Posted by SkySmart
i need to be able to count the number of single quotes
Code:
 '

in the entire string below:

Code:
"description":"DevOps- Test VM's, System Admins Test VM's ",

awk can most likely do this, but here's my attempt using egrep:

Code:
echo "${STRING}" | egrep -wc '"'"\'"'"'

or

Code:
echo "${STRING}" | egrep -wc ".*\'"

both of which do not even come close to doing what i want.
Hello skysmart,

Following may also help you in same.
Code:
awk -vs1="'" '{n=gsub(s1,X,$0);print n}'  Input_file
OR
awk -vs1="'" '{n+=gsub(s1,X,$0)} END{print n}'  Input_file (In case you need total number including all lines)

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-11-2015 at 01:34 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 11  
Old 01-11-2015
Quote:
Originally Posted by wisecracker
In my defence I was refering to the builtin commands not transient ones from the external storage source, (HDD).
I have understood that. The problem is the following: when the shell reads and executes shell code there are three possible ways to do that:

1) builtins: the shell executes them itself. This means there is a provision inside the shells binary code which gets executed. This is the case with builtins, reserved words (which is not the same, but for our purposes equal), variable expansion and similar syntactical devices. Into this group falls "if", "do", "while", "[[" (test), "((...))", etc..

2) externals: the shell simply loads and executes them. A separate process (child process) is created for every such binary.

3) process substitution/subshells: This is similar to externals but first a subshell is created as a child process and then the command is executed within this subshell. $( command1 [ | command2 | ... ] ) means: create a child process and start a shell there, then execute the shell code command1 [ | command2 | ... ] there, applying the same rules as if it where a one-line script. If command1 is a builtin it is processed like any builtin, but not from the original shell, though! It is processed by the child process. All this happens during commandline evaluation (of the main shell). Finally the output of the child shell is collected, the "$( ... )" is replaced by this collected output and the resulting line is executed.

You see, the process substitution involves the same process creation (exec(), ...) as the external do. Even more so when the commands in the subshell are externals themselves.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 12  
Old 01-12-2015
Hi, for fun, builtin bash:
Code:
$ string="\"description\"":"\"DevOps- Test VM's, System Admins Test VM's \"",
$ echo $string 
"description":"DevOps- Test VM's, System Admins Test VM's ",
$ XX=${string//[^\']/}
$ echo ${#XX}
2

These 4 Users Gave Thanks to disedorgue For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

2. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

3. Shell Programming and Scripting

Counting number of records with string row delimiter

HI, i have a file like this t.txt f1|_f2|_ f1|_f2|_ f1|_f2|_ as if col delimiter is |_ and row delimiter |_\n trying to count number of records using awk $ awk 'BEGIN{FS="|_" ; RS="~~\n"} {n++}END{print n} ' t.txt 7 wondering how can i count this to 3 ? thx (9 Replies)
Discussion started by: aksforum
9 Replies

4. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

5. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

6. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

7. UNIX for Dummies Questions & Answers

Add single quotes in string

Hi All, I love this site, it helps newbie people like me and I appreciate everyone's help! Here is my questions. I am trying to concatenate a single quote into a character/string from a text file for each line (lets say ABC should look like 'ABC'). I tried to use awk print command to do... (1 Reply)
Discussion started by: mrjunsy
1 Replies

8. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

9. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. Shell Programming and Scripting

problem with single quotes in a string and findbug

I'm having trouble manipulating a string that contains single quotes (') in it. I'm writing a ksh script to parse in a few queries from a config file, such as this: findbug \(\(Project 'in' "Deployment,HDRCI,LHS,LSS,WUCI" '&&' Status 'in' "N" '&&' New_on 'lessthan' "070107" \)\) '&&' \(Class... (9 Replies)
Discussion started by: bob122480
9 Replies
Login or Register to Ask a Question