In: Computer Science
Ubuntu Linux
HW5: text processing; scripting
1. Write a Linux command to rewrite the /var/passwd file to have a tab for each delimiter ':'. Hint: use tr
2. Write a Linux command to extract the user names and sort them. Hint: use cut
3. Write a for loop to to display a time table, e.g., 17 x 1 = 17; 17 x 2 = 34; etc., as follows:
17 x 1 = 17 17 x 2 = 34
17 x 3 = 51 17 x 4 = 68
17 x 5 = 85 17 x 6 = 102
17 x 7 = 119 17 x 8 = 136
17 x 9 = 153 17 x 10 = 170
17 x 11 = 187 17 x 12 = 204
17 x 13 = 221 17 x 14 = 238
17 x 15 = 255 17 x 16 = 272
17 x 17 = 289 4.
Write a Linux command to download the following webpage to the fail called "viral-diseases" Hint: use wget
5. Using a regular expression, write a Linux command to extract URL addresses from the viral-diseases. The URL starts with "www" and ending with "com" or "gov". Hint: use grep -E 6. Write a bash shell to
1) using a for loop to create the directory called "jyoon_0" to "jyoon_9";
2) change directory to each of the directories created,
3) write the sentence "Yoon visited in directory jyoon_0t" in the file jyoon_0.txt",
4) attach the timestamp to the file;
5) writhe the same sentence to the standard output device,
6) the timestamp too;
7) do this to all those 10 directories, and
8) at the end, write "All done" to the standard output device. So, the example standard output:
jyoon@MT-UBUN01:~$ ./hw5create.sh
Yoon visited jyoon_0 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_1 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_2 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_3 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_4 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_5 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_6 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_7 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_8 directory
Thu 09 Jul 2020 11:48:42 AM EDT
Yoon visited jyoon_9 directory
Thu 09 Jul 2020 11:48:42 AM EDT
All Created!
One of the directories has the following:
jyoon@MT-UBUN01:~$ cd jyoon_5
jyoon@MT-UBUN01:~/jyoon_5$ ls
jyoon_5.txt jyoon@MT-UBUN01:~/jyoon_5$ cat jyoon_5.txt
Yoon visited jyoon_5 directory
Thu 09 Jul 2020 11:48:42 AM EDT
jyoon@MT-UBUN01:~/jyoon_5$
Note that the directory name, file name and your name should be formed with your login ID, but not jyoon, which is not yours!
Solution:
1.
the tr command in linux is used to replace the characters present in the standard input with the given characters.
Since tr can only operate on standard input we need to pass our data to the standard input then pipe it to the tr command.
So, we use cat command to view the contents of the file and then pipe the data to the tr command then redirect the output to a temporary file ( /var/passwdt ) and then rename the temporary file with original file name.( /var/passwd in this case )
Command looks like:
cat /var/passwd | tr -s ':' '\t' > /var/passwdt
mv /var/passwdt /var/passwd
Output:
/var/passwd file before rewriting
/var/passwd file after rewriting
2.
the cut command separates or cuts the given file data based on the given condition. In the above question we replaced all the : 's with "\t" characters now using cut we can extract the field values. field 1 contains the usernames. To sort the usernames we use sort command.
Command to sort the usernames:
cut -f 1 /var/passwd | sort
Output:
3.
The for loop looks something like shown below. when you are using * in expr you need to use it as \* because bash treats * as special character..
LOOP:
for((i=1;i<=17;i++))
{
echo "17 x $i = "$(expr 17 \* $i)
}
OUTPUT:
I hope this would help...................:-))