Assignment (2) Linux
Assignment (2) Linux
01. Write a sed command that prints number of lines that begins with
‘0’.
02. Write a sed command that deletes digits in the given input file.
[sed 's/[0-9]//g' input_file > output_file ]
03. Write a sed command that deletes lines that contain both BEGIN and
END.
[sed '/BEGIN.*END/d' input_file > output_file]
04. Write a sed command that deletes lines that contain BEGIN but not
END.
[sed '/BEGIN/!b; /END/d; :loop; n; /BEGIN/!bloop; /END/d; b loop'
input_file > output_file]
05. Write a sed command that deletes first character in each line of a file.
[sed 's/^.//' input_file > output_file]
06. Write a sed command that deletes last character in each line of a file.
[sed 's/.$//' input_file > output_file]
07. Write a sed command that deletes character before last character in
each line of a file.
[sed 's/.$//; s/.$//' input_file > output_file]
08. Write a sed command to swap first and second character in each line
of a file.
[sed 's/^\(.\)\(.\)\(.*\)$/\2\1\3/' input_file > output_file]
09. Write a sed command to swap first and last word in each line of a
file.
[sed -r 's/^(\S+)(.*)(\S+)$/ \3\2\1/' input_file > output_file]
Grep Commands:
02. Write a grep command to print the lines that has the pattern "July".
[grep "July" file.txt]
03. Write a grep command to print the lines that has the pattern "July" in
all the files.
[grep "July" *]
04. Write a grep command to print the lines that has the word "July" in
all the files and also print the filename in the output.
[grep -H "July" *]
05. Write a grep command to print the lines that has the word "July"
while ignoring the case insensitive.
[grep -i "July" file.txt]
06. Write a UNIX command to display the lines in a file that do not
contain the word "July".
[grep -v "July" file.txt]
07. Write a grep command to print the line numbers along with the line
that has the word "July".
[grep -n "July" file.txt]
08. Write a grep command to print the lines that starts with the word
"start".
[grep "^start" file.txt]
09. Write a grep command to print the lines which end with the word
"end".
[grep "end$" file.txt]
10. Write a grep command to select only those lines containing "July" as
a whole word.
[grep "\<July\>" file.txt]
AWK Commands:
01. Write an awk command to print the lines and line numbers in the
given input file.
02. Write an awk program/command to print first field and second field,
only if third field value is >= 50 in the given input file. (Input field
separator is “ : ”, output filed separator is “ , ”).
[awk -F " : " -v OFS=" , " '$3 >= 50 {print $1, $2}' file.txt]
03. Consider marks.txt is a file that contains one record per line (comma
separated) of the student data in the form of Student id, Student name,
Telugu, English, Maths, Science, Social marks. Write a awk script to
generate result for every student in the form of Student id, Total Marks
and Result, Result is PASS if marks is > 30 in Telugu & English, and if
marks >= 40 in other subjects. Otherwise the Result is FAIL.
04. Write an awk program to print the fields 1 and 4 of file that is passed
as command line arguments. The file contains lines of information that is
separated by “,” as delimiter. The awk program must print at the end
average of all 4th field data.
BEGIN {
FS=","
sum = 0
count = 0
}
{
print $1, $4
sum += $4
count++
}
END {
print "Average of 4th field data: " sum/count
}
05. Write an awk program to demonstrate user defined functions and
system commands.
06. Write an awk script to count the number of lines in a file that do not
contain vowels.
07. Write an awk script to find the number of characters, words and lines
in a file.
BEGIN {
# Set the field separator to whitespace
FS = " "
# Initialize the counters
num_chars = 0
num_words = 0
num_lines = 0
}
{
# Count the number of characters
num_chars += length($0)
# Count the number of words
num_words += NF
# Count the number of lines
num_lines++
}
END {
# Print the results
printf("Number of characters: %d\n", num_chars)
printf("Number of words: %d\n", num_words)
printf("Number of lines: %d\n", num_lines)
}