Using sed to add carriage retuns and line numbers.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Using sed to add carriage retuns and line numbers.
I think that sed will be the right tool for this job. Here is what I'm trying to do: Take a block of text (like an essay) and turn it into a database where each sentence becomes one field and each sentence gets assigned a number based on the order. So, I thougt that I could use sed to add the returns (just have it look for a '.' (or some other punctuation marks) and insert a '\r' after it. But, how can I get sed to add the line numbers? Would awk be better for that?
An example of what I want to do:
Turn this:
Code:
This is a sentence of an essay. There will be many sentences.
Some sentences are long and some sentences are short, but all end
with punctuation.
Into this:
Code:
1<tab>This is a sentence of an essay.
2<tab>There will be many sentences.
3<tab> Some sentences are long and some sentences are short, but all end with punctuation.
The "<tab>s" will be actual tabs, not written out.
#!/usr/bin/perl
undef $/; # slurp mode on
# foreach argument
foreach $file (@ARGV) {
# slurp up this file
open(FH, "<$file") or die "Couldn't open $file: $!";
$everything = <FH>;
# split into sentences
@lines = split /(?<=[.?!])\s*/, $everything;
# save sentences from this file
push @all_lines, @lines;
}
# decide how many leading zeros to have in the number
$line_number = ("0"x (log(@all_lines)/log(10)))."1";
# an alternative to the above it to use a fixed number like so
# $line_number = "000001";
# print the result to stdout
foreach $line (@all_lines) {
print $line_number++,"\t$line\n";
}
Yes, this is oneupmanship, but I've squeezed it into 90 characters (counting the shebang), and it works for any number of input files, and it works for any ending punctuation (not just periods). Before you ask, the answer is: no life at all.
Ahh, homey I had completely forgotten about a bash sed combo. Here is a little something I cooked up based on your script which is a bit more interactive:
Thanks Optimistic. I tend to write utilities more like the standard unix ones -- non-interactive so they can be used with pipes or redirection, or in other scripts. I hope some of this has been useful to you, or maybe has gotten more people interested in perl . In fact, sed and awk were the inspiration for a lot of perl features, which is why I read this thread in the first place.
puffinman: I think that I will look into perl, I probably should have long ago, but after reading a few essays by Larry Wall and some reviews about how 'crazy' perl was, I kinda decided to explore other places. Looks like I was wrong, for your script was quite elegant. I'm not a programmer (I'm one of those wierd philosopher-logicians), but I do like an elegant script and a language that allows for creativity.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.