-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsed.html
More file actions
6947 lines (5711 loc) · 346 KB
/
Copy pathsed.html
File metadata and controls
6947 lines (5711 loc) · 346 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html><body><title>Sed Tutorial</title>
<h1>Sed Tutorial</h1>
<p>This tutorial takes you through all about Stream EDitor (Sed), one of the most prominent text-processing utilities on GNU/Linux. Similar to many other GNU/Linux utilities, it is stream-oriented and uses simple programming language. It is capable of solving complex text processing tasks with few lines of code. This easy, yet powerful utility makes GNU/Linux more interesting.</p>
<h1>Audience</h1>
<p>If you are a software developer, system administrator, or a GNU/Linux loving person, then this tutorial is for you.</p>
<h1>Prerequisites</h1>
<p>You must have basic understanding of GNU/Linux operating system and shell scripting.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Stream Editor - Overview</title>
<h1>Stream Editor - Overview</h1>
<p>The acronym SED stands for <b>Stream EDitor.</b> It is a simple yet powerful utility that parses the text and transforms it seamlessly. SED was developed during 1973–74 by Lee E. McMahon of Bell Labs. Today, it runs on all major operating systems.</p>
<p>McMahon wrote a general-purpose line-oriented editor, which eventually became SED. SED borrowed syntax and many useful features from ed editor. Since its beginning, it has support for regular expressions. SED accepts inputs from files as well as pipes. Additionally, it can also accept inputs from standard input streams.</p>
<p>SED is written and maintained by the Free Software Foundation (FSF) and it is distributed by GNU/Linux. Hence it is often referred to as <b>GNU SED.</b> To a novice user, the syntax of SED may look cryptic. However, once you get familiar with its syntax, you can solve many complex tasks with a few lines of SED script. This is the beauty of SED.</p>
<h2>Typical Uses of SED</h2>
<p>SED can be used in many different ways, such as:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Stream Editor - Environment</title>
<h1>Stream Editor - Environment</h1>
<p>This chapter describes how to set up the SED environment on your GNU/Linux system.</p>
<h2>Installation Using Package Manager</h2>
<p>Generally, SED is available by default on most GNU/Linux distributions. Use <b>which</b> command to identify whether it is present on your system or not. If not, then install SED on Debian based GNU/Linux using <b>apt</b> package manager as follows:</p>
<p>After installation, ensure that SED is accessible via command line.</p>
<p>On executing the above code, you get the following result:</p>
<p>Similarly, to install SED on RPM based GNU/Linux, use yum package manager as follows:</p>
<p>After installation, ensure that SED is accessible via command line.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Installation from Source Code</h2>
<p>As GNU SED is a part of the GNU project, its source code is available for free download. We have already seen how to install SED using package manager. Let us now understand how to install SED from its source code.</p>
<p>The following installation is applicable to any GNU/Linux software, and for most other freely-available programs as well. Here are the installation steps:</p>
<p>Download the source code from an authentic place. The command-line utility <b>wget</b> serves this purpose.</p>
<p>Decompress and extract the downloaded source code.</p>
<p>Change into the directory and run configure.</p>
<p>Upon successful completion, the <b>configure</b> generates Makefile. To compile the source code, issue a <b>make</b> command.</p>
<p>You can run the test suite to ensure the build is clean. This is an optional step.</p>
<p>Finally, install the SED utility. Make sure you have superuser privileges.</p>
<p>That is it! You have successfully compiled and installed SED. Verify it by executing the <b>sed</b> command as follows:</p>
<p>On executing the above code, you get the following result:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
[jerry]$ sudo apt-get install sed
</pre>
<p>After installation, ensure that SED is accessible via command line.</p>
<pre class="result notranslate">
[jerry]$ sed --version
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
Be sure to include the word "sed" somewhere in the "Subject:" field.
</pre>
<p>Similarly, to install SED on RPM based GNU/Linux, use yum package manager as follows:</p>
<pre class="result notranslate">
[root]# yum -y install sed
</pre>
<p>After installation, ensure that SED is accessible via command line.</p>
<pre class="result notranslate">
[root]# sed --version
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-utils@gnu.org>.
Be sure to include the word "sed" somewhere in the "Subject:" field.
</pre>
<h2>Installation from Source Code</h2>
<p>As GNU SED is a part of the GNU project, its source code is available for free download. We have already seen how to install SED using package manager. Let us now understand how to install SED from its source code.</p>
<p>The following installation is applicable to any GNU/Linux software, and for most other freely-available programs as well. Here are the installation steps:</p>
<ul class="list">
<li><p>Download the source code from an authentic place. The command-line utility <b>wget</b> serves this purpose.</p></li>
<pre class="result notranslate">
[jerry]$ wget ftp://ftp.gnu.org/gnu/sed/sed-4.2.2.tar.bz2
</pre>
<li><p>Decompress and extract the downloaded source code.</p></li>
<pre class="result notranslate">
[jerry]$ tar xvf sed-4.2.2.tar.bz2
</pre>
<li><p>Change into the directory and run configure.</p></li>
<pre class="result notranslate">
[jerry]$ ./configure
</pre>
<li><p>Upon successful completion, the <b>configure</b> generates Makefile. To compile the source code, issue a <b>make</b> command.</p></li>
<pre class="result notranslate">
[jerry]$ make
</pre>
<li><p>You can run the test suite to ensure the build is clean. This is an optional step.</p></li>
<pre class="result notranslate">
[jerry]$ make check
</pre>
<li><p>Finally, install the SED utility. Make sure you have superuser privileges.</p></li>
<pre class="result notranslate">
[jerry]$ sudo make install
</pre>
</ul>
<p>That is it! You have successfully compiled and installed SED. Verify it by executing the <b>sed</b> command as follows:</p>
<pre class="result notranslate">
[jerry]$ sed --version
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
Be sure to include the word "sed" somewhere in the "Subject:" field.
</pre>
<title>Stream Editor - Workflow</title>
<h1>Stream Editor - Workflow</h1>
<p>In this chapter, we will explore how SED exactly works. To become an expert SED user, one needs to know its internals. SED follows a simple workflow: Read, Execute, and Display. The following diagram depicts the workflow.</p>
<p><b>Read</b>: SED reads a line from the input stream (file, pipe, or stdin) and stores it in its internal buffer called <b>pattern buffer</b>.</p>
<p><b>Execute</b>: All SED commands are applied sequentially on the pattern buffer. By default, SED commands are applied on all lines (globally) unless line addressing is specified.</p>
<p><b>Display</b>: Send the (modified) contents to the output stream. After sending the data, the pattern buffer will be empty.</p>
<p>The above process repeats until the file is exhausted.</p>
<h2>Points to Note</h2>
<p>Pattern buffer is a private, in-memory, volatile storage area used by the SED.</p>
<p>By default, all SED commands are applied on the pattern buffer, hence the input file remains unchanged. GNU SED provides a way to modify the input file in-a-place. We will explore about it in later sections.</p>
<p>There is another memory area called <b>hold buffer</b> which is also private, in- memory, volatile storage area. Data can be stored in a hold buffer for later retrieval. At the end of each cycle, SED removes the contents of the pattern buffer but the contents of the hold buffer remains persistent between SED cycles. However SED commands cannot be directly executed on hold buffer, hence SED allows data movement between the hold buffer and the pattern buffer.</p>
<p>Initially both pattern and hold buffers are empty.</p>
<p>If no input files are provided, then SED accepts input from the standard input stream (stdin).</p>
<p>If address range is not provided by default, then SED operates on each line.</p>
<h3>Examples</h3>
<p>Let us create a text file <b>quote.txt</b> to contain a quote of the famous author Paulo Coelho.</p>
<p>To understand the workflow of SED, let us display the contents of the file quote.txt using SED. This example simulates the <b>cat</b> command.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>In the above example, quote.txt is the input file name and before that there is a pair of single quote that implies the SED command. Let us demystify this operation.</p>
<p>First SED reads a line from the input file quote.txt and stores it in its pattern buffer. Then it applies SED commands on the pattern buffer. In our case, no SED commands are there, hence no operation is performed on the pattern buffer. Finally it deletes and prints the contents of the pattern buffer on the standard output. Isn't it simple?</p>
<p>In the following example, SED accepts input from the standard input stream.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>Here, the first line is entered through keyboard and the second is the output generated by SED. To exit from the SED session, press ctrl-D (^D).</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result">
[jerry]$ vi quote.txt
There is only one thing that makes a dream impossible to achieve: the fear of failure.
- Paulo Coelho, The Alchemist
</pre>
<p>To understand the workflow of SED, let us display the contents of the file quote.txt using SED. This example simulates the <b>cat</b> command.</p>
<pre class="result notranslate">
[jerry]$ sed '' quote.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
There is only one thing that makes a dream impossible to achieve: the fear of failure.
</pre>
<p>In the above example, quote.txt is the input file name and before that there is a pair of single quote that implies the SED command. Let us demystify this operation.</p>
<p>First SED reads a line from the input file quote.txt and stores it in its pattern buffer. Then it applies SED commands on the pattern buffer. In our case, no SED commands are there, hence no operation is performed on the pattern buffer. Finally it deletes and prints the contents of the pattern buffer on the standard output. Isn't it simple?</p>
<p>In the following example, SED accepts input from the standard input stream.</p>
<pre class="result notranslate">
[jerry]$ sed '' <press enter>
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
There is only one thing that makes a dream impossible to achieve: the fear of failure.
There is only one thing that makes a dream impossible to achieve: the fear of failure.
</pre>
<title>Stream Editor - Basic syntax</title>
<h1>Stream Editor - Basic Syntax</h1>
<p>This chapter introduces the basic commands that SED supports and their command-line syntax. SED can be invoked in the following two forms:</p>
<p>The first form allows to specify the commands in-line and they are enclosed within single quotes. The later allows to specify a script file that contains SED commands. However, we can use both forms together multiple times. SED provides various command-line options to control its behavior.</p>
<p>Let us see how we can specify multiple SED commands. SED provides the <b>delete</b> command to delete certain lines. Let us delete the 1st, 2nd, and 5th lines. For the time being, ignore all the details of the delete command. We will discuss more about the delete command later.</p>
<p>First, display the file contents using the <b>cat</b> command.</p>
<p>On executing the above code, you get the following result:</p>
<p>Now instruct SED to remove only certain lines. Here, to delete three lines, we have specified three separate commands with -e option.</p>
<p>On executing the above code, you get the following result:</p>
<p>Additionally, we can write multiple SED commands in a text file and provide the text file as an argument to SED. SED can apply each command on the pattern buffer. The following example illustrates the second form of SED.</p>
<p>First, create a text file containing SED commands. For easy understanding, let us use the same SED commands.</p>
<p>On executing the above code, you get the following result:</p>
<p>Now instruct the SED to read commands from the text file. Here, we achieve the same result as shown in the above example.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Standard Options</h2>
<p>SED supports the following standard options:</p>
<p>-n: Default printing of pattern buffer. For example, the following SED command does not show any output:</p>
<p>-e <cmd> : Next argument is an editing command. Here, angular brackets imply mandatory parameter. By using this option, we can specify multiple commands. Let us print each line twice:</p>
<p>On executing the above code, you get the following result:</p>
<p>-f <filename> : Next argument is a file containing editing commands. The angular brackets imply mandatory parameter. In the following example, we specify print command through file:</p>
<p>On executing the above code, you get the following result:</p>
<h2>GNU Specific Options</h2>
<p>Let us quickly go through the GNU specific SED options. Note that these options are GNU specific; and may not be supported by other variants of the SED. In later sections, we will discuss these options in more details.</p>
<p>-n, --quiet, --silent: Same as standard -n option.</p>
<p>-e script, --expression=script: Same as standard -e option.</p>
<p>-f script-file, --file=script-file: Same as standard -f option.</p>
<p>--follow-symlinks: If this option is provided, the SED follows symbolic links while editing files in place.</p>
<p>-i[SUFFIX], --in-place[=SUFFIX]: This option is used to edit file in place. If suffix is provided, it takes a backup of the original file, otherwise it overwrites the original file.</p>
<p>-l N, --line-lenght=N: This option sets the line length for l command to N characters.</p>
<p>--posix: This option disables all GNU extensions.</p>
<p>-r, --regexp-extended: This option allows to use extended regular expressions rather than basic regular expressions. </p>
<p>-u, --unbuffered: When this option is provided, the SED loads minimal amount of data from the input files and flushes the output buffers more often. It is useful for editing the output of "tail -f" when you do not want to wait for the output.</p>
<p>-z, --null-data: By default, the SED separates each line by a new-line character. If NULL-data option is provided, it separates the lines by NULL characters.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
sed [-n] [-e] 'command(s)' files
sed [-n] -f scriptfile files
</pre>
<p>The first form allows to specify the commands in-line and they are enclosed within single quotes. The later allows to specify a script file that contains SED commands. However, we can use both forms together multiple times. SED provides various command-line options to control its behavior.</p>
<p>Let us see how we can specify multiple SED commands. SED provides the <b>delete</b> command to delete certain lines. Let us delete the 1st, 2nd, and 5th lines. For the time being, ignore all the details of the delete command. We will discuss more about the delete command later.</p>
<p>First, display the file contents using the <b>cat</b> command.</p>
<pre class="result notranslate">
[jerry]$ cat books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>Now instruct SED to remove only certain lines. Here, to delete three lines, we have specified three separate commands with -e option.</p>
<pre class="result notranslate">
[jerry]$ sed -e '1d' -e '2d' -e '5d' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>Additionally, we can write multiple SED commands in a text file and provide the text file as an argument to SED. SED can apply each command on the pattern buffer. The following example illustrates the second form of SED.</p>
<p>First, create a text file containing SED commands. For easy understanding, let us use the same SED commands.</p>
<pre class="result notranslate">
[jerry]$ echo -e "1d\n2d\n5d" > commands.txt
[jerry]$ cat commands.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
1d
2d
5d
</pre>
<p>Now instruct the SED to read commands from the text file. Here, we achieve the same result as shown in the above example.</p>
<pre class="result notranslate">
[jerry]$ sed -f commands.txt books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
6) A Game of Thrones,George R. R. Martin, 864
</pre>
<h2>Standard Options</h2>
<p>SED supports the following standard options:</p>
<ul class="list">
<li><p>-n: Default printing of pattern buffer. For example, the following SED command does not show any output:</p></li>
<pre class="result notranslate">
[jerry]$ sed -n '' quote.txt
</pre>
<li><p>-e <cmd> : Next argument is an editing command. Here, angular brackets imply mandatory parameter. By using this option, we can specify multiple commands. Let us print each line twice:</p></li>
<pre class="result notranslate">
[jerry]$ sed -e '' -e 'p' quote.txt
</pre>
</ul>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
There is only one thing that makes a dream impossible to achieve: the fear of failure.
There is only one thing that makes a dream impossible to achieve: the fear of failure.
- Paulo Coelho, The Alchemist
- Paulo Coelho, The Alchemist
</pre>
<ul class="list">
<li><p>-f <filename> : Next argument is a file containing editing commands. The angular brackets imply mandatory parameter. In the following example, we specify print command through file:</p></li>
</ul>
<pre class="result notranslate">
[jerry]$ echo "p" > commands
[jerry]$ sed -n -f commands quote.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
There is only one thing that makes a dream impossible to achieve: the fear of failure.
- Paulo Coelho, The Alchemist
</pre>
<title>Stream Editor - Loops</title>
<h1>Stream Editor - Loops</h1>
<p>Like other programming languages, SED too provides a looping and branching facility to control the flow of execution. In this chapter, we are going to explore more about how to use loops and branches in SED.</p>
<p>A loop in SED works similar to a <b>goto</b> statement. SED can jump to the line marked by the label and continue executing the remaining commands. In SED, we can define a <b>label</b> as follows:</p>
<p>In the above example, a name after colon(:) implies the label name.</p>
<p>To jump to a specific label, we can use the <b>b</b> command followed by the label name. If the label name is omitted, then the SED jumps to the end of the SED file.</p>
<p>Let us write a simple SED script to understand the loops and branches. In our books.txt file, there are several entries of book titles and their authors. The following example combines a book title and its author name in one line separated by a comma. Then it searches for the pattern "Paulo". If the pattern matches, it prints a hyphen(-) in front of the line, otherwise it jumps to the <b>Print</b> label which prints the line.</p>
<p>On executing the above code, you get the following result:</p>
<p>At first glance, the above script may look cryptic. Let us demystify this.</p>
<p>The first two commands are self-explanatory <b>h;n;H;x</b> and <b>s/\n/, /</b> combine the book title and its author separated by a comma(,).</p>
<p>The third command jumps to the label <b>Print</b> only when the pattern does not match, otherwise substitution is performed by the fourth command.</p>
<p><b>:Print</b> is just a label name and as you already know, <b>p</b> is the print command.</p>
<p>To improve readability, each SED command is placed on a separate line. However, one can choose to place all the commands in one line as follows:</p>
<p>On executing the above code, you get the following result:</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
:label
:start
:end
:up
</pre>
<p>In the above example, a name after colon(:) implies the label name.</p>
<p>To jump to a specific label, we can use the <b>b</b> command followed by the label name. If the label name is omitted, then the SED jumps to the end of the SED file.</p>
<p>Let us write a simple SED script to understand the loops and branches. In our books.txt file, there are several entries of book titles and their authors. The following example combines a book title and its author name in one line separated by a comma. Then it searches for the pattern "Paulo". If the pattern matches, it prints a hyphen(-) in front of the line, otherwise it jumps to the <b>Print</b> label which prints the line.</p>
<pre class="result notranslate">
[jerry]$ sed -n '
h;n;H;x
s/\n/, /
/Paulo/!b Print
s/^/- /
:Print
p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
A Storm of Swords, George R. R. Martin
The Two Towers, J. R. R. Tolkien
- The Alchemist, Paulo Coelho
The Fellowship of the Ring, J. R. R. Tolkien
- The Pilgrimage, Paulo Coelho
</pre>
<pre class="result">
A Game of Thrones, George R. R. Martin
</pre>
<p>At first glance, the above script may look cryptic. Let us demystify this.</p>
<ul class="list">
<li><p>The first two commands are self-explanatory <b>h;n;H;x</b> and <b>s/\n/, /</b> combine the book title and its author separated by a comma(,).</p></li>
<li><p>The third command jumps to the label <b>Print</b> only when the pattern does not match, otherwise substitution is performed by the fourth command.</p></li>
<li><p><b>:Print</b> is just a label name and as you already know, <b>p</b> is the print command.</p></li>
</ul>
<p>To improve readability, each SED command is placed on a separate line. However, one can choose to place all the commands in one line as follows:</p>
<pre class="result notranslate">
[jerry]$ sed -n 'h;n;H;x;s/\n/, /;/Paulo/!b Print; s/^/- /; :Print;p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
A Storm of Swords, George R. R. Martin
The Two Towers, J. R. R. Tolkien
- The Alchemist, Paulo Coelho
The Fellowship of the Ring, J. R. R. Tolkien
- The Pilgrimage, Paulo Coelho
A Game of Thrones, George R. R. Martin
</pre>
<title>Stream Editor - Branches</title>
<h1>Stream Editor - Branches</h1>
<p>Branches can be created using the t command. The <b>t</b> command jumps to the label only if the previous substitute command was successful. Let us take the same example as in the previous chapter, but instead of printing a single hyphen(-), now we print four hyphens. The following example illustrates the usage of the <b>t</b> command.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>In the above example, the first two commands are self-explanatory. The third command defines a label <b>Loop</b>. The fourth command prepends hyphen(-) if the line contains the string "Paulo" and the <b>t</b> command repeats the procedure until there are four hyphens at the beginning of the line.</p>
<p>To improve readability, each SED command is written on a separate line. Otherwise, we can write a one-liner SED as follows:</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
[jerry]$ sed -n '
h;n;H;x
s/\n/, /
:Loop
/Paulo/s/^/-/
/----/!t Loop
p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
A Storm of Swords, George R. R. Martin
The Two Towers, J. R. R. Tolkien
----The Alchemist, Paulo Coelho
The Fellowship of the Ring, J. R. R. Tolkien
----The Pilgrimage, Paulo Coelho
A Game of Thrones, George R. R. Martin
</pre>
<p>In the above example, the first two commands are self-explanatory. The third command defines a label <b>Loop</b>. The fourth command prepends hyphen(-) if the line contains the string "Paulo" and the <b>t</b> command repeats the procedure until there are four hyphens at the beginning of the line.</p>
<p>To improve readability, each SED command is written on a separate line. Otherwise, we can write a one-liner SED as follows:</p>
<pre class="result notranslate">
[jerry]$ sed -n 'h;n;H;x; s/\n/, /; :Loop;/Paulo/s/^/-/; /----/!t Loop; p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
A Storm of Swords, George R. R. Martin
The Two Towers, J. R. R. Tolkien
----The Alchemist, Paulo Coelho
The Fellowship of the Ring, J. R. R. Tolkien
----The Pilgrimage, Paulo Coelho
A Game of Thrones, George R. R. Martin
</pre>
<title>Stream Editor - Pattern Buffer</title>
<h1>Stream Editor - Pattern Buffer</h1>
<p>One of the basic operations we perform on any file is display its contents. For this purpose, we can use the <b>print</b> command which prints the contents of the pattern buffer. So let us learn more about the pattern buffer</p>
<p>First create a file containing the line number, the name of the book, its author, and the number of pages. In this tutorial, we will be using this file. You can use any text file according to your convenience. Our text file will look like this:</p>
<p>Now, let us print the file contents.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>You might wonder why each line is being displayed twice. Let us find out.</p>
<p>Do you remember the workflow of SED? By default, SED prints the contents of the pattern buffer. In addition, we have included a print command explicitly in our command section. Hence each line is printed twice. But don't worry. SED has the <b>-n</b> option to suppress the default printing of the pattern buffer. The following command illustrates that.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>Congratulations! we got the expected result. By default, SED operates on all lines. But we can force SED to operate only on certain lines. For instance, in the example below, SED only operates on the 3rd line. In this example, we have specified an address range before the SED command.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>Additionally, we can also instruct SED to print only certain lines. For instance, the following code prints all the lines from 2 to 5. Here we have used the comma(,) operator to specify the address range.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>There is also a special character Dollar($) which represents the last line of the file. So let us print the last line of the file.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>However we can also use Dollar($) character to specify address range. Below example prints through line 3 to last line.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>We learnt how to specify an address range using the comma(,) operator. SED supports two more operators that can be used to specify address range. First is the plus(+) operator and it can be used with the comma(,) operator. For instance <b>M, +n</b> will print the next <b>n</b> lines starting from line number <b>M</b>. Sounds confusing? Let us check it with a simple example. The following example prints the next 4 lines starting from line number 2.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>Optionally, we can also specify address range using the tilde(~) operator. It uses <b>M~n</b> form. It indicates that SED should start at line number M and process every n(th) line. For instance, <b>50~5</b> matches line number 50, 55, 60, 65, and so on. Let us print only odd lines from the file.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>The following code prints only even lines from the file.</p>
<p>When the above code is executed, it will produce the following result.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result">
[jerry]$ vi books.txt
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho,288
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>Now, let us print the file contents.</p>
<pre class="result notranslate">
[jerry]$ sed 'p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
1) A Storm of Swords, George R. R. Martin, 1216
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>You might wonder why each line is being displayed twice. Let us find out.</p>
<p>Do you remember the workflow of SED? By default, SED prints the contents of the pattern buffer. In addition, we have included a print command explicitly in our command section. Hence each line is printed twice. But don't worry. SED has the <b>-n</b> option to suppress the default printing of the pattern buffer. The following command illustrates that.</p>
<pre class="result notranslate">
[jerry]$ sed -n 'p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>Congratulations! we got the expected result. By default, SED operates on all lines. But we can force SED to operate only on certain lines. For instance, in the example below, SED only operates on the 3rd line. In this example, we have specified an address range before the SED command.</p>
<pre class="result notranslate">
[jerry]$ sed -n '3p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
3) The Alchemist, Paulo Coelho, 197
</pre>
<p>Additionally, we can also instruct SED to print only certain lines. For instance, the following code prints all the lines from 2 to 5. Here we have used the comma(,) operator to specify the address range.</p>
<pre class="result notranslate">
[jerry]$ sed -n '2,5 p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
</pre>
<p>There is also a special character Dollar($) which represents the last line of the file. So let us print the last line of the file.</p>
<pre class="result notranslate">
[jerry]$ sed -n '$ p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>However we can also use Dollar($) character to specify address range. Below example prints through line 3 to last line.</p>
<pre class="result notranslate">
[jerry]$ sed -n '3,$ p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>We learnt how to specify an address range using the comma(,) operator. SED supports two more operators that can be used to specify address range. First is the plus(+) operator and it can be used with the comma(,) operator. For instance <b>M, +n</b> will print the next <b>n</b> lines starting from line number <b>M</b>. Sounds confusing? Let us check it with a simple example. The following example prints the next 4 lines starting from line number 2.</p>
<pre class="result notranslate">
[jerry]$ sed -n '2,+4 p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>Optionally, we can also specify address range using the tilde(~) operator. It uses <b>M~n</b> form. It indicates that SED should start at line number M and process every n(th) line. For instance, <b>50~5</b> matches line number 50, 55, 60, 65, and so on. Let us print only odd lines from the file.</p>
<pre class="result notranslate">
[jerry]$ sed -n '1~2 p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
1) A Storm of Swords, George R. R. Martin, 1216
3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelho, 288
</pre>
<p>The following code prints only even lines from the file.</p>
<pre class="result notranslate">
[jerry]$ sed -n '2~2 p' books.txt
</pre>
<p>When the above code is executed, it will produce the following result.</p>
<pre class="result">
2) The Two Towers, J. R. R. Tolkien, 352
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<title>Stream Editor - Pattern Range</title>
<h1>Stream Editor - Pattern Range</h1>
<p>In the previous chapter, we learnt how SED handles an address range. This chapter covers how SED takes care of a pattern range. A pattern range can be a simple text or a complex regular expression. Let us take an example. The following example prints all the books of the author Paulo Coelho.</p>
<p>On executing the above code, you get the following result:</p>
<p>In the above example, the SED operates on each line and prints only those lines that match the string Paulo.</p>
<p>We can also combine a pattern range with an address range. The following example prints lines starting with the first match of Alchemist until the fifth line.</p>
<p>On executing the above code, you get the following result:</p>
<p>We can use the Dollar($) character to print all the lines after finding the first occurrence of the pattern. The following example finds the first occurrence of the pattern The and immediately prints the remaining lines from the file</p>
<p>On executing the above code, you get the following result:</p>
<p>We can also specify more than one pattern ranges using the comma(,) operator. The following example prints all the lines that exist between the patterns Two and Pilgrimage.</p>
<p>On executing the above code, you get the following result:</p>
<p>Additionally, we can use the plus(+) operator within a pattern range. The following example finds the first occurrence of the pattern Two and prints the next 4 lines after that.</p>
<p>On executing the above code, you get the following result:</p>
<p>We have supplied here only a few examples to get you acquainted with SED. You can always get to know more by trying a few examples on your own.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
[jerry]$ sed -n '/Paulo/ p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelho, 288
</pre>
<p>In the above example, the SED operates on each line and prints only those lines that match the string Paulo.</p>
<p>We can also combine a pattern range with an address range. The following example prints lines starting with the first match of Alchemist until the fifth line.</p>
<pre class="result notranslate">
[jerry]$ sed -n '/Alchemist/, 5 p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
</pre>
<p>We can use the Dollar($) character to print all the lines after finding the first occurrence of the pattern. The following example finds the first occurrence of the pattern The and immediately prints the remaining lines from the file</p>
<pre class="result notranslate">
[jerry]$ sed -n '/The/,$ p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<p>We can also specify more than one pattern ranges using the comma(,) operator. The following example prints all the lines that exist between the patterns Two and Pilgrimage.</p>
<pre class="result notranslate">
[jerry]$ sed -n '/Two/, /Pilgrimage/ p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
</pre>
<p>Additionally, we can use the plus(+) operator within a pattern range. The following example finds the first occurrence of the pattern Two and prints the next 4 lines after that.</p>
<pre class="result notranslate">
[jerry]$ sed -n '/Two/, +4 p' books.txt
</pre>
<p>On executing the above code, you get the following result:</p>
<pre class="result">
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
</pre>
<title>Stream Editor - Basic Commands</title>
<h1>Stream Editor - Basic Commands</h1>
<p>This chapter describes several useful SED commands.</p>
<h2>Delete Command</h2>
<p>SED provides various commands to manipulate text. Let us first explore about the <b>delete</b> command. Here is how you execute a delete command:</p>
<p><b>address1</b> and <b>address2</b> are the starting and the ending addresses respectively, which can be either line numbers or pattern strings. Both of these addresses are optional parameters.</p>
<p>As the name suggests, the delete command is used to perform delete operation and since the SED operates on line, we can say that this command is used to delete lines. Note that the delete command removes lines only from the pattern buffer; the line is not sent to the output stream and the original file remains unchanged. The following example illustrates the point.</p>
<p>But where is the output? If no line address is provided, then the SED operates on every line by default. Hence, it deletes all the lines from the pattern buffer. That is why the command does not print anything on the standard output.</p>
<p>Let us instruct the SED to operate only on certain lines. The following example removes the 4th line only.</p>
<p>On executing the above code, you get the following result:</p>
<p>Additionally, SED also accepts address range using comma(,). We can instruct the SED to remove N1 to N2 lines. For instance, the following example deletes all the lines from 2 through 4.</p>
<p>On executing the above code, you get the following result:</p>
<p>SED's address range is not only limited to numbers. We can also specify patterns as an address. The following example removes all the books of the author Paulo Coelho.</p>
<p>On executing the above code, you get the following result:</p>
<p>We can also specify an address range using textual pattern. The following example removes all lines between the patterns <b>Storm</b> and <b>Fellowship</b>.</p>
<p>In addition to this, we can also use dollar($), plus(+), and tilde(~) operators with SED.</p>
<h2>Write Command</h2>
<p>One of the important operations we perform on any file is backup, i.e., we make another copy of the file. SED provides the <b>write</b> command to store the contents of the pattern buffer in a file. Given below is the syntax of the <b>write</b> command which is similar to the <b>delete</b> command.</p>
<p>Here, <b>address1</b> and <b>address2</b> are the starting and the ending address respectively, which can be either line numbers or pattern strings. Both of these addresses are optional parameters.</p>
<p>In the above syntax, <b>w</b> refers to the write command and <b>file</b> is the file name in which you store contents. Be careful with the <b>file</b> parameter. When a file name is provided, the SED creates a file on the fly if it is not present, and overwrites it if it is already present.</p>
<p>Let us make an exact copy of the file using SED. Note that there must be exactly one space between <b>w</b> and <b>file</b>.</p>
<p>We created another file called <b>books.bak.</b> Now verify that both the files have identical content.</p>
<p>On executing the above code, you get the following result:</p>
<p>You may assume that the <b>cp</b> command does exactly the same thing. Yes! The <b>cp</b> command does the same thing, but SED is a matured utility. It allows creating a file containing only certain lines from the source file. Let us store only even lines to another file.</p>
<p>On executing the above code, you get the following result:</p>
<p>You can also use comma(,), dollar($), and plus(+) operators with the write command.</p>
<p>In addition to this, SED also supports pattern matching with the write command. Suppose you want to store all the books of individual authors into a separate file. One boring and lengthy way is do it manually, and the smarter way is to use SED.</p>
<p>In the above example, we are matching each line against a pattern and storing the matched line in a particular file. It is very simple. To specify multiple commands, we used <b>-e</b> switch of the SED command. Now let use see what each file contains:</p>
<p>On executing the above code, you get the following result:</p>
<p>Let us display the file contents.</p>
<p>On executing the above code, you get the following result:</p>
<p>Let us display the file contents.</p>
<p>On executing the above code, you get the following result:</p>
<p>Excellent! We got the expected result. SED is really an amazing utility.</p>
<h2>Append Command</h2>
<p>One of the most useful operations of any text editor is to provide append functionality. SED supports this operation through its append command. Given below is the syntax of append:</p>
<p>Let us append a new book entry after line number 4. The following example shows how to do it</p>
<p>On executing the above code, you get the following result:</p>
<p>In the command section, <b>4</b> implies the line number, <b>a</b> is the append command, and the remaining part is the text to be appended.</p>
<p>Let us insert a text line at the end of the file. To do this, use <b>$</b> as the address. The following example illustrates this:</p>
<p>On executing the above code, you get the following result:</p>
<p>Apart from line number, we can also specify an address using textual pattern. For instance, the following example appends text after matching the string <b>The Alchemist</b>.</p>
<p>On executing the above code, you get the following result:</p>
<p>Note that if there are multiple patterns matching, then the text is appended after each match. The following example illustrates this scenario.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Change Command</h2>
<p>SED provides <b>change</b> or <b>replace</b> command which is represented by c. This command helps replace an existing line with new text. When line range is provided, all the lines are replaced as a group by a single text line. Given below is the syntax of the change command:</p>
<p>Let us replace the third line with some other text.</p>
<p>On executing the above code, you get the following result:</p>
<p>SED also accepts patterns as an address. In the following example, a line is replaced when the pattern match succeeds.</p>
<p>SED also allows replacement of multiple lines with a single line. The following example removes lines from fourth through sixth and replaces them with new text.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Insert Command</h2>
<p>The insert command works much in the same way as append does. The only difference is that it inserts a line before a specific position. Given below is the syntax of the insert command:</p>
<p>Let us understand the insert command with some examples. The following command inserts a new entry before the fourth line.</p>
<p>On executing the above code, you get the following result:</p>
<p>In the above example, <b>4</b> is the location number, <b>i</b> implies the insert command, and the remaining part is the text to be inserted.</p>
<p>To insert text at the start of a file, provide the line address as <b>1</b>. The following command illustrates this:</p>
<p>On executing the above code, you get the following result:</p>
<p>Additionally, we can insert multiple lines. The following command inserts two lines before the last line.</p>
<p>On executing the above code, you get the following result:</p>
<p>Note that the entries to be inserted are entered on separate lines and delimited by the backslash(\) character.</p>
<h2>Translate Command</h2>
<p>SED provides a command to translate characters and it is represented as <b>y</b>. It transforms the characters by position. Given below is the syntax of the translate command:</p>
<p>Note that translation is based on the position of the character from <b>list 1</b> to the character in the same position in <b>list 2</b> and both lists must be explicit character lists. Regular expressions and character classes are unsupported. Additionally, the size of <b>list 1</b> and <b>list 2</b> must be same.</p>
<p>The following example converts Arabic numbers to Roman numbers.</p>
<p>On executing the above code, you get the following result:</p>
<h2>l command</h2>
<p>Can you differentiate between words separated by spaces and words separated by tab characters only by looking at them? Certainly not. But SED can do this for you. SED uses the <b>l</b> command to display hidden characters in the text. For example, tab character with <b>\t</b> and End-Of-Line with <b>$</b> character. Given below is the syntax of the <b>l</b> command.</p>
<p>Let us create a file with tab characters for demonstration. For simplicity, we are going to use the same file, just by replacing spaces with tabs. Wait! But how to do that — by opening the file in a text editor and replacing each space with tab? Certainly not! We can make use of SED commands for that.</p>
<p>Now let us display the hidden characters by using the <b>l</b> command:</p>
<p>On executing the above code, you get the following result:</p>
<p>Like other SED commands, it also accepts line numbers and patterns as an address. You can try it yourselves.</p>
<p>Let us take a close look at another interesting feature of SED. We can instruct the SED to perform line wrapping after a certain number of characters. The following example wraps lines after 25 characters.</p>
<p>On executing the above code, you get the following result:</p>
<p>Note that in the above example, wrap limit is provided after l command. In this case, it is 25 characters. This option is GNU specific and may not work with other variants of the SED.</p>
<p>A wrap limit of 0 means never break the line unless there is a new line character. The following simple command illustrates this.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Quit Command</h2>
<p>Quit command instructs the SED to quit the current execution flow. It is represented by the <b>q</b> command. Given below is the syntax of the quit command:</p>
<p>Note that the quit command does not accept range of addresses, it only supports a single address. By default, SED follows read, execute, and repeat workflow; but when the quit command is encountered, it simply stops the current execution.</p>
<p>Let us print the first 3 lines from the file.</p>
<p>On executing the above code, you get the following result:</p>
<p>In addition to line number, we can also use textual patterns. The following command quits when pattern match succeeds.</p>
<p>On executing the above code, you get the following result:</p>
<p>In addition to this, SED can also accept a <b>value</b> which can be used as the exit status. The following command shows its exit status as 100.</p>
<p>On executing the above code, you get the following result:</p>
<p>Now let us verify the exit status.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Read Command</h2>
<p>We can instruct the SED to read the contents of a file and display them when a specific condition matches. The command is represented by the alphabet <b>r</b>. Given below is the syntax of the read command.</p>
<p>Note that there must be exactly one space between the <b>r</b> command and the file name.</p>
<p>Let us understand it with a simple example. Create a sample file called <b>junk.txt</b>.</p>
<p>The following command instructs the SED to read the contents of <b>junk.txt</b> and insert them after the third line.</p>
<p>On executing the above code, you get the following result:</p>
<p>In the above example, 3 implies the line address, <b>r</b> is the command name, and <b>junk.txt</b> is the file name the contents of which are to be displayed. Additionally, the GNU SED also accepts a range of addresses. For instance, the following command inserts the contents of <b>junk.txt</b> after the third, fourth, and fifth lines.</p>
<p>On executing the above code, you get the following result:</p>
<p>Like other SED commands, the read command also accepts pattern as an address. For instance, the following command inserts the contents of <b>junk.txt</b> when the pattern match succeeds.</p>
<p>On executing the above code, you get the following result:</p>
<h2>Execute Command</h2>
<p>We can execute external commands from SED using the <b>execute</b> command. It is represented by <b>e</b>. Given below is the syntax of the execute command.</p>
<p>Let us illustrate the execute command with a simple example. The following SED command executes the UNIX <b>date</b> command before the third line.</p>
<p>On executing the above code, you get the following result:</p>
<p>Like other commands, it also accepts patterns as an address. For example, the following example executes <b>date</b> command when a pattern match succeeds. Note that after each pattern match, first the command is executed and then the contents of the pattern buffer are displayed.</p>
<p>On executing the above code, you get the following result:</p>
<p>If you observe the syntax of the <b>e</b> command carefully, you will notice that <b>command</b> is optional. When no command is provided after <b>e,</b> it treats the contents of the pattern buffer as an external command. To illustrate this, let us create a commands.txt file with a few simple commands.</p>
<p>On executing the above code, you get the following result:</p>
<p>Commands from the file are self-explanatory. In the absence of <b>command</b> after <b>e,</b> SED executes all these commands one by one. The following simple example illustrates this.</p>
<p>On executing the above code, you get the following result:</p>
<p>Like other SED commands, the execute command also accepts all valid ranges of addresses.</p>
<h2>Miscellaneous Commands</h2>
<p>By default, SED operates on single line, however it can operate on multiple lines as well. Multi-line commands are denoted by uppercase letters. For example, unlike the <b>n</b> command, the <b>N</b> command does not clear and print the pattern space. Instead, it adds a newline (\n) at the end of the current pattern space and appends the next line from the input-file to the current pattern space and continues with the SED's standard flow by executing the rest of the SED commands. Given below is the syntax of the <b>N</b> command.</p>
<p>Let us print a comma-separated list of book titles and their respective authors. The following example illustrates this.</p>
<p>On executing the above code, you get the following result:</p>
<p>Let us understand how the above example works. The <b>N</b> command reads the first line, i.e.,A Storm of Swords into the pattern buffer and appends \n followed by the next line. The pattern space now contains A Storm of Swords<b>\n</b>George R. R. Martin. In the next step, we are replacing the newline with a comma.</p>
<p>Like <b>p</b> command, we have a <b>P</b> command to print the first part (up to embedded newline) of the multi-line pattern space created by the <b>N</b> command. Given below is the syntax of the <b>P</b> command which is similar to the <b>p</b> command.</p>
<p>In the previous example, we saw that the <b>N</b> command creates a newline- separated list of book titles and their authors. Let us print only the first part of it, i.e., only the titles of the book. The following command illustrates this.</p>
<p>On executing the above code, you get the following result:</p>
<p>Note that in the absence of <b>N</b>, it behaves same as the <b>p</b> command. The following simple command illustrates this scenario.</p>
<p>On executing the above code, you get the following result:</p>