The elegance of UNIX has always lain in its ability to string together relatively simple programs into pipelines to achieve complexity. You can do a sed
-only solution but it's not likely to be as readable as a pipeline.
To that end, you can use a combination of sed
to get a specific line and cut
to get character positions on that line:
pax> echo '12345
...> abcde
...> fghij' | sed -n 2p | cut -c2-4
bcd
If you just want to use a single tool, awk
can do it:
pax> echo '12345
...> abcde
...> fghij' | awk 'NR==2{print substr($0,2,3);exit}'
bcd
So can Perl:
pax> echo '12345
...> abcde
...> fghij' | perl -ne 'if($.==2){print substr($_,1,3); exit}'
In both those cases, it exits after the relevant line to avoid processing the rest of the file.