Sponsored Content
Full Discussion: String between quotes
Top Forums Shell Programming and Scripting String between quotes Post 302636941 by rbatte1 on Tuesday 8th of May 2012 06:59:22 AM
Old 05-08-2012
If you are in ksh then you can slice the strings with the following:-
Code:
#!/bin/ksh

MYSTRING="a b c d e"            # Quoting to set the variable only. Quotes do not form part of string
TEMPVAR="${MYSTRING#*\"}" # Cut off everything before first quote - no effect
OUTPUT="${TEMPVAR%\"*}"   # Cut off everything after last quote - no effect
echo $OUTPUT

MYSTRING="a b \"c\" d e"            # Escaped quotes do form part of string
TEMPVAR="${MYSTRING#*\"}" # Cut off everything before first quote
OUTPUT="${TEMPVAR%\"*}"   # Cut off everything after last quote
echo $OUTPUT

The last criteria is a bit awkward though. The output you suggest ignores the fact that your string may have embedded quotes in it. Do you not want to know these exist?

If not, then append | tr -d "\"" to the echo $OUTPUT statement.



I hope that this helps.

Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 05-08-2012 at 08:00 AM.. Reason: I forgot the signing off bit
 

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

String concat that keeps quotes

Hi All, I hope you can help. i am concatenating String variables using the following method. command="$command$x" i have created a script which takes a set of args passed to the script using the $* #example script args=$* count=0 for x in $args do count=`expr $count + 1` ... (8 Replies)
Discussion started by: duke
8 Replies

3. Shell Programming and Scripting

Removing back quotes from string in CSH

Hello, I am using csh to read a text file and save its words into variable $word in a foreach loop. These words have small back quotes ` as integral parts of them, for example, one word would be `abc`, another would be `xyz1` etc... These quotes are always the first and last characters of the... (5 Replies)
Discussion started by: aplaydoc
5 Replies

4. Shell Programming and Scripting

Add double quotes around the string

I have a line in multiple scripts:select into table /dir1/dir2/file.dat dir1 and dir2 are the same but file.dat is different from script to script. I need to include /dir1/dir2/file.dat into double quotes in each file of my directory:select into table "/dir1/dir2/file.dat" (13 Replies)
Discussion started by: surfer515
13 Replies

5. Shell Programming and Scripting

need to enclose a string in quotes

I have a script which I call and pass a text string to it. This string is then is assigned to a variable in the script. I then call another script and pass that variable to the second script, but when I do, the quotes are lost and the second script gets a total of three variables 'my', 'lovely' and... (3 Replies)
Discussion started by: iskatel
3 Replies

6. Shell Programming and Scripting

Get string between quotes separate by commas

I'm a beginner with shell and tried to do this per hours and everytinhg gives different want i do. So I have a lot of file in *.csv ( a.csv, b.csv ...) in each file csv , it has some fields separeted by commas. ----- "joseph";"21","m";"groups";"j.j@gmail.com,j.j2@hotmail.com"... (6 Replies)
Discussion started by: flaviof
6 Replies

7. Shell Programming and Scripting

How to find a string with double quotes?

I have thousands of files in a directory. I need to find/list all files that have the below matching string - RETURNCODE: "1017" Thank you! (5 Replies)
Discussion started by: esmgr
5 Replies

8. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

9. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

10. Shell Programming and Scripting

Adding double quotes at the end of string

My input is this: Inputfile = file.txt needs to change to, Inputfile = file.txt" I have tried using: Inputfile = `echo ${Inputfile}"' doesn't work for me. Similarly how do I change it to all double quotes: Inputfile = "file.txt" (4 Replies)
Discussion started by: bvnprasad123
4 Replies
String::Formatter::Cookbook(3pm)			User Contributed Perl Documentation			  String::Formatter::Cookbook(3pm)

