Ade Malsasa Akbar contact
Senior author, Open Source enthusiast.
Thursday, July 21, 2016 at 16:46

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

  1. Episode 1 (Examples Number 1-10)
  2. Episode 2 (Examples Number 11-20)
  3. Episode 3 (Examples Number 21-30)

Text Examples


text9.txt:

unix unix unix
bsd:bsd:bsd
gnu-gnu-gnu
1. xnu
2. mach
3. linux
4. hurd

text10.txt:

unix unix bsd gnu
ultrix dunix tru64 unix
darwin
vms openvms
hp-ux sunos solaris minix
illumos inferno

text11.txt:

unix
aix
ultrix
xenix
irix
bsd
tru64
hp-ux
solaris
opensolaris
openindiana
illumos
darwin
gnu

31. Print Matched Lines


Command Example:

sed -n '/unix/p' text9.txt

Output Example:

master@master:/tmp$ sed -n '/unix/p' text9.txt
unix unix unix
master@master:/tmp$

Explanation:

This printing command is often used. This is a combination between the `-n` option and the ‘p’ command.

  • pcommand: 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


Command Examples:

sed -n '/unix/!p' text9.txt

Output Examples:

master@master:/tmp$ sed -n '/unix/!p' text9.txt
bsd:bsd:bsd
gnu-gnu-gnu
1. xnu
2. mach
3. linux
4. hurd
master@master:/tmp$

Explanation:

This command is exactly the same like the example 31 but the `!` sign. This explanation mark sign is a sign to reverse the matched result. So it will print anything except the matched regex string. In other word, in this example it will print lines that don’t have “unix” string.


33. Bash Looping with Delay Time


Command Example:

for i in *.txt; do sed 's/x/[X]/g' $i; echo " "; sleep 1; done

Output Example:

master@master:/tmp$ for i in *.txt; do sed 's/x/[X]/g' $i; echo " "; sleep 1; done
uni[X] uni[X] gnu bsd
tru64 duni[X] ultri[X]
hp-u[X] sunos solaris
darwin mini[X] vms

uni[X] bsd gnu mini[X]
[X]nu hurd linu[X] mach
[X]11 wayland mir
kde gnome [X]fce l[X]de
libreoffice openoffice abiword gnumeric
calligra te[X]maker gummi
ly[X] emacs vi vim

uni[X] uni[X] uni[X]
bsd:bsd:bsd
gnu-gnu-gnu
1. [X]nu
2. mach
3. linu[X]
4. hurd

master@master:/tmp$

Explanation:

This example is a basic example for bash looping command (already explained in Episode 3), with delay time added. This command does the same with the example number 23 in Episode 3, but it will pause a while for 1 second for every command loop. The advantage to use this delay time is to understand the command output more easily.

  • 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


Command Example:

sed -n '$ F' text9.txt

Output Example:

master@master:/tmp$ sed -n '$ F' text9.txt
text9.txt
master@master:/tmp$

Explanation:

This perhaps looks too easy, but it will be useful later for the next examples. This command ‘F’ (uppercase) prints the file name of every line sed reads. The address `$` makes sed read only the latest line, hence prints the single line of the file name. This command is equal with `echo $i` if it is being used in a shell script.

35. Print The First Line


Command Example:

sed ‘q’ text9.txt

Output Example:

master@master:/tmp$ sed ‘q’ text9.txt
unix unix unix
master@master:/tmp$

Explanation:

The ‘q’ command of sed prints the single first line of a text file or text stream.

36. Print The Last Line


Command Example:

sed -n '$ p' text9.txt

Output Example:

master@master:/tmp$ sed -n '$ p' text9.txt
4. hurd
master@master:/tmp$

Explanation:

We can’t use ‘$ q’ to print the last line, the ‘q’ command is not productive for this purpose. So we need an alternative and we surprisingly can use conjunction of `-n` option and ‘p’ command to do it. But, add the address `$` to force sed read only the last line.

37. Count Total Line Number


Command Example:

sed -n '$ =' text9.txt


Output Example:

master@master:/tmp$ sed -n '$ =' text9.txt
7
master@master:/tmp$

Explanation:

This example introduces the ‘=’ command, a command to print every line number of file being processed. Using `-n` option as a conjunction, and adding the address `$` force sed to print only the latest line number. In this example, the text9.txt file contains 7 lines so it prints the number 7.

38. Print Multiple Files’ First Lines (bash Looping)


Command Examples:

  1. for i in *.txt; do sed ‘q’ $i; done
  2. for i in *.txt; do sed -n '$ F' $i; sed 'q' $i; done

Output Examples:

master@master:/tmp$ for i in *.txt; do sed ‘q’ $i; done
unix unix bsd gnu
unix
unix unix unix
master@master:/tmp$

master@master:/tmp$ for i in *.txt; do sed -n '$ F' $i; sed 'q' $i; done
text10.txt
unix unix bsd gnu
text11.txt
unix
text9.txt
unix unix unix
master@master:/tmp$

Explanation:

This bash looping (just the same as the previously) emphasizes the sed ‘q’ command. By this command, sed take only the first line and print that line to the output. But bash ‘for’ command (bash looping) repeats the sed ‘q’ towards any of .txt file available in the same directory so it prints every first line of every file.

The second example takes advantage of ‘F’ commmand (see example 34) to put file name a line before every matched line so you can see the output clearer.

This example is basically a combination between example 23 (Episode 3) , example 37, and example 35.

39. Print Multiple Files’ Last Lines (bash Looping)


Command Examples:

  1. for i in *.txt; do sed -n '$ p' $i; done
  2. for i in *.txt; do sed -n '$ F' $i; sed -n '$ p' $i; done

Output Examples:

master@master:/tmp$ for i in *.txt; do sed -n '$ p' $i; done
illumos inferno
gnu
4. hurd
master@master:/tmp$

master@master:/tmp$ for i in *.txt; do sed -n '$ F' $i; sed -n '$ p' $i; done
text10.txt
illumos inferno
text11.txt
gnu
text9.txt
4. hurd
master@master:/tmp$

Explanation:

This example is basically a combination between example 23 (Episode 3) and example 36. Two command examples here are basically the same, but the one prints simply and the another add the file name for each matched line. The second command example makes use of ‘F’ command.

In the second command example here, we use special facility of bash, which is we can use more than one command inside a single line of looping command. So you see there are two sed commands, one command select and find the file name, and another one selects and print the last line of every text file.

40. Count Multiple Files Line Numbers (bash Looping)


Command Examples:

for i in *.txt; do sed -n '$ F; $ =' "$i"; done

Output Example:

master@master:/tmp$ for i in *.txt; do sed -n '$ F; $ =' "$i"; done
text10.txt
8
text11.txt
9
text9.txt
7
master@master:/tmp$

Explanation:

This example is basically a combination between example 23 (Episode 3), example 34, and example 37. The core of the bash looping here is the:

sed -n '$ F; $ =' "$i"

It means it first disables the sed “habit” of automatic printing every line. Then, it selects and prints the file name (see the output: every file name line). Last, it selects and prints every count of line number of file (see the output: every number line).