- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ed editor to insert line before the last 10 lines ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-15-2003 12:01 PM
тАО09-15-2003 12:01 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-15-2003 01:22 PM
тАО09-15-2003 01:22 PM
Re: ed editor to insert line before the last 10 lines of a file?
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`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-15-2003 01:28 PM
тАО09-15-2003 01:28 PM
Solutionvi (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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-15-2003 02:58 PM
тАО09-15-2003 02:58 PM
Re: ed editor to insert line before the last 10 lines of a file?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-15-2003 07:50 PM
тАО09-15-2003 07:50 PM
Re: ed editor to insert line before the last 10 lines of a file?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2003 04:50 AM
тАО09-16-2003 04:50 AM
Re: ed editor to insert line before the last 10 lines of a file?
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-17-2003 12:32 PM
тАО09-17-2003 12:32 PM
Re: ed editor to insert line before the last 10 lines of a file?
In the past ed used less memory then using vi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-17-2004 12:08 AM
тАО09-17-2004 12:08 AM