LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-01-2015, 04:01 PM   #1
keirvt
Member
 
Registered: Sep 2006
Location: Sydney Australia
Distribution: fedora/Ubuntu
Posts: 155

Rep: Reputation: 18
sed substitute one of two strings


I want to use sed to remove a string with capitals that is followed by a comma, then space or just a space then a country name that begins with a captital. I achieve this at present with this pair of sed commands;

Code:
sed 's/[A-Z]\+ \([A-Z]\)/\1/'|sed 's/[A-Z]\+, \([A-Z]\)/\1/'
So for example, a file, "infile" with the lines
|
UCTEXT, Australia
MOREUCTEXT Zambia

will output

Australia
Zambia

Is there a way to do this with a single sed regex? It would be nice if something like below existed
Code:
sed 's/[A-Z]\+[" ",", "] \([A-Z]\)/\1/'
 
Old 10-01-2015, 06:17 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,952

Rep: Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073Reputation: 4073
Use extended regex (-r) and use (round) parentheses for the "or" - ( |, )
Note this will change the back-reference you want to use.
As a matter of style, perhaps better to use character classes to make the regex codepage safe - [[:upper:]] rather than [A-Z]
 
  


Reply

Tags
sed regex


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
[Cygwin, sed] Using filenames as both files and search strings within sed lingh Linux - Newbie 5 10-20-2012 10:38 AM
[SOLVED] substitute ( in sed ghantauke Programming 7 03-11-2011 10:46 AM
[SOLVED] sed substitute everything until character sqn Programming 5 03-30-2010 10:27 AM
[SOLVED] SED - substitute a word only in a certain line carolflb Linux - Newbie 3 02-02-2010 09:30 AM
Sed substitute for my username? camaroblue87 Linux - Newbie 1 04-29-2006 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:25 PM.

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