Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:11:30+00:00 2026-06-10T23:11:30+00:00

Is it possible to do simple arithmetic in sed addresses? Judging by the addresses

  • 0

Is it possible to do simple arithmetic in sed addresses?
Judging by the “addresses” manual section, the answer seems no. But maybe there is a workaround?

For example, how can I print the second last line of a file? It would be cool something like:

sed -n '$-1 p' file

But it obviously does not work… so I usually have to do multiple sed calls, first for identifying the line, then do the arithmetic using the shell $((expr)) and then finally call sed again. Like this:

sed -n "$(($(sed -n '$ =' file)-1)) p" file

Is there a “better”, more compact, more readable way for doing arithmetics with sed addresses?

In a serious moment of procrastination, I decided to write a small script that quickly changes the xterm colorscheme. The idea is that you have the .Xresources a file with a start marker and an end marker:

...
START_MARKER
...
END_MARKER
...

and you want to delete everything that is between the markers, but not the markers themselves. Again, it would be great to do something like:

sed '/START_MARKER/+1,/END_MARKER/-1 d' file

…but you can’t!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:11:31+00:00Added an answer on June 10, 2026 at 11:11 pm

    You’re right, one can’t directly do math in sed1, even addresses. But you can use some trickery to do what you want:

    Second-last row:

    $ seq 5 | sed -n -e '${ # On the last line
    > g # Replace the buffer with the hold space
    > p # and print it
    > }
    > h' # All lines, store the current line in the hold space.
    4
    

    Between START and END:

    $ cat test.in
    1
    START
    2
    3
    END
    4
    $ cat test.in | sed '/^START$/,/^END$/{
    > /^START$/d
    > /^END$/d
    > p
    > }
    > d'
    2
    3
    $ cat test.in | sed -n -e '/^START$/,/^END$/!d' -e '/^START/d' -e '/^END$/d' -e p
    2
    3
    

    I’m using a BSD (mac) sed; on GNU systems you can use ; between lines instead of a newline. Or stick it in a script.

    1: Sed is Turing complete, so you can do math, but it’s unwieldy at best: http://rosettacode.org/wiki/A%2BB#sed

    Yes, I know, UUOC; it’s for illustration only

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I try to implement this simple-as-possible example: Update Content with AJAXRefreshRequest it does
Possible Duplicate: Is there a simple way of obtaining all object instances of a
Is it possible to classify html hex colours into simple string values?? For example,
Possible Duplicate: simple deadlock example in c# I've been dealing with multithreading in my
Possible Duplicate: Performing simple arithmetic in MySQL statement or in PHP code Im wondering
Possible Duplicate: A simple program to CRUD node and node values of xml file
Possible Duplicate: Dirt-simple PHP templates… can this work without eval ? Let's say I
Is it possible to nest simple programs within a Google Sheets, similar to how
I'll try to make this as simple as possible. I'm working on a site
I want my Makefile to be as simple as possible and still function. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.