LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Insert character into a line with sed? & variables in sed? (http://www.linuxquestions.org/questions/programming-9/insert-character-into-a-line-with-sed-and-variables-in-sed-155831/)

jago25_98 03-10-2004 09:21 AM

Insert character into a line with sed? & variables in sed?
 
I don't really want to have to write to a file.

I need to insert a `0` into "0x0123453" between the x and the 2nd `0`.

This is the closest thing to help I've found:

# insert 5 blank spaces at beginning of each line (make page offset)
$ sed 's/^/ /'

But this is using [s]ubsitute rather than [i]nsert?

Also,

I've tried to delete by using the output from a variable:

$ export VARIABLE=0x012345
$ echo "0x012345 0x042315" | sed -e 'd/$VARIABLE'

-> my goal in this example would be "0x042315"

Is it possible to use an external variable like this? I need to correct the syntax somehow?


Thanks if you can help with either problem. I've found a real lack of documentation that covers insert of use of variables, the docs seem to contain many examples, all except the ones I need!

jim mcnamara 03-10-2004 09:29 AM

Try:
Code:

cat somefile | sed 's/x0/x00/' > tmpfile
# you like the result then do this:
mv tmpfile somefile


druuna 03-10-2004 09:37 AM

You can use the s for inserting.

sed 's/x/x0/' : the x is replaced by x0. So the following does what you want:

$ echo "0x012345" | sed 's/x/x0/'

You can use variables inside sed, but the quoting is important:

$ VARIABLE=0x012345
$ echo "0x012345 0x042315" | sed "s/$VARIABLE//"


You need to use double quotes.

Why use the export?? That is only needed if you want to make the variable known in all other shells (which has the term were the command was typed as 'mother').

Hope this helps.

jago25_98 03-10-2004 09:41 AM

I only used export because I'm testing it with the command line. But I can see that's not actually needed.

Thanks for the help! :)

mfeat 03-10-2004 09:43 AM

"I've tried to delete by using the output from a variable:
$ export VARIABLE=0x012345
$ echo "0x012345 0x042315" | sed -e 'd/$VARIABLE'"
______________________________________________

"d" is to delete an entire line use "s" instead, also use double quotes:

$ export VARIABLE=0x012345
$ echo "0x012345 0x042315" | sed "s/$VARIABLE//"

jago25_98 03-11-2004 06:12 AM

resulting script so far
 
just for interests sake here's what I've managed to do with it so far:

Code:

CURRENT_DESKTOP=`wmctrl -d  |cut -b-5 | grep '*' | awk '{print $1'}`
VIRTUAL_DESKTOP_SIZE=`wmctrl -d  |cut -b4- | grep '^*' |awk {'print $8'} | sed -e 's/x/ /'`
WINDOWS_PRESENT=`wmctrl -l -G |awk {'print $2'} | grep $CURRENT_DESKTOP | wc -l`
ACTIVE_WINDOW=`window -getfocuswindow |awk {'print $1'}`
WINDOW_IDS_PRESENT=`wmctrl -l -G |awk {'print $2 $1'} |grep ^$CURRENT_DESKTOP | cut -b2-`
DESKTOP_WIDTH=`echo $VIRTUAL_DESKTOP_SIZE | awk {'print $1'}`
DESKTOP_WIDTH_HALF=`echo $DESKTOP_WIDTH / 2 | bc`
DESKTOP_HEIGHT=`echo $VIRTUAL_DESKTOP_SIZE | awk {'print $2'}`
DESKTOP_HEIGHT_HALF=`echo $DESKTOP_HEIGHT / 2 |bc`

echo $ACTIVE_WINDOW > /tmp/tyler.work

echo ""
echo ">>> current desktop is..."
echo $CURRENT_DESKTOP
echo ""

echo ">>> current virtual desktop size is..."
echo $VIRTUAL_DESKTOP_SIZE

echo ""

echo ">>> number of windows present on current desktop..."
echo $WINDOWS_PRESENT

echo ""

echo ">>> active window id is..."
echo $ACTIVE_WINDOW

echo ""

echo ">>> ids of windows on present desktop..."
echo $WINDOW_IDS_PRESENT

echo ""

echo ">>> width of current desktop..."
echo $DESKTOP_WIDTH

echo ""

echo ">>> height of current desktop..."
echo $DESKTOP_HEIGHT

echo ""

echo ">>> half of width of current desktop..."
echo $DESKTOP_WIDTH_HALF

echo ""

echo ">>> half of height of current desktop..."
echo $DESKTOP_HEIGHT_HALF

echo ""

echo ">>> other windows apart from active..."
#doesn't work yet, need to correct output from window command
echo $WINDOW_IDS_PRESENT | sed "s/$ACTIVE_WINDOW//"

echo ">>> corrected window output"
echo $ACTIVE_WINDOW | sed 's/x/x0/'

wmctrl
"window" command? don't know where I got that from....


All times are GMT -5. The time now is 09:23 PM.