How is this possible in shell using sed or any other filter
Lets say that i have two specific line numbers in two variables $line1
and $line2
and i want to extract lines between these two lines like this
cat my_file_path | command $line1 $line2
Example if my file is:
1:bbb
2:cccc
3:dddd
4:eeeee
My output should be if i do:
cat my_file_path | command 1 3
Output
bbb
cccc
p - Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. n - If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input.
The cut command offers many ways to extract portions of each line from a text file. It's similar to awk in some ways, but it has its own advantages and quirks. One surprisingly easy command for grabbing a portion of every line in a text file on a Linux system is cut.
Press Home key to get to the start of the line. For Selecting multiple lines, use Up/Down key.
Using sed:
$ cat my_file_path | sed -n "${line1},${line2}p"
or, even better (cat
is somehow redundant):
$ sed -n "${line1},${line2}p" my_file_path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With