LinuxQuestions.org
Review your favorite Linux distribution.
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-04-2012, 04:09 AM   #1
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Rep: Reputation: Disabled
Unhappy Moving of file content to another two files after searching with specific pattern


Hello,
Please help me with this!! Thanks in advance!!
I have a file named file.gc with the content:
--------------------------------------------------------------------
1-- Mon Sep 10 08:53:09 CDT 2012
2revoke connect from FR2261;
3delete from mkt_allow where grantee = 'FR2261';
4grant connect to FR2261 with '******';
5alter user FR2261 comment
6Channel/SP/LMP';
7grant CLNTSV_PEK to FR2261
8;
9insert into mkt_allow
10select 'FR2261', marketkey
11from (select distinct marketkey
12from mkt_allow
13where grantee in ('CLNTSV_PEK')
14) mktlist (marketkey)
15;
-- Mon Sep 10 08:54:10 CDT 2012
revoke connect from NB2197;
delete from mkt_allow where grantee = 'NB2197';
grant connect to NB2197 with '*****';
alter user NB2197 comment
Kroger/Exxon/Mapco/LMP Stater Bros/Couch/petco';
grant CLNTSV_PEK to NB2197
;
insert into mkt_allow
select 'NB2197', marketkey
from (select distinct marketkey
from mkt_allow
where grantee in ('CLNTSV_PEK')
) mktlist (marketkey)
;
-----------------------------------------------------------------------
Query1:
I need to search for the pattern FR2261 and need to move the lines from the pattern till the next semicolon ( to a file called revoke.
This revoke file already contains some date. I need to add the lines at the end of the revoke file.
Before adding this line i need to add one more line which looks like the output of
echo "--`date` Revoked as per $Requestor/$ticket" (where Requestor and ticket are two variables read from the user).
Query:
Also we need to paste the lines starting with revoke and delete (ex: lines 2&3) matched to the pattern FR2261 to a file called man_revoke. This revoke file already contains 4 lines (may also change). 1st line and last line should be as it is. Only in between lines need to be replaced with the current revoke&delete lines.
Query3:
After copying those lines to revoke & man_revoke files i need to delete the same lines from file.gc file. Also we should remove the line -- Mon Sep 10 08:53:09 CDT 2012.
Sometimes this line may not exist. If it doesn't exist, we just neglect that part. so we need to give a condition as if the line prior to the searched pattern contains "--" (reference of the above line) we need to delete that line. Otherwise, we need to neglect it.
Query4:
How the above steps can be performed for multiple patterns (like FR2261 &NB2197)

Best Regards,
Sreenivas

Last edited by raosr020; 12-04-2012 at 04:10 AM.
 
Old 12-04-2012, 06:17 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
What have you done so far; show us your code and any issues you have.
 
Old 12-05-2012, 08:57 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

The tool you'll most likely want to use for most of this is sed.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
 
  


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
Need help get particular pattern from a file content!! vaibhavs17 Linux - General 10 05-02-2012 09:55 AM
what happens when the file in which grep is searching for pattern is blank? ameylimaye Linux - Newbie 4 10-01-2011 02:10 AM
Copy lines starting and ending with specific pattern from multiple files to a file ssn Linux - Newbie 2 07-27-2011 10:44 AM
Searching for a PATTERN in source files RECURSIVELY dsuratman Linux - General 6 11-23-2007 01:51 AM
Searching text files by content will103 Linux - Software 1 01-24-2005 07:43 AM


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