$ sed 's/^\([0-9][0-9][0-9]\).*/<\1>/g' employee.txt
<101>
<102>
<103>
<104>
<105>
or via -r
$ sed -r 's/^([0-9][0-9][0-9]).*/<\1>/g' employee.txt
<101>
<102>
<103>
<104>
<105>
# or
$ sed -nr 's/^([0-9][0-9][0-9])(.*)/<\1>\2/gp' employee.txt
<101>,John Doe,CEO
<102>,Jason Smith,IT Manager
<103>,Raj Reddy,Sysadmin
<104>,Anand Ram,Developer
<105>,Jane Miller,Sales Manager
cheatsheet
reference:
more samples:
get first matching patten ( for CERTIFICATE )
[!TIP]
sample.crt
$ cat sample.crt
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
second paragraph
-----END CERTIFICATE-----
regular pattern
$ cat sample.crt | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
second paragraph
-----END CERTIFICATE-----
# or for short
$ cat sample.crt | sed -ne '/-BEGIN/,/-END/p'
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
second paragraph
-----END CERTIFICATE-----
get first
[!TIP]
more :
# or `-n /../p`
# `-n` `p`
# | |
# v v
$ cat sample.crt | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p; /-END CERTIFICATE-/q'
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
# or `/../!d`
# no `-n` `!d`
# | |
# v v
$ cat sample.crt | sed '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/!d; /-END CERTIFICATE-/q'
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
# or for short
$ cat sample.crt | sed '/-END CERTIFICATE-/q'
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
# or
$ cat sample.crt | sed '/-END/q'
-----BEGIN CERTIFICATE-----
first paragraph
-----END CERTIFICATE-----
remove both '#' and empty lines
[!NOTE|label:references:]
$ .. | sed -r '/^(#.*)$/d' | sed -r '/^\s*$/d'
# or
$ .. | sed -r '/^(#.*)$/d;/^\s*$/d'
# or
$ .. | sed -r '/(^#.*)|(^\s*)$/d'
example
$ ldapsearch CN=marslo DN | sed -r '/^(#.*)$/d;/^\s*$/d'
dn: CN=marslo,OU=Workers,DC=company,DC=com
remove tailing spaces
[!TIP|label:available patterns]
's/[ \t]*$//'
's/[[:blank:]]*$//'
's/[[:space:]]*$//'
$ cal | cat -pp -A
····January·2024····␊
Su·Mo·Tu·We·Th·Fr·Sa␊
····1··2··3··4··5··6␊
·7··8··9·10·11·12·13␊
14·15·16·17·18·19·20␊
21·22·23·24·25·26·27␊
28·29·30·31·········␊
····················␊
# remove empty line
$ cal | sed -r '/^(#.*)$/d;/^\s*$/d' | cat -pp -A
····January·2024····␊
Su·Mo·Tu·We·Th·Fr·Sa␊
····1··2··3··4··5··6␊
·7··8··9·10·11·12·13␊
14·15·16·17·18·19·20␊
21·22·23·24·25·26·27␊
28·29·30·31·········␊
# remove trailing spaces
$ cal | sed 's/[[:space:]]*$//' | cat -pp -A
····January·2024␊
Su·Mo·Tu·We·Th·Fr·Sa␊
····1··2··3··4··5··6␊
·7··8··9·10·11·12·13␊
14·15·16·17·18·19·20␊
21·22·23·24·25·26·27␊
28·29·30·31␊
␊
show top summary
contains empty line
$ top -bn1 | sed -n '0,/^\s*$/p'
top - 03:41:45 up 258 days, 19:05, 1 user, load average: 2.33, 0.92, 0.95
Tasks: 856 total, 2 running, 447 sleeping, 0 stopped, 36 zombie
%Cpu(s): 0.3 us, 0.4 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 52802012+total, 11152644+free, 24536944 used, 39195673+buff/cache
KiB Swap: 0 total, 0 free, 0 used. 49137280+avail Mem
without empty line
[!TIP|label:references:]
manual:
The "q" command prints the current line again in less the -n flag was used on the command line and exits the script completely
q[exit-code]
(quit) Exit sed without processing any more commands or input.
Q[exit-code]
(quit) This command is the same as q, but will not print the contents of pattern space. Like q, it provides the ability to return an exit code to the caller.
$ top -bn1 | sed -e '/^$/Q'
top - 03:45:55 up 258 days, 19:09, 1 user, load average: 0.17, 0.51, 0.77
Tasks: 857 total, 2 running, 448 sleeping, 0 stopped, 36 zombie
%Cpu(s): 0.1 us, 0.4 sy, 0.0 ni, 99.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 52802012+total, 11151089+free, 24546520 used, 39196272+buff/cache
KiB Swap: 0 total, 0 free, 0 used. 49136291+avail Mem