0% found this document useful (0 votes)
87 views2 pages

Grep Sed Lab - Tasks

The document provides solutions to grep and sed tasks/commands on a file. For grep, it gives answers to commands that select or count lines based on certain patterns or strings. For sed, it provides solutions to commands that delete, replace or swap parts of lines based on patterns.

Uploaded by

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

Grep Sed Lab - Tasks

The document provides solutions to grep and sed tasks/commands on a file. For grep, it gives answers to commands that select or count lines based on certain patterns or strings. For sed, it provides solutions to commands that delete, replace or swap parts of lines based on patterns.

Uploaded by

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

grep tasks with solutions

grep: ( grep,egrep,fgrep)
a) Write a grep command that selects the lines from the file1 that have exactly
three characters

Ans: $ grep ^…$ filename

b) Write a grep command that selects the lines from the file1 that have at least
three characters.

A: $ grep … filename

c) Write a grep command that selects the lines from the file1 that have three or
fewer characters

A: $ grep -v …. filename

d) Write a grep command that count the number blank lines in the file1

A: $ grep -c ^$ filename

e) Write a grep command that count the number nonblank lines in the file1

A: $ grep -cv ^$ filename

f) Write a grep command that selects the lines from the file1 that have the string
UNIX.

A: $ grep “UNIX” filename

g) Write a grep command that selects the lines from the file1 that have only the
string UNIX.

A: $ grep ^UNIX$ filename

h) Write a grep command that copy the file to the monitor, but delete the blank
lines.

A: $ grep -v ^$ filename

i) Write a grep command that selects the lines from the file1 that have at least
two digits without any other characters in between

A: $ grep [0-9][0-9] filename


j) Write a grep command that selects the lines from the file1 that do not start with
A to G

A: $ grep -v ^[A-G] filename

sed: Tasks-solutions
a) Write a sed command that print numbers of lines beginning with “O”

Ans: $ sed -ne ‘/^O/ =’ filename

b) Write a sed command that delete digits in the given input file.

A: $ sed ‘s/[0-9]*//g’ filename

c) Write a sed command that delete lines that contain both BEGIN and END

A: $ sed ‘/begin.*end/d ’ filename

d) Write a sed command that delete lines that contain BEGIN but not END

A: $ sed -ne ‘/begin/p’ filename | sed ‘/end/!d’

e) Write a sed command that deletes the first character in each line in a file

A: $ sed ‘s/^.//g ’ filename

f) Write a sed command that deletes the last character in each line in a file

A: $ sed ‘s/.$//g ‘ filename

g) Write a sed command to delete character before last character in each line in a file

A: $ sed ‘s,\(.*\)\(.\)\(.\),\1\3,g ’ filename

h) Write a sed command that swaps the first and second character in each line in the file

A: $ sed ‘s,\(.\)\(.\)\(.*\),\2\1\3,g ‘ filename

i) Write a sed command that swaps the first and second word in each line in the file

A: $ sed ‘s,\([^ ]*\) *\([^ ]*\),\2 \1 ,g ‘ filename

You might also like