LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2004, 09:21 AM   #1
jago25_98
Member
 
Registered: Jun 2001
Posts: 302

Rep: Reputation: 30
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!
 
Old 03-10-2004, 09:29 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Try:
Code:
cat somefile | sed 's/x0/x00/' > tmpfile
# you like the result then do this:
mv tmpfile somefile
 
Old 03-10-2004, 09:37 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 03-10-2004, 09:41 AM   #4
jago25_98
Member
 
Registered: Jun 2001
Posts: 302

Original Poster
Rep: Reputation: 30
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!
 
Old 03-10-2004, 09:43 AM   #5
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
"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//"
 
Old 03-11-2004, 06:12 AM   #6
jago25_98
Member
 
Registered: Jun 2001
Posts: 302

Original Poster
Rep: Reputation: 30
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....

Last edited by jago25_98; 03-11-2004 at 06:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 5 06-14-2019 08:18 AM
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 07:46 AM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
insert a symbol with sed tonton Programming 5 08-31-2004 11:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration