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 12-11-2014, 03:39 AM   #16
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267

Hi grail, what is that \ good for?
sed '/search/a\new_word' file
it works without that too.
 
Old 12-11-2014, 08:54 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well to be perfectly honest I just always followed the grymoire stuff, http://www.grymoire.com/Unix/Sed.html#uh-40

But if it works without I am happy too
 
Old 12-11-2014, 11:14 PM   #18
Sunray74
LQ Newbie
 
Registered: Dec 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks a lot for all your help.
 
Old 12-12-2014, 04:12 AM   #19
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Glad to help you.
If you really want to say thanks just press YES
 
Old 12-16-2014, 01:42 PM   #20
Sunray74
LQ Newbie
 
Registered: Dec 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Hi Guys,

Need some more help on a specific scenario to achieve using a SED command, i have the logic in AWK but since i have lot of logic involved using SED am requesting the command for SED.

i have a pattern as shown below, need to search for 'zzzzz' and delete the next line but should not touch the first pattern, it should operate from the 2nd pattern

xxxxx
yyyyy
zzzzz -->search string and delete the empty line below (there are 3 empty lines) from the 2nd pattern



12345
12345
xxxxx
yyyyy
zzzzz --> basically from here, the empty line should be deleted



12345
12345

AWK command which does the job is shown below, tested succefully..
awk '/<search_word>/&&c++ {next} 1' < test2.txt

SED command which i tried is not working....
sed '1!{/^<search_word>/d;}' < test2.txt
or
sed '2,${/^<search_word>/d;}' < test2.txt

Appreciate your help.
 
Old 12-16-2014, 01:48 PM   #21
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
this is a new problem, would be nice to open a new thread for that next time.
I do not really understand what should be deleted and also I do not really understand that awk script (for example what is that c++ good for?) Can you please show the result too.
 
Old 12-16-2014, 02:12 PM   #22
Sunray74
LQ Newbie
 
Registered: Dec 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Hi Pan64,

I have actually used the logic which you have advised to <search_word> and use <replace_word> but whats happening was it was actually copying the whole line and then replacing the <search_word> with the <replace_word>, so to delete the rest of the line, i have modified the script to remove the rest of the line as..

Original: sed '/search_word/{p;s/search_word/replace_word/}'

modified: sed -e "/^ <search_word>/{p;s/<search_word> .*/<replace_word>/}"

but this is actually inserting in a new line and i need to write another SED command to delete the extra line created from the above command, like

-e "/<look_for_replace_word>/{N;s/\n.*//;}"

which is working fine but now there is an issue with the alignment, as from the 2nd pattern the output seems good after deleting the extra line but the alignment is going wrong on the first pattern as am deleting the line below the pattern which should not happen.....should only delete the next line only after the 2nd pattern...

is there an option which would use the Original command above and add the <replace_word> with out adding a new line, like read the empty line and write the
<replace_word> with out inserting ? so that i do not need to delete any extra lines... ???

Make sense ?
 
Old 12-16-2014, 05:17 PM   #23
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Moderator response

This whole thread seems to be Homework style queries. Spoon feeding helps no one.

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
 
  


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
Using sed in bash script to serach and replace special characters, and text PoltoX Programming 6 08-14-2013 11:25 PM
Remove control characters in Script output Dalavich Linux - Newbie 2 02-26-2013 04:05 PM
Bash script to accept password and replace characters with * as they are typed krisdames Programming 16 10-03-2011 12:22 AM
sed: replace same number of characters between tags unihiekka Linux - Newbie 6 12-30-2008 04:51 AM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 12:56 PM


All times are GMT -5. The time now is 11:46 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