copying a line from a file using sed

Hi All,

I need to copy a specific line from a file to another file.
lets suppose the line number 13 of a file
when I am writing the line number explicitly.. its working fine

sed -n '13p' afile > anotherfile

but, when inside a script, i am getting the line number value inside a variable called line, i am not getting the expected result.

sed -n '$linep' afile > anotherfile

even I tried with

sed -n '"$line"p' afile > anotherfile

I am getting the error :

sed: "$line"p is not a recognized function.

Please help !!!

Hi,

See next example:

$ cat infile
last name,first name,address,zip,telephone,year of birth
Appleby,Anne,Shrimpsbury,SH-124,555-4455,1960
Brown,Carl,Midsomer Garden,MD-945,555-2423,1982
Smith,John,Shrimpsbury,SH-123,555-4584,1978
Underwood,Mary,Oglsby,OG-345,555-3434,1956
$ cat script.sh
#!/usr/bin/env bash

line=3
sed -n "$line"'p' infile
$ bash script.sh
Brown,Carl,Midsomer Garden,MD-945,555-2423,1982

Regards,
Birei

Oh Thanks Birei.. Believe, I have got it from your example.
I was using

sed -n '"$line"p' infile

rather, it should be

sed -n "$line"'p' infile

I am not with my system now. Will check and confirm later. Thanks for the help.

Also works:

$ cat script.sh
#!/usr/bin/env bash                                                                                                                                                                                                                          
                                                                                                                                                                                                                                             
line=3                                                                                                                                                                                                                                       
sed -n "${line}p" infile
$ bash script.sh
Brown,Carl,Midsomer Garden,MD-945,555-2423,1982 

Regards,
Birei

line=13
sed -n ''$line'p' infile

Note: these are two single quotes followed by a $ and then word line

tmp>wc -l austa1106.002
3079 austa1106.002
tmp>sed -n '13 p' austa1106.002
LA999888
tmp>export myln=13
tmp>sed -n ''$myln' p' austa1106.002
LA999888
tmp>