Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-04-2012, 05:09 AM   #1
LQ Newbie
Registered: Nov 2012
Posts: 17

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

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
7grant CLNTSV_PEK to FR2261
9insert into mkt_allow
10select 'FR2261', marketkey
11from (select distinct marketkey
12from mkt_allow
13where grantee in ('CLNTSV_PEK')
14) mktlist (marketkey)
-- 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)
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).
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.
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.
How the above steps can be performed for multiple patterns (like FR2261 &NB2197)

Best Regards,

Last edited by raosr020; 12-04-2012 at 05:10 AM.
Old 12-04-2012, 07:17 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,700

Rep: Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494
What have you done so far; show us your code and any issues you have.
Old 12-05-2012, 09:57 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,845

Rep: Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006
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:


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

All times are GMT -5. The time now is 03:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration