File: sedawk.html

package info (click to toggle)
abs-guide 10-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,960 kB
  • sloc: sh: 14,129; makefile: 81
file content (1319 lines) | stat: -rw-r--r-- 18,930 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>A Sed and Awk Micro-Primer</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Reference Cards"
HREF="refcards.html"><LINK
REL="NEXT"
TITLE="Awk"
HREF="awk.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="APPENDIX"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="refcards.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="awk.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="APPENDIX"
><H1
><A
NAME="SEDAWK"
></A
>Appendix C. A Sed and Awk Micro-Primer</H1
><P
><A
NAME="SEDREF"
></A
></P
><P
>This is a very brief introduction to the <B
CLASS="COMMAND"
>sed</B
>
	and <B
CLASS="COMMAND"
>awk</B
> text processing utilities. We will
	deal with only a few basic commands here, but that will suffice
	for understanding simple sed and awk constructs within shell
	scripts.</P
><P
><B
CLASS="COMMAND"
>sed</B
>: a non-interactive
         text file editor</P
><P
><B
CLASS="COMMAND"
>awk</B
>: a field-oriented pattern processing
         language with a <B
CLASS="COMMAND"
>C</B
>-style syntax</P
><P
>For all their differences, the two utilities share a similar
	 invocation syntax, use <A
HREF="regexp.html#REGEXREF"
>regular
	 expressions </A
>, read input by default
	 from <TT
CLASS="FILENAME"
>stdin</TT
>, and output to
	 <TT
CLASS="FILENAME"
>stdout</TT
>. These are well-behaved UNIX tools,
	 and they work together well. The output from one can be piped
	 to the other, and their combined capabilities give shell scripts
	 some of the power of <A
HREF="wrapper.html#PERLREF"
>Perl</A
>.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>One important difference between the utilities is
	 that while shell scripts can easily pass arguments to sed, it
	 is more cumbersome for awk (see <A
HREF="wrapper.html#COLTOTALER"
>Example 36-5</A
>
	 and <A
HREF="ivr.html#COLTOTALER2"
>Example 28-2</A
>).
	 </P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN23170"
></A
>C.1. Sed</H1
><P
><I
CLASS="FIRSTTERM"
>Sed</I
> is a non-interactive
          <A
NAME="AEN23174"
HREF="#FTN.AEN23174"
>[1]</A
>
	<B
CLASS="COMMAND"
>s</B
>tream <B
CLASS="COMMAND"
>ed</B
>itor. It
	receives text input, whether from <TT
CLASS="FILENAME"
>stdin</TT
>
	or from a file, performs certain operations on specified lines
	of the input, one line at a time, then outputs the result to
	<TT
CLASS="FILENAME"
>stdout</TT
> or to a file.  Within a shell script,
	<I
CLASS="FIRSTTERM"
>sed</I
> is usually one of several tool
	components in a pipe.</P
><P
><I
CLASS="FIRSTTERM"
>Sed</I
> determines which lines of
        its input that it will operate on from the <I
CLASS="FIRSTTERM"
>address
        range</I
> passed to it.
	  <A
NAME="AEN23185"
HREF="#FTN.AEN23185"
>[2]</A
>
	Specify this address range either by line number or by a
	pattern to match. For example, <TT
CLASS="REPLACEABLE"
><I
>3d</I
></TT
>
	signals <I
CLASS="FIRSTTERM"
>sed</I
> to delete line 3 of the
	input, and <TT
CLASS="REPLACEABLE"
><I
>/Windows/d</I
></TT
> tells sed
	that you want every line of the input containing a match to
	<SPAN
CLASS="QUOTE"
>"Windows"</SPAN
> deleted.</P
><P
>Of all the operations in the <I
CLASS="FIRSTTERM"
>sed</I
>
	toolkit, we will focus primarily on the three most commonly
	used ones. These are <B
CLASS="COMMAND"
>p</B
>rinting (to
	<TT
CLASS="FILENAME"
>stdout</TT
>), <B
CLASS="COMMAND"
>d</B
>eletion,
	and <B
CLASS="COMMAND"
>s</B
>ubstitution.</P
><P
><A
NAME="SEDBASICTABLE"
></A
></P
><DIV
CLASS="TABLE"
><HR><A
NAME="AEN23200"
></A
><P
><B
>Table C-1. Basic sed operators</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Operator</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Name</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Effect</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>[address-range]/p</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>print</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Print [specified address range]</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>[address-range]/d</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>delete</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete [specified address range]</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>s/pattern1/pattern2/</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>substitute</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Substitute pattern2 for first instance of pattern1 in a line</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>[address-range]/s/pattern1/pattern2/</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>substitute</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Substitute pattern2 for first instance of pattern1 in a
	      line, over <TT
CLASS="REPLACEABLE"
><I
>address-range</I
></TT
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>[address-range]/y/pattern1/pattern2/</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>transform</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>replace any character in pattern1 with the
	        corresponding character in pattern2, over
	      <TT
CLASS="REPLACEABLE"
><I
>address-range</I
></TT
> (equivalent of
	        <B
CLASS="COMMAND"
>tr</B
>)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>[address] i pattern Filename</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>insert</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Insert pattern at address indicated in file Filename.
                     Usually used with <TT
CLASS="OPTION"
>-i</TT
>
                     <TT
CLASS="REPLACEABLE"
><I
>in-place</I
></TT
> option.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>g</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>global</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Operate on <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>every</I
></SPAN
> pattern match
	        within each matched line of input</TD
></TR
></TBODY
></TABLE
><HR></DIV
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Unless the <TT
CLASS="OPTION"
>g</TT
>
	(<I
CLASS="FIRSTTERM"
>global</I
>) operator is appended to a
	<I
CLASS="FIRSTTERM"
>substitute</I
> command, the substitution
	operates only on the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>first</I
></SPAN
> instance of a
	pattern match within each line.</P
></TD
></TR
></TABLE
></DIV
><P
>From the command-line and in a shell script, a sed operation may
       require quoting and certain options.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;sed -e '/^$/d' $filename
   2&nbsp;# The -e option causes the next string to be interpreted as an editing instruction.
   3&nbsp;#  (If passing only a single instruction to sed, the "-e" is optional.)
   4&nbsp;#  The "strong" quotes ('') protect the RE characters in the instruction
   5&nbsp;#+ from reinterpretation as special characters by the body of the script.
   6&nbsp;# (This reserves RE expansion of the instruction for sed.)
   7&nbsp;#
   8&nbsp;# Operates on the text contained in file $filename.</PRE
></TD
></TR
></TABLE
></P
><P
>In certain cases, a <I
CLASS="FIRSTTERM"
>sed</I
> editing command will
         not work with single quotes.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;filename=file1.txt
   2&nbsp;pattern=BEGIN
   3&nbsp;
   4&nbsp;  sed "/^$pattern/d" "$filename"  # Works as specified.
   5&nbsp;# sed '/^$pattern/d' "$filename"    has unexpected results.
   6&nbsp;#        In this instance, with strong quoting (' ... '),
   7&nbsp;#+      "$pattern" will not expand to "BEGIN".</PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><I
CLASS="FIRSTTERM"
>Sed</I
> uses the <TT
CLASS="OPTION"
>-e</TT
>
	 option to specify that the following string is an instruction
	 or set of instructions. If there is only a single instruction
	 contained in the string, then this may be omitted.</P
></TD
></TR
></TABLE
></DIV
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;sed -n '/xzy/p' $filename
   2&nbsp;# The -n option tells sed to print only those lines matching the pattern.
   3&nbsp;# Otherwise all input lines would print.
   4&nbsp;# The -e option not necessary here since there is only a single editing instruction.</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="SEDOPTABLE"
></A
></P
><DIV
CLASS="TABLE"
><HR><A
NAME="AEN23271"
></A
><P
><B
>Table C-2. Examples of sed operators</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Notation</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Effect</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>8d</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete 8th line of input.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>/^$/d</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete all blank lines.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>1,/^$/d</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete from beginning of input up to, and including
	        first blank line.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>/Jones/p</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Print only lines containing <SPAN
CLASS="QUOTE"
>"Jones"</SPAN
> (with
	        <SPAN
CLASS="TOKEN"
>-n</SPAN
> option).</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>s/Windows/Linux/</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Substitute <SPAN
CLASS="QUOTE"
>"Linux"</SPAN
> for first instance
	        of <SPAN
CLASS="QUOTE"
>"Windows"</SPAN
> found in each input line.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>s/BSOD/stability/g</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Substitute <SPAN
CLASS="QUOTE"
>"stability"</SPAN
> for every instance
	        of <SPAN
CLASS="QUOTE"
>"BSOD"</SPAN
> found in each input line.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>s/ *$//</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete all spaces at the end of every line.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>s/00*/0/g</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Compress all consecutive sequences of zeroes into
	        a single zero.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>echo "Working on it." | sed -e '1i How far are you along?'</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Prints "How far are you along?" as first line,
                    "Working on it" as second.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>5i 'Linux is great.' file.txt</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Inserts 'Linux is great.' at line 5 of the file
	      file.txt.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>/GUI/d</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete all lines containing <SPAN
CLASS="QUOTE"
>"GUI"</SPAN
>.</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>s/GUI//g</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Delete all instances of <SPAN
CLASS="QUOTE"
>"GUI"</SPAN
>, leaving the 
	        remainder of each line intact.</TD
></TR
></TBODY
></TABLE
><HR></DIV
><P
>Substituting a zero-length string for another is equivalent
       to deleting that string within a line of input. This leaves the
       remainder of the line intact. Applying <TT
CLASS="USERINPUT"
><B
>s/GUI//</B
></TT
>   
       to the line
       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="USERINPUT"
><B
>The most important parts of any application are its GUI and sound effects</B
></TT
></PRE
></TD
></TR
></TABLE
>
       results in
       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="COMPUTEROUTPUT"
>The most important parts of any application are its  and sound effects</TT
></PRE
></TD
></TR
></TABLE
></P
><P
>A backslash forces the <B
CLASS="COMMAND"
>sed</B
> replacement
       command to continue on to the next line. This has the effect of
       using the <I
CLASS="FIRSTTERM"
>newline</I
> at the end of the first
       line as the <I
CLASS="FIRSTTERM"
>replacement string</I
>.

       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;s/^  */\
   2&nbsp;/g</PRE
></TD
></TR
></TABLE
>

       This substitution replaces line-beginning spaces with a
       newline. The net result is to replace paragraph indents with a
       blank line between paragraphs.</P
><P
>An address range followed by one or more operations may require
       open and closed curly brackets, with appropriate newlines. 
       <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;/[0-9A-Za-z]/,/^$/{
   2&nbsp;/^$/d
   3&nbsp;}</PRE
></TD
></TR
></TABLE
>
       This deletes only the first of each set of consecutive blank
       lines. That might be useful for single-spacing a text file,
       but retaining the blank line(s) between paragraphs.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The usual delimiter that <I
CLASS="FIRSTTERM"
>sed</I
> uses is
      <SPAN
CLASS="TOKEN"
>/</SPAN
>. However, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>sed</I
></SPAN
> allows other
      delimiters, such as <SPAN
CLASS="TOKEN"
>%</SPAN
>. This is useful when
      <SPAN
CLASS="TOKEN"
>/</SPAN
> is part of a replacement string, as in a file pathname.
      See <A
HREF="loops.html#FINDSTRING"
>Example 11-10</A
> and <A
HREF="filearchiv.html#STRIPC"
>Example 16-32</A
>.</P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="DOUBLESPACE"
></A
></P
><DIV
CLASS="TIP"
><TABLE
CLASS="TIP"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/tip.png"
HSPACE="5"
ALT="Tip"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>A quick way to double-space a text file is <TT
CLASS="USERINPUT"
><B
>sed G
       filename</B
></TT
>.</P
></TD
></TR
></TABLE
></DIV
><P
>For illustrative examples of sed within shell scripts, see:
       <OL
TYPE="1"
><LI
><P
><A
HREF="wrapper.html#EX3"
>Example 36-1</A
></P
></LI
><LI
><P
><A
HREF="wrapper.html#EX4"
>Example 36-2</A
></P
></LI
><LI
><P
><A
HREF="moreadv.html#EX57"
>Example 16-3</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#RN"
>Example A-2</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#GRP"
>Example 16-17</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#COL"
>Example 16-27</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#BEHEAD"
>Example A-12</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#TREE"
>Example A-16</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#TREE2"
>Example A-17</A
></P
></LI
><LI
><P
><A
HREF="filearchiv.html#STRIPC"
>Example 16-32</A
></P
></LI
><LI
><P
><A
HREF="loops.html#FINDSTRING"
>Example 11-10</A
></P
></LI
><LI
><P
><A
HREF="mathc.html#BASE"
>Example 16-48</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#MAILFORMAT"
>Example A-1</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#RND"
>Example 16-14</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#WF"
>Example 16-12</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#LIFESLOW"
>Example A-10</A
></P
></LI
><LI
><P
><A
HREF="here-docs.html#SELFDOCUMENT"
>Example 19-12</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#DICTLOOKUP"
>Example 16-19</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#WHX"
>Example A-29</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#BASHPODDER"
>Example A-31</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#TOHTML"
>Example A-24</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#STOPWATCH"
>Example A-43</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#SEDAPPEND"
>Example A-55</A
></P
></LI
></OL
>
     </P
><P
>For a more extensive treatment of <I
CLASS="FIRSTTERM"
>sed</I
>,
        refer to the <A
HREF="biblio.html#DGSEDREF"
>pertinent references</A
>
        in the <A
HREF="biblio.html"
><I
>Bibliography</I
></A
>.</P
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN23174"
HREF="sedawk.html#AEN23174"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><I
CLASS="FIRSTTERM"
>Sed</I
> executes without
          user intervention.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN23185"
HREF="sedawk.html#AEN23185"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>If no address range is specified, the default
	  is <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>all</I
></SPAN
> lines.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="refcards.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="awk.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Reference Cards</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Awk</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>