How to Specify Variables in sed

Asked 5 months ago, Updated 5 months ago, 21 views

< sed-e'$row_number, $d'$job_name

for any file ($job_name) the last line from any number of lines ($row_number) I would like to delete it.

We have assigned the following variables:


の The results of the run do not match your wishes.

As a result of the investigation, I am wondering if there is a problem with the '$row_number,$d' part. I don't know the answer.変数 I think there is a problem with how to specify variables.

One site said '' was unnecessary.However, the execution did not delete the specified line.

Please let me know.Thank you for your cooperation.


2022-09-30 11:43

2 Answers

Try repositioning '.


$row_number will be interpreted by shell and
Also, $d is surrounded by ' and is not interpreted by shell.

In addition, the above method will output the results to the standard output.If you want to rewrite the file you specified for $job_name, you can add -i.


If you rewrite it, it will not be restored, so please be careful.

2022-09-30 11:43

Is it like this?
Rewrite the -i option if you prefer it.

 If you want to do it with #sed,
sed-e "$row_number through 1d" <"$job_name"

# head-like
head-n "${row_number:+$(row_number-1)))}"$job_name"
# to simplify

2022-09-30 11:43

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.