Programming :: Implement Variable Substitution In Strings?

May 6, 2011

I have an interpteter that supports string literals, and the way it works is that the lexer returns the entire string as a single token, with the quotes removed and escape sequences replaced with the literal characters they represent.

I already implemented single-quote strings, they don't interpret any characters specially except for the single quote. I partially implemented double-quoted strings, they already support all the same backslash escape sequences that C does. But I would also want to add variable substitution.

The way it would work is that "${expression}" would interpret the expression (which could just be a variable name) and replace itself with the result. But I have no idea how to do this.

In case it matters, I'm using a hand-written lexer and recursive-descent parser.

View 14 Replies


ADVERTISEMENT

Programming :: Variable Substitution In BASH?

Feb 17, 2011

Just a simple BASH for loop to read the file path from a text file (clean.txt) echo the variable for debug purposes, and scp it to a server I have using port 50 for SSH.

I've already formatted the entries in clean.txt to handle spaces correctly, using sed replacement.

Example from the clean.txt file:

Code:
/MP3/NAS000000001/Barenaked Ladies/Barenaked Ladies - Barenaked For The Holidays/20 Auld Lang Syne.mp3
/MP3/NAS000000001/Barenaked Ladies/Barenaked Ladies - Barenaked For The Holidays/14 Deck the Stills.mp3

[Code]....

View 5 Replies View Related

Programming :: SH - Recursive Variable Substitution

Jul 1, 2010

I have the following test code fragment:

Code:

When I try to run it 'sh' reports the following message^

Code:

You might understand what I mean with that test code so could you recommend how to achieve the required effect?

View 7 Replies View Related

Programming :: Variable Substitution In Sqlite Open Db Command?

Jun 15, 2010

I have the following TCL code:

if{ [catch {sqlite3 db /path/to/db/file} result] } {
puts stderr $result
} else {
do something
}

What I want is to use a variable for the file name/path. When I put in a variable instead of the absolute path, I get an error: "missing close-brace: possible unbalanced brace in comment while executing"

This is weird because when I run the code with the fully qualified pathname, it works fine. I substitute out the pathname for a variable containing the pathname ($variable) and I get this error.

View 1 Replies View Related

Programming :: Variable Strings In A Script?

Sep 5, 2009

I have a set of numbers under some headers in a file:

FNUM,BNUM,C1N,C2N,C3N,C1S,C2S,C3S,
4535, 109, 5.5955, 5.6523, 5.8084, 311.419, 295.2540, 286.9984,
4536, 109, 5.7667, 5.7174, 5.6771, 291.782, 285.0646, 271.5945,

[code]...

View 2 Replies View Related

General :: Sed Substitution Of Variable With A Variable?

Dec 27, 2010

I am trying to alter the character position of residue numbers above 999 in a pdb file.The following script is an attempt to:1) Get all unique pdb residue numbers (in column 5) using awk and assign it to a variable i.2) Loop through all the values in $i and if it is greater than 999, shift that number one character to the right using sed.However, the script only manages to alter the final residue numberCould anyone please advise how I can loop through all values in $i and shift it one character to the right?

#!/bin/bash
# Script to alter position of residue number in pdb file for resid above 999
i=$(awk '{print $5}' wt-test.pdb | uniq)

[code]...

View 6 Replies View Related

General :: Shell Script -- Awk Variable Substitution?

Oct 7, 2010

I have a script in which I'm reading a file line by line and I'm finding certain position value using awk index as

Code:
# 2 spaces before open parens
pos=`echo | awk -v Line="$Line" '{ print index("'"$Line"'"," (")}'`

[code]....

View 2 Replies View Related

Red Hat / Fedora :: How To Extract Certain Strings From A Variable

Mar 18, 2009

I have one question.

Assumed I have have a variable $userlist, it has the following contents:

brownj
smithk
smithk2
smithk3
hynesp
hynesp2
leeb

How can I write a bash script that extracts all the strings smithk, smithk2, smithk3, etc from variable $userlist, and assigns these strings to a new variable?

View 1 Replies View Related

Programming :: Use Socket Programming In Order To Implement Chatting Feature?

Aug 25, 2010

how to use socket programming in order to implement chatting feature

View 5 Replies View Related

Programming :: Bash - Read Content Of File To Variable And Use This Variable In For Loop ?

Aug 21, 2009

I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.

For example:

Code:

Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).

View 6 Replies View Related

Programming :: Bash - Why Is Process Substitution <() So Much Faster

Nov 25, 2010

file allids consists of 300,000 rows, each containing a 5-7 digit numeric id. file newids consists of 20,000 rows of id's. How do you explain the following timings? time: 0.07s:

Code:

diff <(sort allids) <(sort newids)
time: 1.6s:

[code].....

View 4 Replies View Related

Programming :: C Wrapper For Secure Password Substitution

Feb 3, 2011

I'm working on a program that will allow non-privileged users the ability to substitute passwords that are read in from a 600-root:root file.
A user might call it like this:
/bin/securepasswrapper "/path/to/check_ping -u Admin -p ?webserver1?"

The file (defined now as /tmp/securefile) will be searched for the identifier "webserver1", and it's corresponding password will be returned to securepasswrapper. Now, I know you could just call /bin/echo, and get the output, but that's in scope at this point.
File format of securefile is:
<identifierassword>
ex: webserver1:asdf123

Once the password is properly substituted in, securepasswrapper should then execute it as the UID/EUID of the calling user (not root)...that part I've not got to yet.
Code:
/* program: securepasswrapper
author: R.Briggs + Google
date: 02/02/11
purpose: C wrapper that replaces placeholders in a string with secure passwords retrieved from a file
*/ .....

View 5 Replies View Related

Programming :: Shell Scripting Array - Bad Substitution

Jun 19, 2009

I made a little rsync script for log transfer.

Code:
SERVERS=(SERVER1 SERVER2 SERVER3)
SERVER1_SERV=(web ftp mail)
SERVER2_SERV=(web transcoding)
SERVER3_SERV=(web ftp mail)
for SERVER in ${SERVERS[@]}
do
echo "Starting tranfer for server $SERVER"
for SERVICE in ${$SERVER_$SERVICE[@]}
do
something_to_be_done
fi
done
But when I run it I get ${$SERVER_$SERVICE[@]}: bad substitution

View 5 Replies View Related

Programming :: Make A New Variable With The String From The Old Variable Btut Without Any Plus Sign?

Apr 7, 2010

my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form) for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay.

i need to make a new variable with the string from the old variable btut without any plus sign. I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string. I think this should work but doesn't

foo=+12.40
bar=${foo#+}

View 4 Replies View Related

General :: Searching And Replacing Strings In File With Strings In Other Files

Jul 29, 2010

I want to search and replace strings in a file with strings in other files/i need to do it with big strings(string1 is big) and i want to use a txt file for this.But this code not working :

View 14 Replies View Related

Programming :: Sed Substitution - Doesn't Appear To Be Capable Of Resolving Variables

Dec 21, 2008

I have a simple script that I am writing to parse an XML file for me, and update some version numbers. Basically what happens in my script is that I'm running a grep command on the file to get the current version number, then I prompt for a new number, and then I want to globally replace that version number in the file. The problem is, the sed command seems to only want literal values (it doesn't appear to be capable of resolving variables). For example, this code doesn't work:

[Code]...

I have tried removing the curly brackets from the variable names, quoting them, etc. and nothing seems to work. Should I do this with awk instead (and if so can you kindly write out the command for me because I never bothered to learn awk).

View 2 Replies View Related

Programming :: Awk - Substitution With Parts Of Matched Regular Expression Used?

Apr 30, 2011

I remember reading that using sed, you can do this with parentheses: s/abc(something)def/(something)else/g I can't find an explanation of how to do something like this with Awk. Say you have this in an HTML file, where (number) stands for a one or two-digit number:

<sup>(number)</sup>

And you want to change that to this:

<a name="(number)t" href="#(number)b"><sup>(number)</sup></a>

How would this be possible? Would you have to use sed?

View 4 Replies View Related

Programming :: Sed - Match One Line Make A Substitution A Few Lines Down?

Jun 6, 2011

I need a substitution of a particular string (StringA) with another string (StringB). However, there may be more than one occurrence of StringA within the file, but only one instance needs to be changed, which is why I'm trying to be sure of it's positioning against something I know will be unique in the file, and will always have the same distance from the string to be replaced. So, I intend to match on a string (StringC) above the string to be substituted and then have sed go to StringA below and replace with StringB.

So far, I have had some success with the following:

Code:

... but I can't help thinking that there *has* to be a cleaner way of doing it.

View 6 Replies View Related

Programming :: Search Within A Variable And Assign The Results To A New Variable?

Apr 25, 2011

how I can search within a variable and assign the results to a new variable. I'll use the following as an example -

cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=`echo ${cars} | egrep -o '<A?+|<C+'`

with the echo command I get the following output assigned to list -

A
C
C

What I'd like to get for output is -

Audi
Cadillac
Chevy

how I could do this regardless of upper/lower case letters?

View 5 Replies View Related

Programming :: Bash: Functional Difference Between Process Substitution And Here String With $( )

Mar 14, 2010

Further to solved LQ thread Bash: how to populate a list of arbitrarily named files?, what is the functional difference between feeding a loop with process substitution and feeding it with a here string with embedded command substitution? ABSG pages: process substitution, here string and command substitution. This works

Code:

while IFS= read -r -d '' file
do
files+=("$file")
done < <(find $dir -type f -print0)

[code]....

View 2 Replies View Related

Programming :: Assign Value Of C Variable To Shell Variable?

Apr 28, 2010

included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it?

View 8 Replies View Related

Programming :: Bad Substitution Error - Stripping Unnecessary Word From Song Title

Dec 22, 2010

I can find points to Tillies Garrels TLDP reference. Do I want to create my own variable to hold the string or do I want to place the string in memory? Is that my problem? Or is something different causing the 'bad substitution' error.

[code]...

View 14 Replies View Related

Programming :: Using Sed To Change Strings?

Sep 3, 2010

I am having a lot of problems trying to change one string by another using sed: the sentence is like this:

sed -i 's/KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"/KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0666"/g' 50-udev.rules
it is just to fing the line with:
KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"

[code]....

View 8 Replies View Related

Programming :: C Truncating Strings

Dec 11, 2009

I wrote this small program that will truncate a string that's entered in by the user.An example of its usage:if the user enters in a string say "abcdefghijklmnopqrstuvwxyz" the program will only take the first 9 characters and truncate the rest so that the user can be prompted for a second string and not be worried about remaining characters left in the stream.Now this program works O.K. but I would like to find something in C that has this functionality build into it...Does anyone know of any function that will accomplish this.

View 1 Replies View Related

Programming :: Replace Txt Between 2 Strings?

Aug 4, 2010

I am trying to replace a section of a file between the first instances of the strings {}, with the contents of another file. Example of the format of the file I'm trying to modify

Code:

Servername=something.com
hosts {
macaddress1
macaddress2

[code].....

Then captured all the "macaddress#"s to a variable and used sed to swap

sed "s/$CURRENTDATA/$NEWDATA/" filename

However I get 1 of 2 errors,

Using a small number of macs in "$NEWDATA"

sed: command garbled: s/ macaddresshere

Or when using a large number of macs in the $NEWDATA variable get

bash: /usr/bin/sed: Arg list too long.

how to replace a large block of txt in one file with another large block from another?

View 6 Replies View Related

Programming :: Implement The OOP In Interpreter?

Mar 7, 2011

I have a partially completed interpreter that has first-calss functions and can store scopes in variables. That way it is possible to implement a simple class like this:

Code:

MyClass = func () {
value = 0;
setValue = func (x) value = x;;

[code]....

But what about inheritance, and what if I want to know the type of the object?

View 6 Replies View Related

Debian Programming :: Sh Command Strings

Feb 8, 2014

I use udhcp with some of my minimal installs. I've messed around with the code a bit when it wasn't working correctly - a few years ago. I will find time - I hope soonish - to figure out how to do a few other things with it.

For now though, I'm using this string to grab my ip after startup

Code: Select allsu -c 'ifconfig eth0&&ifdown -a&&ifup lo&&ifup eth0'&&/sbin/ifconfig eth0

I realize I could substitute ifup -a but I'm more interested in figuring out how to make ifup wait for the ip to become available if it is not available yet.

Never mind that one, just typing out the question answered it for me, when I find it in the scripting man ' ; : " & =

Or if there are any other suggestions for better construction of the string.

View 2 Replies View Related

Programming :: Extracting URLs From Strings?

Jun 28, 2009

I have a PHP script written that is checking a string to see if it contains a link in it (i.e. a URL). I have the following if statement, that uses 3 possible regular expressions to determine if there is a link or not.

Code:
// check if we found a link
// links are denoted by strings that:
// - contain http://
// - contain www.*.*

[Code]....

I'm not convinced yet that writing a shell script to do this is the best course of action. If someone is capable of doing this with a Perl or a Python script that's fine too. If you want to make it super high performance and write it in assembly

View 1 Replies View Related

Programming :: Best Command To Use To Parse Strings?

Mar 24, 2010

what is the best command to use to parse strings?I have a variable $str and need to parse this string.Can you provide an example of the command used to get a substring of $str based on the index values of start and end

View 3 Replies View Related

Programming :: Cannot Match Strings That Appear To Be Identical

Feb 25, 2011

i have an sql table with 2 columns i run a script that randomly selects a word from the table in column 1.
the word is displayed on the screen and I guess what it means i concatenate the randomly selected word and the answer the script looks for a match in mysql if it finds a match it says "Good job!" if there is no match it will say "not correct". However when i get it right it says not correct even though when i echo the variables they look exactly the same. the script below:

#!/bin/bash
var=$(mysql translator -u root --password=*-N<<EOF
SELECT word FROM tagalog ORDER BY RAND() LIMIT 1
EOF
)

[Code]....

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved