• Aucun résultat trouvé

sed and awk

N/A
N/A
Protected

Academic year: 2022

Partager "sed and awk"

Copied!
18
0
0

Texte intégral

(1)

sed and awk

CS 2204

(2)

sed

Stream editor

Originally derived from ed line editor

Used primarily for non interactive operations

operates on data streams, hence its name

Usage:

sed options 'address action' file(s)

Example: sed '1$s/^bold/BOLD/g' foo

(3)

sed: Line Addressing

using line numbers (like 1,3p)

sed '3,4p' foo.txt

sed '4q' foo.txt

sed –n '3,4p' foo.txt

print quit

(4)

sed: Line addressing (... continued)

sed –n '$p' foo.txt

Reversing line criteria (!)

sed –n '3,$!p' foo.txt

last line

negation

(5)

sed: Context Addressing

Use patterns/regular expressions rather than explicitly specifying line numbers

sed –n '/^From: /p' $HOME/mbox

retrieve all the sender lines from the mailbox file

ls –l | sed –n '/^...w/p'

(6)

sed: Substitution

Strongest feature of sed

Syntax is

[address]s/pattern/replacement/flag

sed 's/|/:/' data.txt

substitute the character '|' with the character ':'

sed 's/|/:/g' data.txt

global substitute

(7)

sed: Using files

Tedious to type in commands at the prompt, especially if commands are repetitive

Can put commands in a file and sed can use them

sed –f cmds.sed data.txt

file with cmds

(8)

sed example script

/^[A-Z]/i\

NEW SENTENCE /[tT]rash/d

s/GWB/George W. Bush/

(9)

awk

Powerful pattern scanning and processing language

Names after its creators Aho, Weinberger and Kernighan

Most commands operate on entire line

awk operates on fields within each line

Usage:

(10)

awk: Processing model

BEGIN {commands executed before any input is read}

{

Main input loop for each line of input

}

END {commands executed after all

input is read}

(11)

awk: First example

# Begin Processing

BEGIN {print "Print Totals"}

# Body Processing

{total = $1 + $2 + $3}

{print $1 " + " $2 " + " $3 " =

"total}

(12)

Input and output files

Input 22 78 44 66 31 70 52 30 44 88 31 66

Output

Print Totals

22 + 78 + 44 = 144 66 + 31 + 70 = 167 52 + 30 + 44 = 126 88 + 31 + 66 = 185 End Totals

(13)

awk: command line processing

Input

1 clothing 3141 1 computers 9161 1 textbooks 21312 2 clothing 3252 2 computers 12321

Output

1 computers 9161 2 computers 12321 awk '$2 == "computers" {print}' sales.dat

(14)

awk: Other features

Formatted printing using printf

Conditional statements (if-else)

Loops

for

while

do-while

(15)

awk: Associative arrays

Normal arrays use integers for their indices

Associative arrays with strings as their indices

Example: Age[“Robert”] = 56

(16)

awk: Example

# salesDeptLoop.awk script BEGIN {OFS = "\t"}

{deptSales [$2] += $3}

END {for (item in deptSales) {

print item, ":", deptSales[item]

totalSales += deptSales[item]

} # for

print "Total Sales", ":", totalSales } # END

(17)

Input and output

Input

1 clothing 3141 1 computers 9161 1 textbooks 21312 2 clothing 3252 2 computers 12321 2 supplies 2242

Output

computers : 21482 supplies : 2242 textbooks : 36774 clothing : 6393

Total Sales : 66891

(18)

A final awk example

BEGIN {count=0; sum=0}

{if($1>=10 && $1 <= 100){

count++

sum+=$1 }

else{

print $1,"is not a double digit number"

} }

Références

Documents relatifs

The READ command causes digital data (high byte, low byte, or status) to be transferred to the host iSBC microcomputer and the WRITE command transfers the channel

Pro-Log CS18 includes connector, hood, hardware, and key. Part numbers given for other manufac- turers are for connectors and hoods only.. The functional operation of the

wc compter les lignes, mots, octets -l juste les lignes -w juste les mots -c juste les octets -m pour le nombre de caractères -L ligne la plus longue.. Sur les champs

With a parallel printer port, three serial ports (one standard and two optional), and a game port, the Magic I/O packs more features and connectors on a true

If a program writes to its standard output, which is normally the screen, you can make it write to a file instead by using the greater-than symbol ( &gt; ) operator.. The pipe

Ecrit un byte, en fait un int pour qu'il marche avec le read. void write(int b) Ne

Abstract —Analog input Class D Amplifiers are widely used in battery powered systems such as mobile phones to achieve high efficiency but it suffers from a complex

ADVERTENCIA: Lea todas las advertencias de seguridad, instrucciones y marcas de precaución para el paquete de la batería, el cargador y el producto.. La falla en seguir