Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Expanding variables with Ed Bash 3.2.33 Post 302339885 by de_la_espada on Friday 31st of July 2009 03:58:26 PM
Old 07-31-2009
Expanding variables with Ed Bash 3.2.33

Hi,

The following code finds the line containing fruits in test.txt and replaces instances of apple with banana.

Code:
   ed -s test.txt <<< $'/fruits/s/apple/banana/g\nw'

What I want to do is put variables in the place of fruits, apple and banana.

I have tried replacing ' with " to get the variables to expand, but to no avail.

Code:
ed -s accounts.txt <<< $"/$account/s/$balance/$newBalance/g\nw"

Also
Code:
ed -s accounts.txt <<< $"/${account}/s/${balance}/${newBalance}/g\nw"

And trying to echo them, but still no luck.


I'm using GNU bash, version 3.2.33(1)-release (i386-redhat-linux-gnu)
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

expanding dotted paths to absolute ones in bash or sh

I have a little script to help me manage a gallery of image files. It makes symbolic links to every file in and below the current directory, placing them in a target directory which is passed to the script as a parameter. Unfortunately, the script pukes when I pass a parameter that contains... (4 Replies)
Discussion started by: TanRanger
4 Replies

2. UNIX for Advanced & Expert Users

Expanding Variables in User Input

If have var='$variable' how can I expand $variable. I have tried many thing like duble quotes/braces etc, but nothing worked. I need the solution ASAP. (2 Replies)
Discussion started by: Bsk
2 Replies

3. Shell Programming and Scripting

Bash variables

Ummm can anybody help me with this one? Its prob quite simple. I bascially have a file name say J1x2x3x7.dat Im using the file name as a variable in a bash script. Want I want to do is extract most of the file name and make it a new variable expect with say one of the number now a... (2 Replies)
Discussion started by: RichieFondel
2 Replies

4. Shell Programming and Scripting

how to use in bash variables and quotes

I have some troubles with variables and quotes... I want: if $URL is empty (no user input) go to http://www.localhost/index.php/ else add this string (search) "?s=+$URL" EXAMPLE: No user input string= http://www.localhost/index.php/ User input = "unix" string=... (3 Replies)
Discussion started by: aspire
3 Replies

5. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

6. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

7. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

8. Shell Programming and Scripting

Possible ksh93 Bug Expanding Variables?

My OS is Linux (kernel 4.08.something) and AIX (7100-04-01-1543), the used ksh versions are: ksh88: Version M-11/16/88f (AIX) ksh93: Version M 93t+ 2009-05-01 (AIX), Version M 93u (Linux) When writing a parser for stanza files in ksh i encountered a rather strange behavior. Here is a... (4 Replies)
Discussion started by: bakunin
4 Replies

9. UNIX for Beginners Questions & Answers

Bash argument not expanding in script

I pass an argument to bash as run. The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the... (3 Replies)
Discussion started by: cmccabe
3 Replies

10. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

Hi Guys, I wrote a collection of bash functions years ago and now need to use them again but I'm getting some error messages when eval tries to expand the variables names. I recollect that I used the shopt command to set one of the options but I can't quite remember the command that I... (8 Replies)
Discussion started by: ASGR
8 Replies
SORT(3) 								 1								   SORT(3)

sort - Sort an array

SYNOPSIS
bool sort (array &$array, [int $sort_flags = SORT_REGULAR]) DESCRIPTION
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. PARAMETERS
o $array - The input array. o $sort_flags - The optional second parameter $sort_flags may be used to modify the sorting behavior using these values: Sorting type flags: o SORT_REGULAR - compare items normally (don't change types) o SORT_NUMERIC - compare items numerically o SORT_STRING - compare items as strings o SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale(3) o SORT_NATURAL - compare items as strings using "natural ordering" like natsort(3) o SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.4.0 | | | | | | | Added support for SORT_NATURAL and | | | SORT_FLAG_CASE as $sort_flags | | | | | 5.0.2 | | | | | | | Added SORT_LOCALE_STRING | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 sort(3) example <?php $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . " "; } ?> The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange The fruits have been sorted in alphabetical order. Example #2 sort(3) example using case-insensitive natural ordering <?php $fruits = array( "Orange1", "orange2", "Orange3", "orange20" ); sort($fruits, SORT_NATURAL | SORT_FLAG_CASE); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . " "; } ?> The above example will output: fruits[0] = Orange1 fruits[1] = orange2 fruits[2] = Orange3 fruits[3] = orange20 The fruits have been sorted like natcasesort(3). NOTES
Note This function assigns new keys to the elements in $array. It will remove any existing keys that may have been assigned, rather than just reordering the keys. Note Like most PHP sorting functions, sort(3) uses an implementation of Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays. This is however an implementation detail you shouldn't rely on. Warning Be careful when sorting arrays with mixed types values because sort(3) can produce unpredictable results. SEE ALSO
asort(3), The comparison of array sorting functions. PHP Documentation Group SORT(3)
All times are GMT -4. The time now is 10:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy