LinuxQuestions.org
Visit Jeremy's Blog.
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 03-13-2012, 03:45 PM   #1
VladC
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Rep: Reputation: Disabled
Sed insert before character


I have the following homework. I need to enter a certain word, let s call it "exemple" , before every lowercase character in a document. I tryed

sed '/[a-z]/i \exemple' text.txt


OUTPUT:

1: exemple
2: same text no changed

Can you please help me figure this out?
 
Old 03-13-2012, 03:55 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
The "i" command is for inserting whole lines above the one you matched (and "a" appends them after it). You need to use the "s" command to edit the line itself.

In this case you need to match the character or string you want on the left side of the substitution, and save it in a regex backreference. Then you can insert it into the right-hand-side replacement text. This means you'll probably want to use the -r program option. Don't forget the "g" substitution modifier too if there can be multiple matches on the line (s///g).

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


PS: Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Last edited by David the H.; 03-13-2012 at 04:02 PM. Reason: Expanded answer
 
1 members found this post helpful.
Old 03-13-2012, 04:01 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
An easy way of doing it would be using the special character "&".
 
1 members found this post helpful.
Old 03-15-2012, 12:44 AM   #4
VladC
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you guys. Solved
 
  


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
[SOLVED] insert a character in line using sed/awk sunilsagar Programming 3 07-26-2011 01:44 AM
sed insert newline character jhwilliams Linux - Software 6 06-08-2007 03:13 PM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 12:19 PM
Insert after every nth character using sed DoDo1975 Linux - Software 2 01-17-2007 06:59 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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