Unix Imp
Unix Imp
grep
Here are the solutions to your listed tasks using sed
, awkutilities:
, and
● Explanation: This substitutes all occurrences of "doshi"with "desai" in the file.
● E -noption shows the line number alongwith the lines containing
xplanation: The
"Marketing".
● E
xplanation: The^symbol matches the beginning ofthe line, so this will display lines
starting with "The".
X1
6. Display two lines starting from the 7th line of file .
bash
X1
7. Display all blank lines between lines 20 to 30 of file .
bash
X1
8. Display lines beginning with alphabets or digits from file .
bash
grep '^[a-zA-Z0-9]' X1
● Explanation: This matches lines starting with eitheran alphabet or a digit.
● Explanation: This excludes lines that start with thedigit 2.
● Explanation: This substitutes "Unix OS" with "LinuxOS" 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 filenamesthat consist entirely of digits.
● Explanation: The
ls | grep '^[0-9]\{4\}$'
XI
14. To display lines beginning with 'A' from file .
bash
wc -w
● Explanation: This prints lines 40-60 and counts thenumber of words using .
awk
17. Using , simulate the following commands:
file1
● Explanation: This prints the last 40 lines of .
xl
● Explanation: This redirects both standard output anderror output to .
19. Write a command that displays users logged in more than once.
bash
printf
● Explanation: This converts the decimal number 192to hexadecimal using .
21. Match all file names not beginning with a dot (.)
bash
ls | grep -v '^\.'
fl
22. Delete all lines beginning with 'T' in file .
bash
fl
● Explanation: This deletes lines beginning with theletter "T" in .
ls -lS | head -n 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" andcounts the number of lines and words.
fl
25. Replace multiple spaces with a space in file .
bash
sed 's/
*/ /g' fl
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 haveat least 4 characters.
ls -lR --color=auto
● Explanation: This lists all files, including hiddenfiles, 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 '^[!@#$%^&*()_+={}[\]|:;,.<>?/]'
ls -ld dir1
dir1
● Explanation: This displays the attributes of .
● Explanation: This selects lines 30 to 40 and onlyprints lines containing "unix".
● E empby
xplanation: Assuming the date of birth is in the3rd 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 charactersin 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 twodashes to the end of each line.
awk 'NR % 2 == 1 {print $0; print $0} NR % 2 == 0 {print $0; print $0;
print $0}' file
5. Copy the lines of a file that have only lowercase letters in it to another
3
file.
bash
newfile
● Explanation: This copies lines containing only lowercaseletters 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
● E
xplanation: This displays lines with uppercase charactersand 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
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 displaysthe first three.
file1to
● Explanation: Copies lines 12 to 20 from file2
.
fl
40. Display even lines of file .
bash
fl
● Explanation: This displays even-numbered lines fromthe file .
1. Display those words that start and end with the same character from file
4
fl
.
bash
● Explanation: This finds words that start and end withthe same character.
2. Display all regular files of working directory whose permission for all
4
users are read and write.
bash
● Explanation: This finds all regular files with readand 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 consistonly 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 modificationtime, displaying the most recently
modified file.