Part III: Write down the output of the last command in each question and describ
ID: 3755713 • Letter: P
Question
Part III: Write down the output of the last command in each question and describe the function of it.
Note: please do not simply say awk executes the commands from a file.
Question 1) to 4) are processing the same text file "float". "float" has a content shown as in the table below. To try those commands in the questions, you can create your own "float" file in your Ubuntu system.
Hint: you may need to create "h1.awk" and "h2.awk" in your own computer by yourself.
Wish I was floating in blue across the sky, my imagination is strong, And I often visit the days
When everything seemed so clear.
Now I wonder what I'm doing here at all...
1) Assume "h1.awk" is located at the current working directory.$ cat h1.awk
NR<=2{print $1 “,” $NF }$ awk -f h1.awk float
Output:
Function:
2)
$awk '{print NR, $0, $NF }' float
Output:
Function:
3) Assume "h2.awk" is located at the current working directory.$ cat h2.awk
BEGIN { print "Start to scan file" FILENAME}
{print $1}
END { print "Finished"}$ awk -f h2.awk float
Output:
Function:
4)
$ sed -E 's/s[a-zA-Z]{4}s//g' float
Output:
Function:
5)
$ls *.awk|awk '{print "wc -l " $1 }'|sh(Hint: sh file runs file as a shell script.)
Output:
Function:
6)
$ mkdir test1 test2 test3
$ls -l|grep '^d'|awk '{print "cp -r " $9 " " $9 ".new"}'| sh
(Hint: use ls to discover the changes in your working directory)
Output:
Function:
Explanation / Answer
ANSWER:
1)
Output:
Unix Terminal> awk -f h1.awk float
And I often visit the days:6
When everything seemed so clear:5
Now I wonder what I'm doing here at all...:9
Unix Terminal>
Explanation:
1) The h1.awk program fetches lines from 2 to 5 using NR in the given file float.
2) For each line from 2 to 5 it counts number of words in it using NF in awk
3) Note that $0 represents entire line, NR(Record Number) points to line number and NF represents number of fields
2)
Output:
Unix Terminal> awk '{print $1, $NF}' float
Wish strong,
And days
When clear
Now all...
Unix Terminal>
Explanation:
1) The awk command prints first and last column in the file float
2) $1 represents first column and $NF represents last column. For example, the line "And I often visit the days" has "And" as the first column and "days" as the last column which gets printed by the awk command
3)
Output:
Unix Terminal> awk -f h2.awk float
Start to scan file
113
26
35
49
END float
Unix Terminal>
Explanation:
1) h2.awk program prints the line number and number of words or fields in each line.
2) The program uses NR to print the record or line number and NF to print number of words or fields in each line.
3) For example the second line "And I often visit the days" is line number 2 and has 6 words. Thats why in the above output we can see it shows 26. The same logic for other lines.
4)
Output:
Unix Terminal> sed 's/[^,]*,//g' float
And I often visit the days
When everything seemed so clear
Now I wonder what I'm doing here at all...
Unix Terminal>
Explanation:
1) The sed command removes lines containing "," in the sentence.
2) In the file float we can see only first line has "," thats why it gets removed after executing the sed command.
3) [^,] represents not containing "," symbol and s/[^,]*,//g represents lines not containing "," in the sentence.
5)
Output:
Unix Terminal> ls -tlrh
total 24
-rw-r--r-- 1 vishal group Users 104B Feb 15 07:42 h2.awk
-rw-r--r-- 1 vishal group Users 60B Feb 15 07:42 h1.awk
-rw-r--r-- 1 vishal group Users 172B Feb 15 07:42 float
Unix Terminal> ls *.awk|awk '{print "cp " $1 " " $1".new"}' |sh
Unix Terminal> ls -tlrh
total 40
-rw-r--r-- 1 vishal group Users 104B Feb 15 07:42 h2.awk
-rw-r--r-- 1 vishal group Users 60B Feb 15 07:42 h1.awk
-rw-r--r-- 1 vishal group Users 172B Feb 15 07:42 float
-rw-r--r-- 1 vishal group Users 104B Feb 15 07:42 h2.awk.new
-rw-r--r-- 1 vishal group Users 60B Feb 15 07:42 h1.awk.new
Unix Terminal>
Explanation:
1) The command copies all awk files in the current and sub directories to new files with extension ".new". The same can be observed in the above command output.
2) ls *.awk
lists all the files with extension "awk"
3) awk '{print "cp " $1 " " $1".new"}'
forms a cp command for *.awk files. For example, if the file name is h1.awk the command formed would be "cp h1.awk h1.awk.new"
3) |sh
the sh command will take the command formed using awk and executes it. That means the actual cp command gets executed.
4) Note that "-r" of cp command is not available in my system so i have not used that. Otherwise everything is same as said above.
6)
Output:
Unix Terminal> mkdir test1 test2 test3
Unix Terminal>
Unix Terminal> ls -l
total 40
-rw-r--r-- 1 vishal group Users 172 Feb 15 07:42 float
-rw-r--r-- 1 vishal group Users 60 Feb 15 07:42 h1.awk
-rw-r--r-- 1 vishal group Users 60 Feb 15 07:42 h1.awk.new
-rw-r--r-- 1 vishal group Users 104 Feb 15 07:42 h2.awk
-rw-r--r-- 1 vishal group Users 104 Feb 15 07:42 h2.awk.new
drwxr-xr-x 2 vishal group Users 68 Feb 15 07:48 test1
drwxr-xr-x 2 vishal group Users 68 Feb 15 07:48 test2
drwxr-xr-x 2 vishal group Users 68 Feb 15 07:48 test3
Unix Terminal>
Unix Terminal> ls -l | grep '^d' | awk '{print "mv " $10 " " $10 ".new"}' | sh
Unix Terminal> ls -tlrh
total 40
-rw-r--r-- 1 vishal group Users 104B Feb 15 07:42 h2.awk
-rw-r--r-- 1 vishal group Users 60B Feb 15 07:42 h1.awk
-rw-r--r-- 1 vishal group Users 172B Feb 15 07:42 float
-rw-r--r-- 1 vishal group Users 104B Feb 15 07:42 h2.awk.new
-rw-r--r-- 1 vishal group Users 60B Feb 15 07:42 h1.awk.new
drwxr-xr-x 2 vishal group Users 68B Feb 15 07:48 test3.new
drwxr-xr-x 2 vishal group Users 68B Feb 15 07:48 test2.new
drwxr-xr-x 2 vishal group Users 68B Feb 15 07:48 test1.new
Unix Terminal>
Explanation:
1) The command renames all test1, test2, test3 directories to test1.new, test2.new and test3.new directories.
2) ls -l
lists file in current directory
3) grep '^d'
fetches only directory files
4) awk '{print "mv " $10 " " $10 ".new"}'
forms the mv command for all the directories in current directory. Note that the filename is 10th field in my system. You can change to 9 in your computer. Also there is no -r option in mv command in my computer, so i have removed it. You can put -r in your computer.
5) sh
takes the mv command formed by awk command and executes it. That means the mv command gets executed.