LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2018, 09:35 AM   #1
mikesimp2
LQ Newbie
 
Registered: Feb 2018
Posts: 1

Rep: Reputation: Disabled
sed command to delete text after match not working


I have been using the sed command in the following fashion:

sed -i 's/\(.*\MODEL1_INPUT_DIR:\).*/\1/g' /file1
sed -i 's/\(.*\MODEL2_INPUT_DIR:\).*/\1/g' /file2
sed -i 's/\(.*\MODEL3_INPUT_DIR:\).*/\1/g' /file3
sed -i 's/\(.*\dpt_DIR:\).*/\1/g' /file1
sed -i 's/\(.*\wbt_DIR:\).*/\1/g' /file2
etc..

And everything works fine, such that whenever it finds these strings within their respective files, it deletes the line after the string.

However, for this particular instance:

sed -i 's/\(.*\WF_INPUT_DIR:\).*/\1/g' /file1

It is not working. Furthermore, any instance when the first two letters are "WF", the sed command does not seem to recognize this pattern and does not delete anything after this. For example:

sed -i 's/\(.*\WF_FLAG:\).*/\1/g' /file1
sed -i 's/\(.*\WF_OUTPUT_DIR:\).*/\1/g' /file1

would not work either, such that any text after these strings are not deleted. Any thoughts?
 
Old 02-01-2018, 09:52 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,292
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Welcome. Can you please show some sample data for the sed statement in question?
 
1 members found this post helpful.
Old 02-01-2018, 10:51 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Turbocapitalist View Post
Can you please show some sample data for the sed statement in question?
Turbocapitalist is right. A well-formed inquiry should include a sample of an input file (or files) and a sample of the desired output file.

Lacking that, I offer this guess:
You have over-used the backslash character.
\W specifies a word boundary and you probably didn't intend that.
Try changing this ...
Code:
sed -i 's/\(.*\WF_INPUT_DIR:\).*/\1/g' /file1
... to this ...
Code:
sed -iE 's/(.*WF_INPUT_DIR:).*/\1/g' /file1
Daniel B. Martin

.
 
Old 02-01-2018, 06:22 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
As others have pointed out, an example of the input and the expected output are very helpful to others in understanding your question.

Please review the Site FAQ for guidance in asking well formed questions, and especially the links at bottom of that page.

All that said, I believe danielbmartin has pointed to the most obvious problem.

Welcome to LQ, and good luck!
 
Old 02-01-2018, 10:13 PM   #5
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214

Rep: Reputation: 112Reputation: 112
One oddity: If your pattern is designed to find the first match and delete the rest of the line, then adding the g qualifier which finds all matches is meaningless. It probably doesn't hurt anything in this case - unless you wanted to preserve multiple occurrences of the pattern.
 
  


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
sed command to delete text until match is found - for each line of csv LilLinuxLearner Linux - Newbie 5 07-15-2015 08:55 PM
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 08:33 AM
[SOLVED] sed match span text with style ted_chou12 Programming 3 03-11-2012 06:33 PM
How to use sed to delete all lines before the first match of a pattern? C_Blade Linux - Newbie 9 05-01-2010 04:18 AM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 01:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 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
Open Source Consulting | Domain Registration