LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-16-2010, 08:42 AM   #16
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738

Quote:
Originally Posted by mamun2015 View Post
Basically, I need to search for a word then I need to replace it with the output of "ps ax", each process in new line.
Why? I'm having trouble seeing how this is useful.

Regardless, go back to the question from jamescondron---what happens when you run the command?
 
Old 03-16-2010, 11:28 AM   #17
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Trouble might be that `ps ax` produces a screen-full or more, of lines of information, and sed is choking on it.

Here's something that seems to do the trick, but may not be guaranteed to work under all circumstances:

I do the command `ps ax` and dump it to a temporary file:
Code:
ps ax >> tempfile
and now, use sed's s/ function to delete 'ncdbrun' followed by sed's r function to insert the tempfile we just made:
Code:
 echo 'ncTODAY...ncdbrun' | sed -e "s/ncTODAY/$TODAY/g" -e 's/ncdbrun//; r tempfile'
and then delete the tempfile. Also, you probably want to encode the name of the tempfile using `mktemp` or whatever, and create it somewhere safe (in /tmp) for a production system.

There's probably a better way to do this, but I don't know it, or at least at this time of day, I can't find it so good luck!


P.S.
Quote:
Originally Posted by mamun2015
...thanks a lot,man...
..please help,man!!
Try this; copy and paste this into a console:
Code:
echo 'please help,man!!' | sed 's/^p/P/;s/,/ me, Wo/'
Cheers,

Miss Sasha
 
Old 03-16-2010, 11:45 AM   #18
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I have merged your two threads----Please: Only one thread per topic....thank you
 
Old 03-16-2010, 11:50 AM   #19
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
To get your code to start working any newlines and special characters such as /&\ need to be escaped in $DBRUN.
This uses GNU sed.
:a N; $!ba reads all the lines of 'ps ax' into the pattern space
s/[\n/&\\]/\\\&/g escapes the characters /&\ and \n.
\n represents a newline in a multi-line pattern space.
Code:
TODAY=$(date '+%d-%m-%y')
DBRUN=`ps ax | sed ':a N; $!ba; s/[\n/&\\]/\\\&/g'`

echo 'ncTODAY...ncdbrun' | sed -e "s/ncTODAY/$TODAY/g" -e "s/ncdbrun/$DBRUN/g"
The above method isn't very elegant and I'm not confident that it will always give perfect results.
Using the GNU sed 'e' command to execute shell code in the pattern space is better.
Code:
TODAY=$(date '+%d-%m-%y')
DBRUN="ps ax"    # Not `ps ax`

echo 'ncTODAY...ncdbrun' | sed  "s/ncTODAY...ncdbrun/echo $TODAY...;$DBRUN/e"
 
  


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
[SOLVED] sed help - replace line feed with different character bradvan Programming 7 04-23-2012 12:31 AM
find and replace (probably with sed) or some other linux commands dbgeek Linux - Newbie 2 12-14-2009 06:57 AM
Replace 2nd to last Character with SED elproducto Programming 5 03-31-2009 01:41 PM
find certain character and replace with a number yongitz Programming 1 01-18-2007 08:40 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


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