LinuxQuestions.org
Review your favorite Linux distribution.
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 12-10-2008, 07:45 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
sed question


Hi,

I have a large file with text and I need to replace every capital letter (from A to Z) with itself but { in front and } at the back, so e.g.

As always I am happy with LQ > {A}s always {I} am happy with {L}{Q}

I have tried this with sed:

Code:
sed 's/[A-Z]/\{[A-Z]\}/g' file.txt
But the problem is that I now get {[A-Z]} at each place of a capital, so my above line becomes:

{[A-Z]}s always {[A-Z]} am ...

How can I fix that? Any sed gurus online to help me?

How would I do the above but now excluding all lines that start with "@" (without quotes)?
 
Old 12-10-2008, 07:56 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
try this one:
sed 's/[A-Z]/{&}/g' file.txt

does it work?
 
Old 12-10-2008, 08:07 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
Quote:
How would I do the above but now excluding all lines that start with "@" (without quotes)?
I would write something like that, however, I'm at work now and can't test it
Quote:
sed '/^[^@]/ s/[A-Z]/{&}/g' file.txt
 
Old 12-10-2008, 08:14 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Here is sycamorex's solution with the added condition (only lines not starting with "@"):

sed '/^@/!s/[A-Z]/{&}/g' file.txt



Really good SED tutorial:

http://www.grymoire.com/Unix/Sed.html


OOPs, redundant--I was experimenting while typing (and trying to eat breakfast at the same time)

These both work.....
/[^a]/<command>
/a/!<command>

So what does this do?
/[^a]/!<command>

(Execute <command> for all lines that do not begin with a character that is not "a")
We value SED for its power--and for its obfuscational prowess......

Last edited by pixellany; 12-10-2008 at 08:26 AM.
 
Old 12-10-2008, 08:14 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Or as alternative:
Code:
sed '/^@/ !s/[A-Z]/{&}/g' file.txt
literally: for lines containing a leading @ don't do the substitution, do it for all the others.

Edit: beaten by pixellany for a bunch of seconds!
 
Old 12-11-2008, 01:54 AM   #6
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
Thanks

Awesome guys! Works like a charm. I should really read that tutorial... sed is marvelous, but it can be quite incomprehensible sometimes.
 
  


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] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
question on sed tifu Linux - Newbie 3 03-18-2005 04:38 PM
sed question tifu Programming 5 03-18-2005 12:02 PM
sed question from Ch 6.12 na5m Linux From Scratch 1 02-02-2005 03:36 PM
A question on 'sed' Barbarian Programming 4 04-11-2002 10:19 PM


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