LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-08-2015, 07:17 PM   #1
susanau
LQ Newbie
 
Registered: May 2015
Posts: 2

Rep: Reputation: Disabled
sed command to global change if it match some pattern


This is what I have
FINPXXX
FOLLOWS FINPJOB99
CALENDAR1 FINP456
CALENDAR2 FINP374
CALENDAR3 FINPOE9

End result I want is
FINPXXX
FOLLOWS FINPJOB99
CALENDAR1 FIND456
CALENDAR2 FIND374
CALENDAR3 FINDOE9

I have 3 commands
sed 's/CALENDAR1 FINP/CALENDAR1 FIND/g'
sed 's/CALENDAR2 FINP/CALENDAR2 FIND/g'
sed 's/CALENDAR3 FINP/CALENDAR3 FIND/g'

I cannot just do sed 's/FINP/FIND/g' because there are a lot of 'FINP' pattern in the file I need to keep.

The problem I have is I have a lot of files, and I have to go thru each file to find out how many CALENDAR# pattern I have. Is there a better way to do the sed command? I tried this
sed 's/CALENDAR. FINP/CALENDAR. FIND' But it replaces all CALENDAR# to CALENDAR.

Thank you.
 
Old 05-08-2015, 10:20 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,848

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
Use regex - sed supports character classes
Code:
sed -r 's/(CALENDAR[[:digit:][:space:]]+FIN)P(.*)/\1D\2/' cal.txt
No need for global change unless there is more than one occurrence per record - doesn't harm if you leave it in.
 
1 members found this post helpful.
Old 05-09-2015, 03:37 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
An alternative which assumes 'CAL' is always at the start of the lines we want to change:
Code:
sed '/^CAL/s/FINP/FIND/' file
 
Old 05-11-2015, 04:57 PM   #4
susanau
LQ Newbie
 
Registered: May 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you. It works!
 
Old 05-12-2015, 03:57 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Remember to mark as SOLVED once you have a solution.
 
  


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
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 05: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 05:18 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 03:31 PM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 02:47 PM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 09:34 AM


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