0

I tried to extract text from line N to line M in bash to a separate file, with N and M to be variable. For example, I want to extract from line 3 to line 5. I set

N=3
M=5

and do

sed -n 'N,Mp' input > output

It doesn't work for me.

Romeo Ninov
  • 4,621
  • 1
  • 16
  • 24
xinshou
  • 35
  • 3
  • You need to reference `M` and `N` as shell variables and double quote the string.which is passed to sed. This would read `sed -n "${M},${N}p" file > output` (be aware that m is alphabetically before n, this has nothing to do with your answer or question, but just a generic remark that you might want to swap the values assigned to m and n) – kvantour Jan 04 '20 at 19:07

1 Answers1

2

You can do this with sample awk script:

awk -v s=$N -v n=$M 'NR>s && NR<n' input >output

with sed will be something like:

sed -n "${N},${M}p" input >output
Romeo Ninov
  • 4,621
  • 1
  • 16
  • 24