Sponsored Content
Top Forums Programming substituting one string for another Post 302227535 by cleopard on Thursday 21st of August 2008 11:13:43 AM
Old 08-21-2008
substituting one string for another

I have a Linux C program I'm writing that has one section where, within a large string, I need to substitute a smaller string for another, and those probably won't be the same size.

For instance, if I have a string:

"Nowisthetimeforallgoodmen"

and I want to substitute 'most' for 'all' the main string will increase in size:

"Nowisthetimeformostgoodmen"


or a substitution that will increase the size of the main string:

"Nowisthetimefornogoodmen"


I thought I might get some opinions about what might be the best approach on something like this, besides some more cumbersome way of doing it. I've been looking at the C string functions to see if there's one that might be helpful, but I don't really see any.

Any help would be appreciated.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

substituting shell variables

I have a script run_batch.sh as below :- PAR_VALIDATION=val_siconf PAR_RUN_LEVEL=1 PAR_EXCLUSIVE_RUN_YN=Y DATABASE_USER="/@"$TWO_TASK sqlplus -s $DATABASE_USER |& print -p -- 'set feed off pause off pages 0 head off veri off line 500' print -p -- 'set term off time off... (1 Reply)
Discussion started by: suds19
1 Replies

2. Shell Programming and Scripting

Substituting with value of variable in Sed

Hi, I have a program in which i have to substitute a TAG in a file with the value of a variable. Code Snippet: ---------------- myvar=1234 sed 's/_TAG_/$myvar/' infile outfile When I run this command, the _TAG_ in the "infile" is substituted with "$myvar" but NOT the value "1234"... (1 Reply)
Discussion started by: jyotipg
1 Replies

3. Shell Programming and Scripting

substituting

Hello please how can i change this infrormation within a file dynamically without using vi " || $6 ~ /^229*/ " the * means any number within the file has this content : cat lec|awk -F '|' 'length($6) >= 12 || length($6) <= 10' |awk -F '|' '$6 ~ /^24/ || $6 ~ /^22924/ &&$7 ~... (1 Reply)
Discussion started by: neyo
1 Replies

4. Shell Programming and Scripting

Substituting Characters using SED

Can SED be used to substitute a character (y) with a character (Y) in a specified field? File has 12000 : delimeted rows as; HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT HHC 2 BDE:Lastname, Firstname MI:Firstname.MI.Lastname@mil:SGT I wish to replace the capital letters... (6 Replies)
Discussion started by: altamaha
6 Replies

5. Shell Programming and Scripting

Substituting the values

Hi Gurus this is working finee with tested values #!/bin/ksh V_DATE="2007-11-30" V_ID=789 V_NAME="john_${V_ID}_has_${V_DATE}_s" FILE_NAME=`echo ${V_NAME}` echo ${FILE_NAME} Buttt the problem is the first two values will come dynamically and the file will looks like... (2 Replies)
Discussion started by: SeenuGuddu
2 Replies

6. Shell Programming and Scripting

issue with substituting using sed

have a fileA containing about 260 lines wherein i have to match 2 lines fileA blah blah OF 90 DAYS DOCS PERIOD 12/06/0" Pairs_Id 52006 Amount1 -300000.0 Amount2 15091500.10 Codifiers_Id 0 OriginalId 0 EOT --blah blah blah TBL Tradt_IN CardRate 0.0 hashAmount -15091500.0... (2 Replies)
Discussion started by: sunnyboy
2 Replies

7. UNIX for Dummies Questions & Answers

substituting variable value in AWK

Hi All, my requirement is as below. I need to replace a value in a particular column with a substitution variable(date value) and modified value of the current column value in the same position. for ex. i have a record like 02;aaaa;bbbbb;cccccc;dddddd;123456789;hhhhh;12hs;asdf ;... (3 Replies)
Discussion started by: ganesh_248
3 Replies

8. UNIX for Dummies Questions & Answers

Variable is not substituting values

Hi All, OS HPUX 11.11 I am using following script to take controlfile backup. I have used SID variable to hold "ffin1" value, which I again subsitute in "'/db/ffin1/home/oraffin1/$SID_$wdate.ctl'" command. Well, after running this, SID variable does not subsittue it's value, while wdate... (6 Replies)
Discussion started by: alok.behria
6 Replies

9. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

