0% found this document useful (0 votes)
31 views

AWK and SED

Uploaded by

ramthanish20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

AWK and SED

Uploaded by

ramthanish20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sed Examples

file.txt

Line 1
Line 2
Line 3
Line 4

Commands:

Print specific lines sed -n '1,3p' file.txt

To delete the lines : sed '2d' file.txt

To print particular line : sed -n -e '3 p' file.txt

To print last line : sed -n -e '$ p' file.txt

To print not in range : sed –n ‘ 3,6!p’ f1.txt

example.txt

Hello, world!
This is a test.
Welcome to the world of sed.
Enjoy your day!

Commands:
To substitute sed 's/world/universe/g' example.txt
Insert sed -i 's/world/universe/g' example.txt
Serach sed –n ‘/[hH]ello/p’ example.txt
PRINT LINE $ sed -n '$=' example.txt
Sed script as separate file

data.txt

Welcome to the programming world.


This is a sample file.
It contains several lines.
We love coding.
Enjoy your stay.

script.sed

# Replace 'programming' with 'coding'


s/programming/coding/g

# Delete the line that contains 'several'


/several/d

# Insert a new line after line 3


3a\
This line was added by sed.

Command: sed -f script.sed data.txt

AWK EXAMPLES

script.awk

BEGIN {

FS = "," # Set the field separator to a comma

print "Name Age"

print "-------------------"

{
if ($2 > 30) {

printf "%-18s %s\n", $1, $2 # Print the name and age with formatting

END {

print "End of report."

data.txt

John Doe,25

Jane Smith,30

Alice Johnson,28

Bob Brown,35

Charlie Green,40

Command :

1. awk -f script.awk data.txt

2. awk -F',' '{print $1, $2}' data.txt

You might also like