how to dereference a variable in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to dereference a variable in sed
# 1  
Old 01-28-2009
how to dereference a variable in sed

Hi
I have a 'sed' editor command trying to update a line in the file:

NEW_PX=7777

echo 'PX_LIST="2259 221 270 263 2874" ' | sed "s/PX_LIST="/&${NEW_PX} /"

The above command should add value of NEW_PX as the first entry inside the quotes, so the output should look like this:

PX_LIST="7777 2259 2215 270 263 2874"

The command works fine if the value 777 is hardcoded:
echo 'PX_LIST="2259 221 270 263 2874" ' | sed 's/PX_LIST="/&7777 /'

Thanks for any help and advice -A
# 2  
Old 01-28-2009
Code:
#!/bin/ksh

NEW_PX=7777

echo 'PX_LIST="2259 221 270 263 2874" ' | sed "s/PX_LIST=\"/&${NEW_PX} /"

# 3  
Old 01-28-2009
THANKS A LOT vgersh99 !!!! You saved me from parsing this string....... All The best! -A
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Kernel crash - NULL pointer dereference when calling DEVICE_WRITE from KTHREAD in a USB device drive

I'm writing a simple USB driver to drive a stepper motor based on USB Skeleton 2.2 Driver, kernel 3.8. The basic version is running properly. As a advancement, I introduced KTHREAD to call the DEVICE_WRITE (skel_write) (), so that the driver will be available for other tasks & requests. Calling... (0 Replies)
Discussion started by: miteshgaware
0 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

4. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

5. Shell Programming and Scripting

using variable in sed

i need to use a value in the Variable to print a particular line from a file using sed command. i tried the below one but its is not working sed -n ' "$var"p ' abc.txt but its is not working please help me to sort out this. (3 Replies)
Discussion started by: Kochu77
3 Replies

6. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

7. Shell Programming and Scripting

want to use variable in sed

hello i have following sed command works fine. sed -n 's/.*-L\(*\)myproject.*/\1/p' makefile > m here search words are -L and myproject now i want to replace these words by variables like var1="-L" var2="myproject" so the command would be like this sed -n... (3 Replies)
Discussion started by: shailesh_arya
3 Replies

8. Shell Programming and Scripting

how to dereference the variable in sed editor

Hi I am trying to do the substitution using the 'sed' editor. In the line below I am trying to substitute all instances of XXX by the value of the variable PX sed 's/XXX/${PX}/g' ${TEMPLETE} > ${TEMPLETE}.${PX} The problem is that 'sed' editor takes ${PX} literary (without retrieving the... (2 Replies)
Discussion started by: aoussenko
2 Replies

9. Shell Programming and Scripting

How can I use a variable in sed?

Hi I'm trying to change a part of a line with sed. Usually I will run sed 's/mytext/mynewtext/' Now I have a variable: var=mynewtext sed 's/mytext/$var/' does not work. I have also tried to protect the $ with different characters but it still does'nt work. I will be very happy if... (2 Replies)
Discussion started by: tromag
2 Replies
Login or Register to Ask a Question