10. Shell Programming and Scripting

Nawk sub not substituting

I am trying to use nawk sub to substitute a string in a file. Both the pattern and the replacement I set as variables using bash. here is the code: #!/bin/bash -x ydate=`/usr/local/bin/date +%Y%m%d` echo $ydate test_ca=/home/mdadmin/test_ca for i in `cat ${test_ca}` do if ]; then... (9 Replies)
Discussion started by: smenago
9 Replies
regsub(3tcl)						       Tcl Built-In Commands						      regsub(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
regsub - Perform substitutions based on regular expression pattern matching SYNOPSIS
regsub ?switches? exp string subSpec ?varName? _________________________________________________________________ DESCRIPTION
This command matches the regular expression exp against string, and either copies string to the variable whose name is given by varName or returns string if varName is not present. (Regular expression matching is described in the re_syntax reference page.) If there is a match, then while copying string to varName (or to the result of this command if varName is not present) the portion of string that matched exp is replaced with subSpec. If subSpec contains a "&" or "", then it is replaced in the substitution with the portion of string that matched exp. If subSpec contains a " ", where n is a digit between 1 and 9, then it is replaced in the substitution with the portion of string that matched the n'th parenthesized subexpression of exp. Additional backslashes may be used in subSpec to prevent special inter- pretation of "&", "", " " and backslashes. The use of backslashes in subSpec tends to interact badly with the Tcl parser's use of back- slashes, so it is generally safest to enclose subSpec in braces if it includes backslashes. If the initial arguments to regsub start with - then they are treated as switches. The following switches are currently supported: -all All ranges in string that match exp are found and substitution is performed for each of these ranges. Without this switch only the first matching range is found and substituted. If -all is specified, then "&" and " " sequences are handled for each sub- stitution using the information from the corresponding match. -expanded Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as speci- fying the (?x) embedded option (see the re_syntax manual page). -line Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag, "[^" bracket expressions and "." never match newline, "^" matches an empty string after any newline in addition to its normal function, and "$" matches an empty string before any newline in addition to its normal function. This flag is equivalent to specifying both -linestop and -lineanchor, or the (?n) embedded option (see the re_syntax manual page). -linestop Changes the behavior of "[^" bracket expressions and "." so that they stop at newlines. This is the same as specifying the (?p) embedded option (see the re_syntax manual page). -lineanchor Changes the behavior of "^" and "$" (the "anchors") so they match the beginning and end of a line respectively. This is the same as specifying the (?w) embedded option (see the re_syntax manual page). -nocase Upper-case characters in string will be converted to lower-case before matching against exp; however, substitutions specified by subSpec use the original unconverted form of string. -start index Specifies a character index offset into the string to start matching the regular expression at. The index value is interpreted | in the same manner as the index argument to string index. When using this switch, "^" will not match the beginning of the line, and A will still match the start of the string at index. index will be constrained to the bounds of the input string. -- Marks the end of switches. The argument following this one will be treated as exp even if it starts with a -. If varName is supplied, the command returns a count of the number of matching ranges that were found and replaced, otherwise the string after replacement is returned. See the manual entry for regexp for details on the interpretation of regular expressions. EXAMPLES
Replace (in the string in variable string) every instance of foo which is a word by itself with bar: regsub -all {mfooM} $string bar string or (using the "basic regular expression" syntax): regsub -all {(?b)<foo>} $string bar string Insert double-quotes around the first instance of the word interesting, however it is capitalized. regsub -nocase {yinterestingy} $string {"&"} string Convert all non-ASCII and Tcl-significant characters into u escape sequences by using regsub and subst in combination: # This RE is just a character class for almost everything "bad" set RE {[][{};#\$ u0080-uffff]} # We will substitute with a fragment of Tcl script in brackets set substitution {[format \\u%04x [scan "\&" %c]]} # Now we apply the substitution to get a subst-string that # will perform the computational parts of the conversion. Note # that newline is handled specially through string map since # backslash-newline is a special sequence. set quoted [subst [string map { {\u000a}} [regsub -all $RE $string $substitution]]] SEE ALSO
regexp(3tcl), re_syntax(3tcl), subst(3tcl), string(3tcl) | KEYWORDS
match, pattern, quoting, regular expression, substitute Tcl 8.3 regsub(3tcl)
All times are GMT -4. The time now is 10:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy