Shell scripting: print first line and last line only
Is there a way to print the first line of a file and the last line of a file with one command?
For example: 1 2 3 4 5 and I only want it to be 1 5 Assume that I don't know how many lines are in between the first and last line. The only way I could think of was to do: head -n 1 filename > tempfile tail -n 1 filename >> tempfile |
Code:
sed -n '1p;$p' filename > newfile |
That works great! now, how about something a little more trickier. What if the list looked like this and I wanted to print from "/program/" to the end
apple oranges 789 /program/ 1 2 3 4 5 Assume that my list is random, but I always want to start from "/program/" until the end. And also, would there be a way to start and stop at a certain point? For example, start at "/program/" and stop at "3" |
Hi,
You can use regular expression to express line numbers in sed. I.e: 'Normal': sed -n '15,$p' infile Would print line 15 (included) to end of file. Using regexp: sed -n'/\/program\//,$p' infile Would print from the first found /program/ to end of file. Hope this helps. |
In the case of searching from one regexp to another, you have a situation where the second regexp is a number.
Code:
If you use this... Code:
sed -n '/\/program\//,/3/p' file.txt |
Quote:
Code:
awk 'NR==1 {print}END{print }' file Quote:
Code:
awk '/program/,$NR{print}' "file" Quote:
Code:
awk '/program/,/^3/{print}' "file" |
All times are GMT -5. The time now is 07:09 PM. |