LinuxQuestions.org
Help answer threads with 0 replies.
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 12-20-2006, 10:08 PM   #1
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Rep: Reputation: 15
howto insert a string


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
 
Old 12-21-2006, 12:35 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
You might be able to make it work as a shell script which takes a filename as input. Pseudo-code might look like this:

while not EOF
do
start counting characters and spaces
if character count == 20
then sed insert ABC
else
continue
fi
done

Now all you have to do is convert to the correct code.
 
Old 12-21-2006, 07:43 AM   #3
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
in word macro language you could write a macro
(pseudocode!
Code:
cursor.pos = BOT
while not(cursor.pos == EOT) do
  cursor.left(20)
  document.insert("ABC")
done
HTH,
Flo
 
Old 12-24-2006, 09:55 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Is this what you're looking for?:
Code:
sed -r 's,(.{20}),\1ABC,g'
Code:
$ 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.
 
Old 12-24-2006, 10:10 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Follow up & "Extra credit"

"Extra credit":
The feature of sed you are already using has a simpler alternative in this case, see if you can find it.


If you're not interested in playing this learning "game", I have the/my answers ready to post.

color test == LightBlue
 
Old 12-24-2006, 11:22 AM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by kscott121
...
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.
Here is a perl hack that you should be able to modify to suit your needs. This is based on your input data, which I placed on file "data1":
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate text insertion with slurp.

use warnings;
use strict;

my($insert_distance) = 20;
my($out);
my($string) = "ABC";
my $text = do { local $/; <> }; # Perl Best Practices, p213
my($length) = length $text;

for( my $i = 0 ; $i < $length ; $i += $insert_distance ) {
        $out .= substr($text, $i, $insert_distance);
        $out .= $string;
}

print $out;

exit(0);
Which produces, when run:
Code:
% ./p1 data1
Twas the night beforABCe Christmas, when alABCl through the house
ABCnot a creature was sABCtirring, not even a ABCmouse
matching your desired output.

Best wishes for the season and the new year ... cheers, makyo
 
Old 12-26-2006, 03:50 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Oops!

A lesson in test files that are too short:

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 />
 
  


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
Insert a string to Syslog? Ransak Linux - Software 4 10-26-2006 03:51 AM
Script to insert string in first line of a file minil Programming 13 01-02-2006 11:56 PM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
insert module into non-running kernel... howto? euph0rix Linux - Newbie 2 01-25-2005 06:29 PM
pppsetup : How to insert modem init string xgreen Slackware 1 03-07-2004 07:18 PM

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

All times are GMT -5. The time now is 10:02 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
Open Source Consulting | Domain Registration