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.
I have a several thousand word document (in OpenOffice but it can be in a text document for processing) into which I would like to insert a known fixed string every X characters.
For instance suppose I wanted to insert the string "ABC" into the following after every 20 characters:
Twas the night before Christmas, when all through the house
not a creature was stirring, not even a mouse.
It would look something like:
Twas the night beforABCe Christmas, when alABCl through the house
ABCnot a creature was sABCtirring, not even a ABCmouse.
I tried to figure out a way to this seemingly straight forward action with sed and awk and even in Openoffice. The use of regular expressions seems promising but I couldn't solve it. Also problematic was the need to use multiple lines vs just each line alone.
If anyone can get me on the right track or recommend another tool, I'd appreciate it.
Thanks and Merry Christmas!!
Ken
I tried
sed 's/\(.*\)/ABC\1/' foo2.txt
and it puts ABC at the beginning of each line
$ echo 'Twas the night before Christmas, when all through the house
not a creature was stirring, not even a mouse.' | sed -r 's,(.{20}),\1ABC,g'
Twas the night beforABCe Christmas, when alABCl through the house
not a creature was ABCstirring, not even aABC mouse.
Notice, I did NOT use a text file initially; so I put the 2 lines of verse into "foo.txt", & tried:
Code:
cat foo.txt |sed -r 's,(.{20}),\1ABC,g'
as well as:
Code:
sed -r 's,(.{20}),\1ABC,g' foo.txt
Both worked:
Code:
$ cat foo.txt |sed -r 's,(.{20}),\1ABC,g'
Twas the night beforABCe Christmas, when alABCl through the house ABC
not a creature was sABCtirring, not even a ABCmouse.
Code:
$ sed -r 's,(.{20}),\1ABC,g' foo.txt
Twas the night beforABCe Christmas, when alABCl through the house ABC
not a creature was sABCtirring, not even a ABCmouse.
Before you ask "why?" or "how?", please read man sed especially the references in "See Also", especially the one to "regex(7)". If you have any Q's after that, please ask. If you're really interested in mastering sed & regex's, try explaining here what is going on.
Hint: There are 3 1/2, perhaps 4 1/2, "tricks", one of which you have already shown that you know.
When applied to the whole poem (as d/l'd to foo2.txt), it is obvious that my code, too, starts over at the beginning of each line.
However, the following does work, I tested on the whole thing:
Code:
sed -r 'N;s,(.{20}),\1ABC,g' foo2.txt
Code:
$ sed -r 'N;s,.{20},&ABC,g' foo2.txt | head
'Twas the night befoABCre Christmas, when aABCll through the houseABC
not a creature waABCs stirring, not evenABC a mouse.
The stockings were ABChung by the chimney ABCwith care,
in hopeABCs that St. Nicholas ABCsoon would be there.ABC
The children were ABCnestled all snug in ABCtheir beds,
while visions of suABCgar plums danced in ABCtheir heads.
And MABCama in her 'kerchiefABC, and I in my cap,
had just settled ouABCr brains for a long ABCwinter's nap.
How it works
-r keeps you from having to escape metacharacters.
N solves the "start over" problem by appending the next line.
, instead of '/' -- legibility: the 1st character after the 's' is the delimiter; any character is eligible, not just '/'.
{20} '{' & '}' make a bound that applies to the '.' wildcard.
& inserts the entire matched regex -- a little neater than "(...),\1".
g "rinse & repeat" through the whole line.
Hope this a) helps, & b) isn't too much showing off.
Comment
With all resect to Perl, Python, Ruby, & those who write them well; it's good to know the simple CLI tools also: tr, grep, sed, awk, etc.
Here we see a simple 1-liner that worked on the 2nd try, with no need to create a script file. Don't use an 8 pound sledge when a 16 ounce hammer will do. The higher level scripting languages are invaluable at the right time, but they are not the only answer.
<rant />
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.