sed -i -e 's/^three$/> &/' example.txt
Main Topics
Browse All TopicsI want to edit one line in a file on my computer. All i do is simply put a '>' character on the line that has the word "three" on it (lets assume there is only one line). I have to do this often so I want to make this as easy as possible to do, be it a shell script, a bash script, or a single command that I can alias. Here is an example:
example.txt (BEFORE)
one
two
three
four
example.txt (AFTER)
one
two
> three
four
What I have: simple.pl (it does work)
#!/usr/bin/perl
use warnings;
use strict;
# Vars (could be command line args)
my $FILE = 'example.txt';
my $WORD = 'three';
# Generate a list of lines in the text file,
# and add the '>' to the line with WORD
open(IN, $FILE) or die("$!");
my @lines = <IN>;
for (@lines) {
chomp;
if ($_ =~ /^$WORD$/) {
$_ = '>'.$_;
}
}
close(IN);
# Rewrite the file with the line change
open(OUT, '>example.txt') or die("$!");
print OUT join("\n", @lines);
close(OUT);
What I want:
What other ways can this be done? Bash scripts, better perl scripts, Awk, command line only?
Be creative, I am learning shell scripting and PERL but I think there has to be a better way to do this seemingly simple task. This is for my own learning experiences, so be as complex as you want, I look forward to it!
Thanks,
Joe P
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Wow, nice ozo! Could explain the first script?
perl -i -pe 's/^(?=three$)/> /' example.txt
-i: allows editting of files in place
-e: allows one line of code on the command line
-p: assume loop like -n but print line also, like sed (don't quite understand what this means, perform the line of code for every line in the file?)
Then the actual line of code:
s/^(?=three$)/> /
I thought I knew Regex inside and out but apparently not. An online source said:
(?=pattern)
A zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $&.
Does that mean that you replace the "nothing" in front of the word "three" with '> ', because of the allow editting files in place?
Really great script, I am going to study your sed example now!
Joe P
My friend pointed out:
file_put_contents(str_repl
a nice php one-liner if you have php on the linux box.
Also this website repeats ozo's perl one liner search and replace: and my understanding of the script is better after studing it and playing around with it.
http://joseph.randomnetwor
Is it possible to perform the equivalent using the map function? I figured it would be like this:
open(IN,'example.txt') or die("$!");
@lines = map { chomp; s/^(?=three$)/> / } <IN>
...
But from a failed attempt and some research shows that s/// does not work well inside map because it doesn't return $_, instead it returns a 0 or a 1.
I'll keep looking for somre more creative examples but I like ozo's perl script.
Keem em coming,
Joe P
Excellent Perl_Driver, I confirmed it works with testing and also this website uses similar constructs:
http://www.thescripts.com/
Speaking of which I have a few questions about your script after researching:
local @ARGV = ($FILE);
local $^I = '.bac';
I found that:
$^I -- Holds the file extension used to create a backup file for the in-place editing specified by the -i command line option. For example, it could be equal to ".bak."
http://www.cs.cf.ac.uk/Dav
-- and --
If $^I is set (either from within the program, or by the -i commandline switch), opening files with the diamond operator (<>) automatically performs a similar operation (allowing editing in place by holding a backup file. Notice I didn't need the loop for @ARGV; that's implicit in the diamond operator. The value set for $^I (normally undef) is added to the names of the files in @ARGV to create backup files.
http://www.stonehenge.com/
So my question:
local @ARGV = ($FILE);
Just to make sure I have grasped it: This is making a list of one element (the file name) and putting into @ARGV solely for the block ({ ... }) so that the while(<>) will read from the $FILE as if it was on the command line because the <> reads $ARGV[0] like a file? That is freaking brilliant!
ozo: yours is more complicated and I have more questions but I spent about 20-30 minutes studing Perll_Driver's script so I will have to give yours a fair amount of time as well. Especially researching perl's command line arguments which I have just started looking at because of your excellent one-line scripts.
It is nice to see I went back and shortened my script a little cutting of the if(/^$WORD$/) { ... } down to s/^($WORD)$/> $1/ and I see that your new script uses this regex as well.
Thanks again both of you!
Joe P
Perl_Driver shortened script: (maybe microseconds slower, but no branching could speed it up?)
#!/usr/bin/perl
use warnings;
use strict;
# Vars (could be command line args)
my $FILE = $ARGV[0] || 'example.txt';
my $WORD = $ARGV[1] || 'three';
{
local @ARGV = ($FILE);
local $^I = '.bac';
while (<>) {
s/^($WORD)$/> $1/;
print;
}
}
I think you will enjoy this link too:
http://www.perl.com/pub/a/
Excellent link Perl_Driver. I have heard about the FMTYEWTK pages because I just started following this tutorial here: (and the introduction mentioned those pages)
http://www.perl.org/books/
Thanks for the link, it will certainly help me, and I bookmarked it immediatly.
Again thanks to both you and ozo for helping me out and for suffering through my always long questions and comments =):
I'll get there yet!
perl -e '$points=$ARGV[0]; @scores=split(/_/, $points); print join("\n",@scores), "\n";' 250_250
Joe P
Business Accounts
Answer for Membership
by: ozoPosted on 2007-02-18 at 16:12:12ID: 18560356
perl -i -pe 's/^(?=three$)/> /' example.txt