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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-02-2005, 08:57 AM
|
#1
|
|
Member
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Rep:
|
can sed edit individual characters/portions of a sentence?
I've printed out the sed man page, and having skimming over it I don't see any "individual" edit commands for portions of a sentence. I'm starting to get the impression that you have to modify the entire sentence and make sed replace it just to modify a small portion of the original sentence.
Isn't there a command, for example, where I can say "Insert this phrase at this line after X characters from the beginning of the line"?
Last edited by TGWDNGHN; 10-02-2005 at 09:21 AM.
|
|
|
|
10-02-2005, 01:27 PM
|
#2
|
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Debian, FreeBSD, Ubuntu (desktop)
Posts: 3,859
Rep: 
|
Quote:
|
I'm starting to get the impression that you have to modify the entire sentence and make sed replace it just to modify a small portion of the original sentence.
|
With sed you can edit just a portion of the sentence. Given a file called something.txt, containing:
-----------------------
one two three
four fix six seven
-----------------------
Example.
Code:
sed 's/two/surprise/g' something.txt
This will replace only 'two' with 'surprise'. And the replacement will be in the position of the original.
Quote:
|
Isn't there a command, for example, where I can say "Insert this phrase at this line after X characters from the beginning of the line"?
|
Can you give a specific example with real data?
|
|
|
|
10-03-2005, 04:23 PM
|
#3
|
|
Member
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Original Poster
Rep:
|
Well, allow me to present this following example:
-------------------------
cat cat cat bird dog dog cat cat cat bird owl dog cat
-------------------------
It's quite repetitive isn't it?
Suppose, for example, I want to change the 2nd word "cat" into "dog"
-------------------------
cat dog cat bird dog dog cat cat cat bird owl dog cat
-------------------------
Or... suppose I wanted to add "xxxx" after he 4th "cat":
-------------------------
cat cat cat bird dog dog catxxxx cat cat bird owl dog cat
-------------------------
or remove the 3 "cats" in the middle:
-------------------------
cat cat cat bird dog dog bird owl dog cat
-------------------------
I'm given the impression that Sed CANNOT do these modifications directly- rather, I think Sed has to replace the entire line to make one change (even if the length is the same, such as if I replace ONE "cat" with "dog"). I mean of course you can still do the same replacements, but I was just wondering if Sed could do these changes to individual portions of a sentence.
Not it makes a difference or anything, I just find it very inefficent to replace the whole line when you only need to replace certian portions of it (I can understand if this makes some programming complications, but like I said I was just curious- after all, the guys at GNU are rather clever in their design)
|
|
|
|
10-04-2005, 11:54 AM
|
#4
|
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Debian, FreeBSD, Ubuntu (desktop)
Posts: 3,859
Rep: 
|
Quote:
|
Suppose, for example, I want to change the 2nd word "cat" into "dog"
|
This:
Code:
sed 's/cat/dog/2' testfile
Quote:
Or... suppose I wanted to add "xxxx" after he 4th "cat":
-------------------------
cat cat cat bird dog dog catxxxx cat cat bird owl dog cat
-------------------------
|
This:
Code:
sed 's/cat/&xxxx/4' testfile
Quote:
or remove the 3 "cats" in the middle:
-------------------------
cat cat cat bird dog dog bird owl dog cat
-------------------------
|
This:
Code:
sed -e 's/cat //4' testfile -e 's/cat //4' testfile -e 's/cat //4' testfile
(The last one's not pretty. Maybe someone has a cleaner solution.)
Anyway, all of the cases you mentioned can be handled with sed. I hope this is helpful.
Last edited by anomie; 10-04-2005 at 12:37 PM.
|
|
|
|
10-04-2005, 08:01 PM
|
#5
|
|
Member
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Original Poster
Rep:
|
seems good enough but I just realized that you had a word in each example to reference to- so allow me to present one problem that will answer my question:
You have the following data:
--------------
qwertyuiopasdfghjklzxcvbnm
poiuyt finis dsed fddd
3dgc6
5rfd
---------------
No words! Now... add "xxxx" after at point 15 (assuming the first character is point 0, as in an array in programming)
and add "5555" at line 3 position 5
showing me the command should answer my question- since I'll be able to see what I am missing.
Thanks
|
|
|
|
10-04-2005, 08:06 PM
|
#6
|
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Debian, FreeBSD, Ubuntu (desktop)
Posts: 3,859
Rep: 
|
Quote:
|
No words! Now... add "xxxx" after at point 15
|
Ok. I see what you're asking. The sed (stream editor) program is probably not the appropriate tool for this.
|
|
|
|
10-04-2005, 08:13 PM
|
#7
|
|
Member
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Original Poster
Rep:
|
lol great, this isEXACTLY what I was asking. You would need some sort of a front end in which you modify everything in a buffer, and use sed to flush it- rewriting the entire line even if it has one small change.
Not that it ruins sed for me lol- I just had a hard time finding a command to do this...cause it doesn't exist lol
Thank you very very much
|
|
|
|
10-07-2005, 02:13 AM
|
#8
|
|
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,601
Rep:
|
It is a bit out of topic, but can you not use awk / gawk instead or in addition to sed
and solve your problem?
|
|
|
|
10-07-2005, 03:30 AM
|
#9
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
sed -e '1s/\(.\{15\}\(.*\)$/\1xxxx\2/' \
-e '3s/\(.{5}\)\(.*\)$/\1555\2/' testfile >testfile2
This will do what you asked on post #6.
If this data is broken into records, then indeed awk would be a much better choice. It merely records the first 15 characters in line 1, then records the rest of the line, and spits them out with your characters inserted.
(from post #1
Quote:
|
I've printed out the sed man page
|
Here is how I print out the print out man pages. Just in case you haven't noticed the -t option to man.
man -t sed | lp
Last edited by jschiwal; 10-07-2005 at 03:33 AM.
|
|
|
|
10-13-2005, 05:57 AM
|
#10
|
|
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,601
Rep:
|
jschiwal  I am very impressed
Btw there is a missing \)
should read
'1s/\(.\{15\}\)\(.*\)$/\1xxxx\2/'
instead of
'1s/\(.\{15\}\(.*\)$/\1xxxx\2/'
and in the second -e expression {5} it should read \{5\}
but maybe it is because I have tested this with a "dos version" of sed.
I am sure you have planted those typos on purpose...
I was doing some reading on sed; anybody new to sed
and looking at advanced commands should really look at
http://sed.sourceforge.net/sedfaq.html
Last edited by Emmanuel_uk; 10-13-2005 at 07:44 AM.
|
|
|
|
10-18-2005, 04:50 AM
|
#11
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Thank you for catching my typo's. I had tested it on another computer before typing it in.
Regular expressions can be hard to read, there is no doubt about that. I find myself using egrep instead of grep simply to be able to use something like '[248]{1,3}\*' instead of '[248]\{1,3\}\*'.
The corrected sed command:
sed -e '1s/\(.\{15\}\)\(.*\)$/\1xxxx\2/' -e '3s/\(.\{5\}\)\(.*\)$/\1555\2/' temp >testfile2
jschiwal@hpand64:~> cat testfile2
This was actually an easy one. Sed can get difficult when you have patterns, that can span more than one line. Then you need to use the commands that add more lines to the pattern space and use labels and such.
If you use vim, grep or even search with regular expressions in less, simple sed commands become easier with time.
Last edited by jschiwal; 10-18-2005 at 04:54 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:09 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|