How to Specify Variables in sed

Asked 2 years ago, Updated 2 years ago, 81 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:

$row_number
$job_name

の 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.

sed

2022-09-30 11:43

2 Answers

Try repositioning '.

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

$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.

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

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
head-n"$(row_number-1))"$job_name"


2022-09-30 11:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.