This article is a
more 10 examples continuation from the Episode 3 of our GNU sed
command lines the series. This article covers how to combine sed with
bash looping with delay time and sed line printings in general. This
covers some examples about how to count line numbers, printing the first/last line, in single and multiple files. This article mixes some of the previous examples so if you miss something here we suggest you to read them first.
GNU sed Examples Episodes
- Episode 1 (Examples Number 1-10)
- Episode 2 (Examples Number 11-20)
- Episode 3 (Examples Number 21-30)
31. Print Matched Lines
‘p’ command: print the line matched the search keyword. So in this example, it will print every line containing “unix” string.
Important Note: sed has a “habit” to print every line and then to print the search result (the matched line) until the end. So the `p` command will print the whole lines plus print the matched line.
‘-n’ option: disable that sed’s habit. Using `-n` in conjunction with ‘p’ gives result sed prints only the matched lines.
32. Print Reverse-Matched Lines
33. Bash Looping with Delay Time
The command: `echo “ ”`: it is not necessary, but it gives every single loop output one blank line so you will read the three looped commands clearer.
The command: `sleep 1`: it is the delay command. It gives bash loop delay for 1 second. You may change the number as you wish.
34. Print The File Name
35. Print The First Line
36. Print The Last Line
37. Count Total Line Number
38. Print Multiple Files’ First Lines (bash Looping)
for i in *.txt; do sed ‘q’ $i; done
for i in *.txt; do sed -n '$ F' $i; sed 'q' $i; done
39. Print Multiple Files’ Last Lines (bash Looping)
for i in *.txt; do sed -n '$ p' $i; done
for i in *.txt; do sed -n '$ F' $i; sed -n '$ p' $i; done