sed command to delete a pattern in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to delete a pattern in a file
# 1  
Old 07-08-2014
sed command to delete a pattern in a file

Hi Everyone,
I have an unusual requirement. Here is where i am stuck for sometime now...

I have this text file.. lets say .. output.sql...

it has lot of entries... here below is part of the entry...



Code:
.. . . . . . . . . . . . . . . . . .,('garbage','DirectoryTreePointOfContactRelation','tePointOfContactId','2014-05-26 10:00:29','n_leaf_pages',1,NULL,'Number of leaf pages in the index'),('garbage','DirectoryTreePointOfContactRelation','tePointOfContactId','2014-05-26 10:00:29','size',1,NULL,'Number of pages in the index'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-0410:20:19','n_diff_pfx01',3,1,'parentInstanceId'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-04 10:20:19','n_diff_pfx01',3,1,'parentInstanceId'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-04 10:20:19','n_diff_pfx 02',6,1,'parentInstanceId,id'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-04 10:20:19','n_leaf_pages',1,NULL,'Number of leaf pages in the index'),  .. .. . . .. . .. . .. . . .. . ..


from some error message, i would get below details:

Code:
[root@mysqlbackup]# cat EnterpriseBackupConsoleMessages.txt | grep PRIMARY  | awk -F'-' '{print $1}' | awk -F\' '{print $2}'
garbage
[root@mysqlbackup]#
[root@mysqlbackup]#cat EnterpriseBackupConsoleMessages.txt | grep PRIMARY  | awk -F'-' '{print $2}'
garbageLocation
[root@mysqlbackup]#cat EnterpriseBackupConsoleMessages.txt | grep PRIMARY  | awk -F'-' '{print $3}'
FK_garbageLocation_parentMyId
[root@mysqlbackup]#cat EnterpriseBackupConsoleMessages.txt | grep PRIMARY  | awk -F'-' '{print $4}' | awk -F\' '{print $1}'
n_diff_pfx01
[root@mysqlbackup]#


So i will store these fields in variables as below:
Code:
firstWord=garbage
secondWord=garbageLocation
thirdWord=FK_garbageLocation_parentMyId
fourthWord=n_diff_pfx01


these variable has the same contents as highlighted in red in the text file.Now what i want to achieve is, delete the first occurrence of this matching pattern.

That is delete the first occurence pattern starting from '(' till '),'

i mean to delete the entry

Code:
('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-0410:20:19','n_diff_pfx01',3,1,'parentInstanceId'),

I need a sed -i command so that it removes this first occurence, and efficiently fast (the max file size is 5MB) such that final output will be as below,


Code:
.. . . . . . . . . . . . . . . . . .,('garbage','DirectoryTreePointOfContactRelation','tePointOfContactId','2014-05-26 10:00:29','n_leaf_pages',1,NULL,'Number of leaf pages in the index'),('garbage','DirectoryTreePointOfContactRelation','tePointOfContactId','2014-05-26 10:00:29','size',1,NULL,'Number of pages in the index'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-04 10:20:19','n_diff_pfx01',3,1,'parentInstanceId'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-04 10:20:19','n_diff_pfx 02',6,1,'parentInstanceId,id'),('garbage','garbageLocation','FK_garbageLocation_parentMyId','2014-06-04 10:20:19','n_leaf_pages',1,NULL,'Number of leaf pages in the index'),  .. .. . . .. . .. . .. . . .. . ..


any help or guidance on how to achieve this is deeply appreciated.

Last edited by vivek d r; 07-08-2014 at 01:32 PM.. Reason: highlighting the fields with colour
# 2  
Old 07-08-2014
That doesn't tell the criteria.
Do you want to delete the text between the parenthesis (including parenthesis) if :

Code:
('word1','word2','word3','anything','word4', whatever)

matches?
# 3  
Old 07-09-2014
it should delete with parenthesis and a comma at the end. Basically this whole below content must be deleted from that file.
Code:
('word1','word2','word3','anything','word4', whatever),

# 4  
Old 07-09-2014
Code:
awk -vw1="${firstWord}" -vw2="${secondWord}" -vw3="${thirdWord}" -vw4="${fourthWord}" -vq="'" -F "[(]|[)],[(]|[)]" '
  {for(i = 1; i <=NF; i++)
    {split($i, a, ",");
    if(a[1] == q w1 q && a[2] == q w2 q && a[3] == q w3 q && a[5] == q w4 q)
      {sub("[(]" $i "),?", X); break}}}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete line from file using sed command?

hello Team, I want to delete below linux using sed command but I am getting error.sed -i '/url=/status.cgi?hostgroup=/d' 3 error:sed: -e expression #1, char 32: unknown option to `s' Could you please help me with correct syntax. My line contain / character because of that I am getting... (4 Replies)
Discussion started by: ghpradeep
4 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. Shell Programming and Scripting

delete the line with a particular string in a file using sed command.

Hello, I want to delete all lines with given string in file1 and the string val is dynamic. Can this be done using sed command. Sample: vars="test twinning yellow" for i in $vars do grep $i file1 if then echo "Do Nothing" else sed `/$i/d` file1 fi done Using the above... (5 Replies)
Discussion started by: PrasadAruna
5 Replies

4. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

5. Shell Programming and Scripting

copy, then delete lines in file with sed using a pattern

I need to copy lines to a new file from files with sed using a pattern in char postions 1-3. Then after the copy, I need to delete those same lines from the input files. For example, string "ABC" in pos 1-3 (6 Replies)
Discussion started by: laksjfhoius9123
6 Replies

6. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

7. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

8. HP-UX

How to delete specific pattern in a file with SED?

I have one file which is having content as following... 0513468211,,,,20091208,084005,5,,2,3699310, 0206554475,,,,20090327,123634,85,,2,15615533 0206554475,,,,20090327,134431,554,,2,7246177 0103000300,,,,20090523,115501,89,,2,3869929 0736454328,,,,20091208,084005,75,,2,3699546... (7 Replies)
Discussion started by: ganesh.mandlik
7 Replies

9. Shell Programming and Scripting

sed delete pattern skipping first n lines of file.

I have files of more than 10K lines that I need to delete lines that contain a pattern, but I want to keep the first few lines intact. Can this be done with sed? (7 Replies)
Discussion started by: tkg
7 Replies

10. Shell Programming and Scripting

Sed to delete exactly match pattern and print them in other file

Hi there, I need help about using sed. Iam using sed to delete and print lines that match the port number as listed in sedfile. I am using -d and -p command for delete match port and print them respectively. However, the output is not synchonize where the total deleted lines is not similar with... (3 Replies)
Discussion started by: new_buddy
3 Replies
Login or Register to Ask a Question