LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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, 10:21 AM   #1
jago25_98
Member
 
Registered: Jun 2001
Posts: 280

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, 10:29 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

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

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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, 10:41 AM   #4
jago25_98
Member
 
Registered: Jun 2001
Posts: 280

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, 10: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, 07:12 AM   #6
jago25_98
Member
 
Registered: Jun 2001
Posts: 280

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 07:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 4 03-19-2014 02:28 PM
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 08:46 AM
insert string with sed greg108 Programming 7 02-18-2005 02:11 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
insert a symbol with sed tonton Programming 5 08-31-2004 12:33 PM


All times are GMT -5. The time now is 01:52 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration