1. Trang chủ
  2. » Công Nghệ Thông Tin

linux crash course chapter 13 3

19 59 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 19
Dung lượng 560,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

sed syntax• Syntax: sed [options] program [filelist] sed [options] program-file [filelist] • Program is a set of commands for editing – Can either be issued on the command line or place

Trang 1

Chapter 13:

sed

Say what?

Trang 2

In this chapter …

• Basics

• Programs

• Addresses

• Instructions

• Control

• Spaces

• Examples

Trang 3

• GNU sed (stream editor)

• Noninteractive, batch editing

• Good for repetitive tasks

• Often used in a pipe

Trang 4

sed syntax

• Syntax:

sed [options] program [filelist]

sed [options] program-file [filelist]

• Program is a set of commands for editing

– Can either be issued on the command line or

placed into a file (like gawk)

• Filelist is a list files to edit

– If omitted, input taken from standard in

Trang 5

sed syntax con’t

• Options

in-place[=suffix]

• Instead of sending edited text to standard out, write changes back to input file

• Adding =suffix makes backup of original file

-n

• Do not send lines to output unless program explicitly says to

Trang 6

• sed programs contain one or more lines with the following syntax:

[address[,address]] instruction [args]

• Simple one or two line programs can be

issued at the command line

• More complex programs are usually best put

in a program file

Trang 7

How sed works

1 Read one line of input

2 Read first instruction in program If the

address(es) select this line, runs the

instruction on this line

3 Repeat #2 for each line in the program

4 Read next line of input and go back to step

2, until there are no more lines of input

Trang 8

• Select which lines are to be processed

• Can be a simple integer (line number) or a regular expression (pattern matching)

• Address $ represents last line of input

• If address omitted, all lines processed by

default

• If there is one address, only lines that match will be processed

Trang 9

Addresses con’t

• If two addresses are given, it selects a range

• Once the first address is matched, it and

subsequent lines are processed until the

second address is matched

• If second address is never matched,

processes remainder of lines

• If second addressed matched, sed will then try to match first address again

Trang 10

• d – does not write out (deletes) selected line and does not process line any further

• n – writes out current line, reads next line, and processes next program line

• a – appends lines after current line

• i – inserts lines before current line

• c – changes select line so it contains new

text

• p – print current line (override –n)

Trang 11

Instructions con’t

• w file – write line to a specified file

• r file – read contents of file and appends

to current line

• q – quits sed immediately

Trang 12

Instructions con’t

• s/pattern/replacement-str/[g][p][w file]

– Substitutes first occurrence of pattern with

replacement-str

– g replaces all occurences

– p prints changed line

– w writes changed line to file

• Use & to represent the pattern matched when replacing

– Ex s/a.*/(a.*)/ won’t work … instead use

s/a.*/(&)/

Trang 13

Control Structures

• ! (NOT) – causes instruction to be performed

on all lines not selected by address(es)

• { } (Instruction grouping) – causes multiple

instructions to be run on one address /

address pair; separate with semicolons

• : label – identify a location in a sed program

• b label – branch to label

• t label – conditionally branch to label if last

Substitute instruction was successful

Trang 14

• sed has two spaces (buffers)

• Think of them like vim’s buffers

• Lines read from input are put in pattern

space

• You can also move data back and forth from

the hold space (temporary buffer)

Trang 15

Spaces, cont

• g – overwrites pattern space with hold space

• G – appends hold space to pattern space

• h – overwrites hold space with pattern space

• H – appends pattern space to hold space

• x – swaps the pattern and hold spaces

Trang 16

• sed -n‘/line/ p’ myfile

– Prints out lines in myfile that contain ‘line’

• sed ‘2,4 d’ myfile

– Delete lines 2-4, outputs remaining

• sed in-place ‘2,4 d’ myfile

– Deletes lines 2-4 from myfile

• sed ‘s/tea/coffee/g’ myfile

– Replaces tea with coffee and prints to screen

Trang 17

More Examples

• sed ‘5 q’ myfile

– Prints first five lines then quits ( equiv head -5)

• sed ‘/^[0-9]/ w newfile’ myfile

– Copies lines starting in number to newfile

• sed ‘$ r newfile’ myfile

– Appends contents of newfile to end of myfile

• sed ‘G’ myfile

– What does this do?

Trang 18

Program File Example

1 d

s/company/Company/g

$ a\

Revised 12-1-2005\

by JMH

$ d

Trang 19

Another Program File

1 i \

\

Manuf\tModel\tYear\tMiles\tPrice\

===================================== s/thundbd/tbird/g

s/.*/ &/

$ a \

=====================================

Ngày đăng: 06/02/2018, 09:55

TỪ KHÓA LIÊN QUAN