LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-26-2015, 10:58 PM   #1
ranbon78
LQ Newbie
 
Registered: Oct 2015
Posts: 2

Rep: Reputation: Disabled
Find pattern and update


Hello Gurus,

I have a big file (more than 2000 lines). Where i have to insert a # at the beginning of 1 above line and at the beginning of 2 below line after finding a pattern. Also insert # at the beginning of line where pattern found. Environment is Red Hat Linux. Also if you can explain, that would be great.
Take an example, please see below texts, Search "Fail" and # 1 line before that and 2 lines after that string (beginning of line). Also # line containing string "Fail".


The file is like below....

Name
Reason = Pass
Reasult
Number
Name
Reason = Pass
Reasult
Number
Name
Reason = Fail
Reasult
Number
Name
Reason = Pass
Reasult
Number
Name
Reason = Fail
Reasult
Number
Name
Reason = Pass
Reasult
Number

Thanks in advance
 
Old 10-26-2015, 11:10 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Sounds like something you could do in vi(m) with a macro. Just record one doing the steps for one of them and repeat it however many times as needed.
 
Old 10-26-2015, 11:22 PM   #3
ranbon78
LQ Newbie
 
Registered: Oct 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the and Sefyir. But i have to incorporate it in the flow. user interaction is not an option.

Thanks
 
Old 10-26-2015, 11:45 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed allows you to read multiple lines into the pattern space - if the data is that structured you could search for "Name" and read the next 3 lines in. Then check for "Fail" and if found use a normal "s" command to place a # at the beginning of each line in the pattern space - gnu sed had the "m" modifier for that.
 
  


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
find/replace by line nr or by pattern digerydoo Programming 7 10-08-2012 09:35 AM
[SOLVED] Find the design pattern xeon123 Programming 1 08-16-2012 06:31 PM
What if the I dont find the pattern using grep? ameylimaye Linux - Newbie 14 09-29-2011 01:42 AM
Please help find this wallpaper pattern! Farmgirl General 9 06-10-2011 04:59 PM
[SOLVED] Using find and a pattern file spivey76 Programming 6 11-03-2010 07:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:52 PM.

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