Shell (8) sed-gawk.doc

Source: Internet
Author: User
Tags file separator
Sed

Reads a row of data, matches the edited command, modifies the data, and then outputs it
Output

Basic Format

Sed options script file

-E
Execute multiple commands

-F
Specifies the command file, where the command does not need to"
;
"Split

-N
Disable output
P Flag

Eg: sed-E's/XX/; S/XXX/xxx 'data #
Use
;
, There is no space between the end of the command and the semicolon

 

Sed
Command

S,
By default, only text that appears for the first time is replaced.

S/pattern/replacement/flags



flags: G
replace all;
P
Print the original row, and
-n
combined to output modified rows;
W filename
write the result to
filename
; number, new text replacement mode, for example,
2
indicates replacing the
2
match


Sed
Medium
/
Used as escape,
/
You can also select other characters such as"
!
":
Sed
'S! /Bin/bash! /Bin/CSH! '/Etc/passwd

D,
Delete:
Sed '3, $ d' data

P,

I,
Insert a new row before the specified row

A,
Add a new line after the specified line. The two commands must specify the line to be inserted or appended separately.

Eg: sed '[address] command/
#
Required
/
Line feed. Multiple lines are used.
/


New
Line'

 

Line addressing
Line addressing

Enable
Sed
Apply to specific rows:

1)
Specifies the row number method. The row number of the first line is
1
,
$
Indicates the ending line:
Sed '2, $ S/dog/CAT/'data1

2)
Specify the matching mode,
/Pattern/command
:
Sed/'ri */S/dog/CAT/'data1



Specify multiple modes.
,
"Separator:
Sed '/1/,/3/d' data
#
Sed
In matching mode
/1/
Open the delete button until the mode is matched.
/3/
Close Deletion

 

Combined Command to execute multiple commands on the same line:

Sed
'2 {S/dog/CAT/; S/AAA/BBB/'Data

 

Sed
Generally, processing only a single line of text is a limitation for processing cross-line phrases.
N
,
D
,
P
And other commands

 

 

Gawk
Basic Format

Gawk Options program file

-F FS
Specifies the file Separator Used for data segmentation.
Field seperation character

-F File
, SpecifyProgramFile Name

-V Var = Value
, Variables and default values

-MF n
, The maximum number of fields to process

-Mr N
, Maximum record size

-W keyword
, Compatible mode or alarm level

 

Gawk
Script
{}
Must be enclosed in single quotes,
Gawk
Assume that the script is a single text string

Variable:
$0
Entire line of text,
$1
First field

Eg: gawk-F: '{print $1}'/etc/passwd

Separate multiple commands with semicolons:

Gawk '{$1 = "XXX"; print $0 }'
#
Can be assigned, use"
;
Separate multiple commands

Read programs from files:



File Content:
{Print $5 "'s userid is
"$1 };

 

$ Gawk-F:-F script/etc/passwd

 



Script file:
Script


{


TEXT = "'s
Userid is"

# No;


Print
$5 test $1

# Variable usage is not required
$



}

 

Before processing data
/
Run the script
Begin/end

Eg:


Echo
Adfdfadf | gawk 'in in {print "begin:" }{ Print $0} end {print
"End "}'

 

Variable:

1
)
Fieldwidths
: Use spaces to define the exact width of each data field.
FS
,
Eg:
'Begin{ fieldwidths = "2 3 1 "}'

2
)
FS
Variable, specifying the field separator,
Eg:
Gawk 'in in {FS = ":"}'

3
)
RS
, Specifies the record separator. The default value is line feed.

4
)
OFS
, Delimiter of the output field

5
)
ORS
The delimiter of the output record. The default value is line feed.

6
) Use environment variables,
Environ
["
Home
"], You can use any environment variable

7
)
NF
, Number of data fields,
$ NF
Last Data Field

8
)
FNR
, (Current file) current record number,

9
)
NR
, Number of processed records

 

Use Regular Expressions

It must appear before the left parenthesis of the script to match any record whose fields match the pattern. This can also be used.
Gawk
To directly locate the row.
Sed
?

Eg: gawk 'in in {FS = ","}/11/{print
$1} 'data1

 

Matching Operator
To make the regular expression match a specific field

Eg: gawk '$2 ~ /11/{print $0} 'data1

Negative match, use
! :

Eg: gawk '$2 !~ /11/{print $0} 'data1

Use mathematical expressions to match.
=
<=


> =
>
:

Eg: '$4 = 0 {print $0 }'
#
Cannot be used as a character

 

Format output

Printf "Format
String ", var1, var2...

Eg: printf "result is
% D/N ", X
#
Does not automatically wrap, so we need
/N

The format controller is the same
C
Voice, including
C/D/E/F/S/x
And so on

 

Built-in functions

Eg:

Gawk '{x = "aasf"; print
Toupper (x)} 'data

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.