NAME
String::Formatter::Cookbook - ways to put String::Formatter to use VERSION
version 0.102082 OVERVIEW
String::Formatter is a pretty simple system for building formatting routines, but it can be hard to get started without an idea of the sort of things that are possible. BASIC RECIPES
constants only The simplest stringf interface you can provide is one that just formats constant strings, allowing the user to put them inside other fixed strings with alignment: use String::Formatter stringf => { input_processor => 'forbid_input', codes => { a => 'apples', b => 'bananas', w => 'watermelon', }, }; print stringf('I eat %a and %b but never %w.'); # Output: # I eat apples and bananas but never watermelon. If the user tries to parameterize the string by passing arguments after the format string, an exception will be raised. sprintf-like conversions Another common pattern is to create a routine that behaves like Perl's "sprintf", but with a different set of conversion routines. (It will also almost ceratinly have much simpler semantics than Perl's wildly complex behavior.) use String::Formatter stringf => { codes => { s => sub { $_ }, # string itself l => sub { length }, # length of input string e => sub { /[^x00-x7F]/ ? '8bit' : '7bit' }, # ascii-safeness }, }; print stringf( "My name is %s. I am about %l feet tall. I use an %e alphabet. ", 'Ricardo', 'ffffff', 'abcchdefghijklmnn~opqrrrstuvwxyz', ); # Output: # My name is Ricardo. I am about 6 feet tall. I use an 8bit alphabet. Warning: The behavior of positional string replacement when the conversion codes mix constant strings and code references is currently poorly nailed-down. Do not rely on it yet. named conversions This recipe acts a bit like Python's format operator when given a dictionary. Rather than matching format code position with input ordering, inputs can be chosen by name. use String::Formatter stringf => { input_processor => 'require_named_input', string_replacer => 'named_replace', codes => { s => sub { $_ }, # string itself l => sub { length }, # length of input string e => sub { /[^x00-x7F]/ ? '8bit' : '7bit' }, # ascii-safeness }, }; print stringf( "My %{which}s name is %{name}s. My name is %{name}l letters long.", { which => 'first', name => 'Marvin', }, ); # Output: # My first name is Marvin. My name is 6 letters long. Because this is a useful recipe, there is a shorthand for it: use String::Formatter named_stringf => { codes => { s => sub { $_ }, # string itself l => sub { length }, # length of input string e => sub { /[^x00-x7F]/ ? '8bit' : '7bit' }, # ascii-safeness }, }; method calls Some objects provide methods to stringify them flexibly. For example, many objects that represent timestamps allow you to call "strftime" or something similar. The "method_replace" string replacer comes in handy here: use String::Formatter stringf => { input_processor => 'require_single_input', string_replacer => 'method_replace', codes => { f => 'strftime', c => 'format_cldr', s => sub { "$_[0]" }, }, }; print stringf( "%{%Y-%m-%d}f is also %{yyyy-MM-dd}c. Default string is %s.", DateTime->now, ); # Output: # 2009-11-17 is also 2009-11-17. Default string is 2009-11-17T15:35:11. This recipe is available as the export "method_stringf": use String::Formatter method_stringf => { codes => { f => 'strftime', c => 'format_cldr', s => sub { "$_[0]" }, }, }; You can easily use this to implement an actual stringf-like method: package MyClass; use String::Formatter method_stringf => { -as => '_stringf', codes => { f => 'strftime', c => 'format_cldr', s => sub { "$_[0]" }, }, }; sub format { my ($self, $format) = @_; return _stringf($format, $self); } AUTHORS
o Ricardo Signes <rjbs@cpan.org> o Darren Chamberlain <darren@cpan.org> COPYRIGHT AND LICENSE
This software is Copyright (c) 2010 by Ricardo Signes <rjbs@cpan.org>. This is free software, licensed under: The GNU General Public License, Version 2, June 1991 perl v5.10.1 2010-10-19 String::Formatter::Cookbook(3pm)
All times are GMT -4. The time now is 10:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy