0% found this document useful (0 votes)
6 views14 pages

Unix Imp

The document provides a comprehensive list of UNIX command tasks using grep, sed, and awk, along with their corresponding solutions. It includes commands for filtering, displaying, and manipulating text files, as well as file management tasks. Each command is accompanied by an explanation of its function and usage.

Uploaded by

sachinmandalbgmi
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)
6 views14 pages

Unix Imp

The document provides a comprehensive list of UNIX command tasks using grep, sed, and awk, along with their corresponding solutions. It includes commands for filtering, displaying, and manipulating text files, as well as file management tasks. Each command is accompanied by an explanation of its function and usage.

Uploaded by

sachinmandalbgmi
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/ 14

‭UNIX IMP 😂 CMD Marks Question Answer‬

‭By‬‭Zotarc Marketing Agency‬‭!‬

‭Using grep and sed and awk ( From Theory papers)‬

‭1.‬ ‭W.a.c. to print which do not contain ‘Accounts’.‬


‭2.‬ ‭Print line 10 to 15.‬
‭3.‬ ‭Substitute ‘doshi’ with ‘desai’.‬
‭4.‬ ‭W.a.c. to print lines with line number which‬‭contain ‘Marketing’.‬
‭5.‬ ‭Display line which start with ‘The’.‬
‭6.‬ ‭Display two lines starting from 7‬‭th‬ ‭line of‬‭file X1.‬
‭7.‬ ‭Display all blank line between 20 to 30 line‬‭of file x1.‬
‭8.‬ ‭Display lines beginning with alphabets or digit‬‭from file x1.‬
‭9.‬ ‭Display line that do not contain “Clerk”.‬
‭ 0.‬ ‭Display the line which is not starting with 2‬‭.‬
1
‭11.‬ ‭W.a.c. to replace ‘Unix OS’ on line no 5‬‭th‬ ‭to‬‭10‬‭th‬‭.‬
‭12.‬ ‭W.a.c. to display all file name containing only‬‭digits in a filename.‬
‭13.‬ ‭To list file names consist of only 4 digits.‬
‭14.‬ ‭To display lines beginning with A of a file XI.‬
‭15.‬ ‭Lines 40 through 60 of file f1.txt to count no.‬‭of words.‬
‭16.‬ ‭Write a grep command to display all lines that‬‭begins and ends with same‬
‭Character. ■ –‬‭(grep -x "\(.\). \1" file1)‬
‭17.‬ ‭Using awk Simulate the following commands:‬
‭a.‬ ‭(1) tail -40 file!‬
‭b.‬ ‭(2) head -20 file 1 j tail -6‬
‭18.‬ ‭To assign output as well as error message of a‬‭command Cat x to file xl .‬

‭19.‬ ‭Write a command that user logged in more than‬‭once.‬


‭20.‬ ‭Convert the decimal number 192 to hexadecimal.‬‭21. Match all file names not‬
‭beginning with a dot (.)‬
‭22.‬ ‭Delete all lines begin with T in file fl.‬
‭23.‬ ‭Display contents of top 3 largest files in a working‬‭directory.‬
‭24.‬ ‭Display total number of words and lines of files‬‭which starts with 'm'.‬
‭25.‬ ‭Replace multiple spaces with a space in file fl.‬
‭26.‬ ‭List all files of working directory having at‬‭least 4-characters in file name.‬
‭27.‬ ‭Display all files recursively (including the hidden‬‭ones) in multiple columns with‬
‭distinguishing makes on executables and directory.‬
‭28.‬ ‭Display all files of current directory whose 1st‬‭character is not special character.‬
‭29.‬ ‭Display attributes of directory dir 1 resides in current directory.‬
‭30.‬ ‭Write a sed command to select those lines between‬‭30 and 40 having pattern 'unix'‬
‭in a file fl.‬
‭31.‬ ‭Write a short sequence on order emp. List on the‬‭date of birth.‬
‭32.‬ ‭Count the number of characters from the first‬‭five lines in the file f1.‬
‭33.‬ ‭Write a command to add two spaces to the begging‬‭of each line and two dashes at‬
‭the end of each line.‬
‭34.‬ ‭Write an awk script to print each odd line twice‬‭and even line thrice.‬
‭35.‬ ‭Copy the lines of a file that have only lowercase‬‭letters in it to another file.‬
‭36.‬ ‭Write a command to display the lines on screen‬‭as well as save it in a file that have‬
‭only uppercase characters in it.‬
‭37.‬ ‭Write a command to display the lines that has‬‭atleast two digits without any other‬
‭character in it.‬
‭38.‬ ‭Write a command to display the name of first three‬‭files that are last modified.‬
‭39.‬ ‭Write a command that copies only 12th to 20th‬‭line of one file to another file.‬
‭40.‬ ‭Display even lines of file fl.‬
‭41.‬ ‭Display those words that start and end with same‬‭character of file fl.‬
‭42.‬ ‭Display all regular files of working directory‬‭whose permission for all users are read‬
‭and write.‬
‭43.‬ ‭Displays all file of working directory whose file‬‭name consist of only alphabets.‬
‭44.‬ ‭Display name of most recently modified file of present working directory.‬
grep‬
‭Commands Using‬‭ sed‬
‭,‬‭ awk‬
‭, and‬‭

grep‬
‭Here are the solutions to your listed tasks using‬‭ sed‬
‭,‬‭ awk‬‭utilities:‬
‭, and‬‭

‭1. Write a command to print lines that do not contain 'Accounts'.‬


‭bash‬

grep -v 'Accounts' file‬


‭●‬ E -v‬‭option inverts the match, displaying‬‭lines that do not contain‬


‭ xplanation‬‭: The‬‭
‭"Accounts".‬

‭2. Print lines 10 to 15.‬


‭bash‬

sed -n '10,15p' file‬


‭●‬ E sed -n‬‭suppresses the default output,‬‭and‬‭


‭ xplanation‬‭:‬‭ 10,15p‬‭tells‬‭
sed‬‭to print lines‬
‭from 10 to 15.‬

‭3. Substitute 'doshi' with 'desai'.‬


‭bash‬

sed 's/doshi/desai/g' file‬


‭●‬ ‭Explanation‬‭: This substitutes all occurrences of "doshi"‬‭with "desai" in the file.‬

‭ . Write a command to print lines with line number which contain‬


4
‭'Marketing'.‬
‭bash‬
grep -n 'Marketing' file‬

‭●‬ E -n‬‭option shows the line number along‬‭with the lines containing‬
‭ xplanation‬‭: The‬‭
‭"Marketing".‬

‭5. Display lines which start with 'The'.‬


‭bash‬

grep '^The' file‬


‭●‬ E
‭ xplanation‬‭: The‬‭^‬‭symbol matches the beginning of‬‭the line, so this will display lines‬
‭starting with "The".‬

X1‬
‭6. Display two lines starting from the 7th line of file‬‭ ‭.‬
‭bash‬

sed -n '7,8p' X1‬


sed‬‭to display lines‬‭7 and 8.‬


‭●‬ ‭Explanation‬‭: This command uses‬‭

X1‬
‭7. Display all blank lines between lines 20 to 30 of file‬‭ ‭.‬
‭bash‬

sed -n '20,30{/^$/p}' X1‬


‭ xplanation‬‭: This command finds and prints blank lines‬‭(‭


‭●‬ E /
‬ ^$/‬
‭) between lines 20 and‬
‭30.‬

X1‬
‭8. Display lines beginning with alphabets or digits from file‬‭ ‭.‬
‭bash‬
grep '^[a-zA-Z0-9]' X1‬

‭●‬ ‭Explanation‬‭: This matches lines starting with either‬‭an alphabet or a digit.‬

‭9. Display lines that do not contain 'Clerk'.‬


‭bash‬

grep -v 'Clerk' file‬


-v‬‭option excludes lines containing‬‭the word "Clerk".‬


‭●‬ ‭Explanation‬‭: The‬‭

‭10. Display lines which do not start with 2.‬


‭bash‬

grep -v '^2' file‬


‭●‬ ‭Explanation‬‭: This excludes lines that start with the‬‭digit 2.‬

‭11. Replace 'Unix OS' on lines 5th to 10th.‬


‭bash‬

sed '5,10s/Unix OS/Linux OS/g' file‬


‭●‬ ‭Explanation‬‭: This substitutes "Unix OS" with "Linux‬‭OS" on lines 5 to 10.‬

‭ 2. Write a command to display all file names containing only digits in the‬
1
‭filename.‬
‭bash‬

ls | grep '^[0-9]*$'‬

^[0-9]*$‬‭pattern matches filenames‬‭that consist entirely of digits.‬
‭●‬ ‭Explanation‬‭: The‬‭

‭13. To list file names consisting of only 4 digits.‬


‭bash‬

ls | grep '^[0-9]\{4\}$'‬

‭●‬ ‭Explanation‬‭: This matches filenames consisting of‬‭exactly four digits.‬

XI‬
‭14. To display lines beginning with 'A' from file‬‭ ‭.‬
‭bash‬

grep '^A' XI‬


‭●‬ ‭Explanation‬‭: This matches lines that begin with "A".‬

f1.txt‬‭to count the‬‭number of words.‬


‭15. Lines 40 through 60 of file‬‭
‭bash‬

sed -n '40,60p' f1.txt | wc -w‬


wc -w‬
‭●‬ ‭Explanation‬‭: This prints lines 40-60 and counts the‬‭number of words using‬‭ ‭.‬

grep‬‭command to display all lines that‬‭begin and end with the‬


‭ 6. Write a‬‭
1
‭same character.‬
‭bash‬

grep -x "\(.\). \1" file1‬



‭●‬ ‭Explanation‬‭: This matches lines that start and end with the same character.‬

awk‬
‭17. Using‬‭ ‭, simulate the following commands:‬

tail -40 file1‬


‭a.‬‭
‭bash‬

awk 'NR > (NR - 40)' file1‬


file1‬
‭●‬ ‭Explanation‬‭: This prints the last 40 lines of‬‭ ‭.‬

head -20 file1 | tail -6‬


‭b.‬‭
‭bash‬

awk 'NR>=15 && NR<=20' file1‬


head -20‬‭followed by‬‭


‭●‬ ‭Explanation‬‭: This prints lines 15 to 20, simulating‬‭ tail -6‬
‭.‬

cat x‬‭to file‬


‭18. To assign output as well as error message of a command‬‭
xl‬
‭ ‭.‬
‭bash‬

cat x > xl 2>&1‬


xl‬
‭●‬ ‭Explanation‬‭: This redirects both standard output and‬‭error output to‬‭ ‭.‬

‭19. Write a command that displays users logged in more than once.‬
‭bash‬

who | awk '{print $1}' | sort | uniq -d‬


‭●‬ ‭Explanation‬‭: This lists users who are logged in multiple‬‭times.‬


‭20. Convert the decimal number 192 to hexadecimal.‬
‭bash‬

printf '%x\n' 192‬


printf‬
‭●‬ ‭Explanation‬‭: This converts the decimal number 192‬‭to hexadecimal using‬‭ ‭.‬

‭21. Match all file names not beginning with a dot (.)‬
‭bash‬

ls | grep -v '^\.'‬

‭●‬ ‭Explanation‬‭: This excludes files that start with a‬‭dot.‬

fl‬
‭22. Delete all lines beginning with 'T' in file‬‭ ‭.‬
‭bash‬

sed '/^T/d' fl‬


fl‬
‭●‬ ‭Explanation‬‭: This deletes lines beginning with the‬‭letter "T" in‬‭ ‭.‬

‭23. Display contents of top 3 largest files in the working directory.‬


‭bash‬

ls -lS | head -n 3‬

‭●‬ ‭Explanation‬‭: This lists files sorted by size (‬‭


-lS‬
‭)‬‭and displays the top 3.‬

‭24. Display total number of words and lines of files that start with 'm'.‬
‭bash‬

ls m* | xargs wc -l -w‬

‭●‬ ‭Explanation‬‭: This lists files starting with "m" and‬‭counts the number of lines and words.‬

fl‬
‭25. Replace multiple spaces with a space in file‬‭ ‭.‬
‭bash‬

sed 's/
‭ */ /g' fl‬

‭●‬ ‭Explanation‬‭: This replaces multiple spaces with a‬‭single space.‬

‭ 6. List all files of the working directory having at least 4 characters in the‬
2
‭filename.‬
‭bash‬

ls | grep -E '^.{4,}$'‬

‭●‬ ‭Explanation‬‭: This lists files whose filenames have‬‭at least 4 characters.‬

‭ 7. Display all files recursively (including hidden ones) in multiple columns‬


2
‭with distinguishing marks on executables and directories.‬
‭bash‬

ls -lR --color=auto‬

‭●‬ ‭Explanation‬‭: This lists all files, including hidden‬‭files, recursively with color distinction.‬

‭ 8. Display all files of the current directory whose 1st character is not a‬
2
‭special character.‬
‭bash‬

ls | grep -v '^[!@#$%^&*()_+={}[\]|:;,.<>?/]'‬

‭●‬ ‭Explanation‬‭: This excludes files starting with special‬‭characters.‬

dir1‬‭in the current‬‭directory.‬


‭29. Display attributes of directory‬‭
‭bash‬

ls -ld dir1‬

dir1‬
‭●‬ ‭Explanation‬‭: This displays the attributes of‬‭ ‭.‬

sed‬‭command to select lines between 30‬‭and 40 having pattern‬


‭30. Write a‬‭
fl‬
‭'unix' in file‬‭ ‭.‬
‭bash‬

sed -n '30,40{/unix/p}' fl‬


‭●‬ ‭Explanation‬‭: This selects lines 30 to 40 and only‬‭prints lines containing "unix".‬

emp‬‭list by date‬‭of birth.‬


‭31. Write a short sequence on order‬‭
‭bash‬

sort -k3 emp‬


‭●‬ E emp‬‭by‬
‭ xplanation‬‭: Assuming the date of birth is in the‬‭3rd column, this sorts the file‬‭
‭the date.‬

f1‬
‭32. Count the number of characters from the first five lines in file‬‭ ‭.‬
‭bash‬

head -n 5 f1 | wc -c‬

f1‬
‭●‬ ‭Explanation‬‭: This counts the number of characters‬‭in the first 5 lines of‬‭ ‭.‬

‭ 3. Write a command to add two spaces to the beginning of each line and‬
3
‭two dashes at the end of each line.‬
‭bash‬

sed 's/^/
‭ /; s/$/ --/' file‬

‭●‬ ‭Explanation‬‭: Adds two spaces to the start and two‬‭dashes to the end of each line.‬

awk‬‭script to print each odd line twice‬‭and even line thrice.‬


‭34. Write an‬‭
‭bash‬

awk 'NR % 2 == 1 {print $0; print $0} NR % 2 == 0 {print $0; print $0;‬

print $0}' file‬

‭●‬ ‭Explanation‬‭: Prints odd lines twice and even lines‬‭thrice.‬

‭ 5. Copy the lines of a file that have only lowercase letters in it to another‬
3
‭file.‬
‭bash‬

grep '^[a-z]*$' file > newfile‬


newfile‬
‭●‬ ‭Explanation‬‭: This copies lines containing only lowercase‬‭letters to‬‭ ‭.‬
‭ 6. Write a command to display the lines on the screen as well as save it in‬
3
‭a file that have only uppercase characters in it.‬
‭bash‬

grep '^[A-Z]*$' file | tee newfile‬


‭●‬ E
‭ xplanation‬‭: This displays lines with uppercase characters‬‭and saves them to‬
newfile‬
‭ ‭.‬

‭ 7. Write a command to display the lines that have at least two digits‬
3
‭without any other characters in it.‬
‭bash‬

grep '^[0-9][0-9]*$' file‬


‭●‬ ‭Explanation‬‭: This displays lines containing only digits.‬

‭ 8. Write a command to display the name of the first three files that are last‬
3
‭modified.‬
‭bash‬

ls -lt | head -n 3‬

‭●‬ ‭Explanation‬‭: Lists the last modified files and displays‬‭the first three.‬

‭ 9. Write a command that copies only lines 12 to 20 of one file to another‬


3
‭file.‬
‭bash‬

sed -n '12,20p' file1 > file2‬


file1‬‭to‬‭
‭●‬ ‭Explanation‬‭: Copies lines 12 to 20 from‬‭ file2‬
‭.‬
fl‬
‭40. Display even lines of file‬‭ ‭.‬
‭bash‬

awk 'NR % 2 == 0' fl‬


fl‬
‭●‬ ‭Explanation‬‭: This displays even-numbered lines from‬‭the file‬‭ ‭.‬

‭ 1. Display those words that start and end with the same character from file‬
4
fl‬
‭ ‭.‬
‭bash‬

grep -o '\b\([a-zA-Z]\)[a-zA-Z]*\1\b' fl‬


‭●‬ ‭Explanation‬‭: This finds words that start and end with‬‭the same character.‬

‭ 2. Display all regular files of working directory whose permission for all‬
4
‭users are read and write.‬
‭bash‬

find . -type f -perm 666‬


‭●‬ ‭Explanation‬‭: This finds all regular files with read‬‭and write permissions for all users.‬

‭ 3. Display all files of working directory whose file name consists of only‬
4
‭alphabets.‬
‭bash‬

ls | grep '^[a-zA-Z]*$'‬

‭●‬ ‭Explanation‬‭: This lists files whose names consist‬‭only of alphabet characters.‬
‭ 4. Display the name of most recently modified file of the present working‬
4
‭directory.‬
‭bash‬

ls -lt | head -n 1‬

‭●‬ E
‭ xplanation‬‭: This lists files sorted by modification‬‭time, displaying the most recently‬
‭modified file.‬

You might also like