LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-02-2005, 08:57 AM   #1
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Rep: Reputation: 15
Question 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.
 
Old 10-02-2005, 01:27 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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?
 
Old 10-03-2005, 04:23 PM   #3
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94

Original Poster
Blog Entries: 1

Rep: Reputation: 15
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)
 
Old 10-04-2005, 11:54 AM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 10-04-2005, 08:01 PM   #5
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94

Original Poster
Blog Entries: 1

Rep: Reputation: 15
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
 
Old 10-04-2005, 08:06 PM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 10-04-2005, 08:13 PM   #7
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94

Original Poster
Blog Entries: 1

Rep: Reputation: 15
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
 
Old 10-07-2005, 02:13 AM   #8
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
It is a bit out of topic, but can you not use awk / gawk instead or in addition to sed
and solve your problem?
 
Old 10-07-2005, 03:30 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 10-13-2005, 05:57 AM   #10
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
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.
 
Old 10-18-2005, 04:50 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


Reply



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 error message: extra characters after the command. nano_mag Linux - General 3 05-15-2005 01:00 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
What does this sentence mean? shadkong Slackware 9 04-07-2005 06:42 AM
exploding string into individual characters using a shell script farmerjoe Programming 9 10-13-2004 03:23 AM
using sed to grab two characters? nausicaa3000 Programming 4 04-20-2004 11:01 AM

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

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