can sed edit individual characters/portions of a sentence?
Linux - SoftwareThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"?
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"?
-------------------------
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)
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.
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
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
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.