top of page
Search

Sed and Awk

  • Writer: Oliver Santana
    Oliver Santana
  • Dec 9, 2019
  • 3 min read


A small exercise I completed for reference on how to use Sed and Awk in Linux


Sed (Use the file attached called datebook)


Read the chapter from https://flylib.com/books/en/4.356.1.40/1/ then complete this part of the lab


1. Change the name Jon to Jonathan .

sed -n ‘s/Jon/Jonathan/p’ datebook


2. Delete the first three lines.

sed ‘1,3d’ datebook


3. Print lines 5 through 10 .

sed -n ‘5,10p’ datebook


4. Delete lines containing Lane .

sed -e ‘Lane/d’ datebook


5. Print all lines where the birthdays are in November or December .

sed -rn ‘/:(11|12)\//p’ datebook


6. Append three asterisks to the end of lines starting with Fred .

sed -e ‘/Fred/a\***’ datebook


7. Replace the line containing Jose with JOSE HAS RETIRED .

sed -e ‘s/Jose/JOSE HAS RETIRED/p’ datebook


8. Change Popeye 's birthday to 11/14/46 . Assume you don't know Popeye's original birthday. Use a regular expression to search for it.

sed -e ‘/Popeye/s,[0-9]\+/[0-9]\+/[0-9]\+,11/14/46,’ datebook


9. Delete all blank lines.

sed ‘/^$/d’ datebook


10. Write a sed script that will


vi script.sed #creates script

chmod +x script.sed #makes script executable

sed -f script.sed datebook #runs script


a. Insert above the first line the title PERSONNEL FILE .

1i\PERSONNEL FILE


b. Remove the salaries ending in 500 .

/500$/d


c. Print the contents of the file with the last names and first names reversed .

s/^\([^ ]*\) \([^: ]*\)/\z \1/


d. Append at the end of the file THE END .

$a\THE END




Awk (use the file attached called lab3.data)

Read the chapter at https://flylib.com/books/en/4.356.1.52/1/ and then complete this part of the lab

The database contains the names , phone numbers , and money contributions to the party campaign for the past three months.


1. Print all the phone numbers.

awk ‘{print $2}’ FS=’:’ lab3.data


2. Print Dan 's phone number.

awk ‘/ Dan/{print $2}’ FS=’:’ lab3.data


3. Print Susan 's name and phone number.

awk ‘/Susan/{print $1, $2}’ FS=’:’ lab3.data


4. Print all last names beginning with D .

awk ‘$2 ~ /^D/{print $2}’ FS=’[: ]’ lab3.data


5. Print all first names beginning with either a C or E .

awk ‘$1 ~ /^[CE]/{print $1}’ FS=’[: ]’ lab3.data


6. Print all first names containing only four characters .

awk ‘length($1)==4{print $1}’ lab3.data


7. Print the first names of all those in the 916 area code.

awk ‘$3 ~ /(916)/{print $1}’ FS=’[: ]’ lab3.data


8. Print Mike 's campaign contributions. Each value should be printed with a leading dollar sign; e.g., $250 $100 $175.

awk ‘$1 ~ /Mike/{print “$”$5,”$”$6,”$”$7}’ FS=’[: ]’ lab3.data


9.Print last names followed by a comma and the first name.

awk ‘{print $2 “,” $1}’ FS=’[: ]’ lab3.data


10. Write an awk script called facts that

vi facts.awk #creates an awk script named facts

#!/usr/bin/awk -f #written at the top of the file so you can run awk commands

Begin{FS=”:”}


a. Prints full names and phone numbers for the Savages .

$1 ~ /Savage/{print $1,$2}

$2 ~ /Savage/{print $1,$2,$3,$4}


b. Prints Chet 's contributions.

$1 ~/Chet/{print $3,$4,$5}


c. Prints all those who contributed $250 the first month.

$3 ~ /250/{print $0}


Awk (use the lab4.data file)

The database contains the names , phone numbers , and money contributions to the party campaign for the past three months.


Print the first and last names of those who contributed more than $100 in the second month.

awk ‘$4>100 {print $1}’ FS=’:’ lab4.data


Print the names and phone numbers of those who contributed less than $85 in the last month.

awk ‘$5>85{print $1,$2}’ FS=’:’ lab4.data


Print the names of those who contributed between $75 and $150 in the first month.

awk ‘$3>75 && $3<150{print $1}’ FS=’:’ lab4.data


Print the names of those who contributed less than $800 over the three-month period.

awk ‘{total=$3+$4+$5} total<800{print $1}’ FS=’:’ lab4.data


Print the names and addresses of those with an average monthly contribution greater than $200 .

awk ‘{mean=($3+$4+$5)/3} 200<mean{print $1,$2}’ FS=’:’ lab4.data


Print the first name of those not in the 916 area code.

awk ‘$3 !~ /916/{print $1}’ FS=’[: ]’ lab4.data


Print each record preceded by the number of the record.

awk ‘{print NR, $0}’ FS=’:’ lab4.data


Print the name and total contribution of each person.

awk ‘{total=$3+$4+$5} {print $1, total}’ FS=’:’ lab4.data


Add $10 to Chet 's second contribution.

awk ‘$1 ~ /Chet/{print $4+10}’ FS=’:’ lab4.data


Change Nancy McNeil 's name to Louise McInnes .

Awk ‘{gsub(Nancy McNeil/,”Louise McInnes”);} {print $1,$2}’ lab4.data

 
 
 

Comments


©2019 by Oliver Santana. Proudly created with Wix.com

bottom of page