Operating System - HP-UX
1753325 Members
5644 Online
108792 Solutions
New Discussion юеВ

ed editor to insert line before the last 10 lines of a file?

 
SOLVED
Go to solution
A. Daniel King_1
Super Advisor

ed editor to insert line before the last 10 lines of a file?

I've seen a neat trick:

printf "1i\nYour text here\n.\nw\nq\n" | ed yourfilename

This inserts a line at the beginning of a file. However, if I wanted to insert this line ten lines from the end of a file, how would I do this? I'm sure there is some simple, efficient way to reference this relative line number, something like ($-10).

Thanks in advance.
Command-Line Junkie
7 REPLIES 7
Rob_132
Regular Advisor

Re: ed editor to insert line before the last 10 lines of a file?

No "neat tricks" here....

My *guess* would be that since ed is a line oriented editor, it cannot "count" 10 lines backwards....but I could be wrong.

An alternate may be:

>abc
>def
>outfile
tail -10 infile>def
A=`wc -l infile`

head -A infile>abc
echo "yourline">>abc
cat abc def > outfile

Most likely, awk, sed, or perl can do this in less lines....but this should work. You'll have to work out the command in "<>" - cannot access my UX box at this time.

Good luck!

Rob
Rob_132
Regular Advisor
Solution

Re: ed editor to insert line before the last 10 lines of a file?

One other thought you could play with....

vi (vastly) expanded upon ed - using many of the original commands.

In vi

G10k

takes you 10 lines from the bottom of the file. Maybe this could play into the 'ed' answer.

Rob
Dave La Mar
Honored Contributor

Re: ed editor to insert line before the last 10 lines of a file?

A. Daniel-
Based on Rob's reply the following should "down and dirty" it for you.
#!/usr/bin/ksh
# Test a prepend to file
# 1i means insert at line one.
wc -l test1.fil | read total_lines
let insert_line="$total_line - 10"

echo "Keep on keeping on." | read STRING_TO_INSERT
string="$STRING_TO_INSERT"

ed << EOF >/dev/null
e /home/dlamar/work/test1.fil
$insert_linei
$string
.
w /home/dlamar/work/test1.fil
q
EOF

Best of Luck.
Regards,
dl
"I'm not dumb. I just have a command of thoroughly useless information."
Hein van den Heuvel
Honored Contributor

Re: ed editor to insert line before the last 10 lines of a file?

Daniel> efficient way to reference this relative line number, something like ($-10).

Sure, that works, but you gotta remember to escape the in order not to cunfuse the shell.

$ printf "\$-10i\nYour text here\n.\nw\nq\n" | ed your-file

rob> No "neat tricks" here....
Indeed, no trick needed. Standard funtionality. just read the man page. (not what you meant :^).

man ed...
:
2. The character $ refers to the last line of the buffer.
:

Cheers all,
Hein.
A. Daniel King_1
Super Advisor

Re: ed editor to insert line before the last 10 lines of a file?

Good answers!

I knew it, but I guess I needed it reinforced ... QUOTE your shell special characters!!!

I like the vi item, though, and I'm now using vim for this very task.

Thanks, all!
Command-Line Junkie
Rory R Hammond
Trusted Contributor

Re: ed editor to insert line before the last 10 lines of a file?

echo "$\n.-10\ni\nSome good textxxxxxx\n.\nw\nq"|ed /tmp/xxx

In the past ed used less memory then using vi
There are a 100 ways to do things and 97 of them are right
A. Daniel King_1
Super Advisor

Re: ed editor to insert line before the last 10 lines of a file?

Thanks, all!
Command-Line Junkie