2 added 1 character in body; edited title
source | link

Use Perform sed operations on given line numbers

I am wondering whether there is a simple way to operate on certain lines with preassigned line numbers.

Let's say I want to output the 1st, 7th, 14th and 16th linelines of a file, I can simply do

sed -n '1p;7p;14p;16p' input_file

but this gets more complicated when the operation is not just printing, and I don't want to write the same long command 4 times (and yes, I know I can construct this long sed command by substituting the same bash variable 4 times, but that's not ideal enough ...) , i.e.

sed -n '1{long_command};7{long_command};14{long_command};16{long_command}' input_file

Is there a way to do the operation on these specific lines of my file? I am expecting something like,

sed -n '1,7,14,16p'

which certainly will not work in the current form.

Any help will be appreciated. "No, it is not possible." with explanations is also an answer that I will accept.

Use sed operations on given line numbers

I am wondering whether there is a simple way to operate on certain lines with preassigned line numbers.

Let's say I want to output the 1st, 7th, 14th and 16th line of a file, I can simply do

sed -n '1p;7p;14p;16p' input_file

but this gets more complicated when the operation is not just printing, and I don't want to write the same long command 4 times (and yes, I know I can construct this long sed command by substituting the same bash variable 4 times, but that's not ideal enough ...) , i.e.

sed -n '1{long_command};7{long_command};14{long_command};16{long_command}' input_file

Is there a way to do the operation on these specific lines of my file? I am expecting something like,

sed -n '1,7,14,16p'

which certainly will not work in the current form.

Any help will be appreciated. "No, it is not possible." with explanations is also an answer that I will accept.

Perform sed operations on given line numbers

I am wondering whether there is a simple way to operate on certain lines with preassigned line numbers.

Let's say I want to output the 1st, 7th, 14th and 16th lines of a file, I can simply do

sed -n '1p;7p;14p;16p' input_file

but this gets more complicated when the operation is not just printing, and I don't want to write the same long command 4 times (and yes, I know I can construct this long sed command by substituting the same bash variable 4 times, but that's not ideal enough ...) , i.e.

sed -n '1{long_command};7{long_command};14{long_command};16{long_command}' input_file

Is there a way to do the operation on these specific lines of my file? I am expecting something like,

sed -n '1,7,14,16p'

which certainly will not work in the current form.

Any help will be appreciated. "No, it is not possible." with explanations is also an answer that I will accept.

1
source | link

Use sed operations on given line numbers

I am wondering whether there is a simple way to operate on certain lines with preassigned line numbers.

Let's say I want to output the 1st, 7th, 14th and 16th line of a file, I can simply do

sed -n '1p;7p;14p;16p' input_file

but this gets more complicated when the operation is not just printing, and I don't want to write the same long command 4 times (and yes, I know I can construct this long sed command by substituting the same bash variable 4 times, but that's not ideal enough ...) , i.e.

sed -n '1{long_command};7{long_command};14{long_command};16{long_command}' input_file

Is there a way to do the operation on these specific lines of my file? I am expecting something like,

sed -n '1,7,14,16p'

which certainly will not work in the current form.

Any help will be appreciated. "No, it is not possible." with explanations is also an answer that I will accept.