Write down the output of the last command in each question and describe the func
ID: 3794932 • Letter: W
Question
Write down the output of the last command in each question and describe the function of it.
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.
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...
Hint: you may need to create "h1.awk" and "h2.awk" in your own computer by yourself.
1) Assume "h1.awk" is located at the current working directory.
$ cat h1.awk
NR>=2&&NR<=5{ print NR " " $0 }
$ awk -f h1.awk float
Output:
Function:
2) $ awk '{print NF, $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 NR NF}
END { print "END"}
$ awk -f h2.awk float
Output:
Function:
4)
$ sed '/./d' float
Output:
Function:
5)
$ ls *.awk | awk '{ print "mv " $1 " " $1 ".new" }' |sh
(Hint: sh file runs file as a shell script. Use ls to discover the changes in your working directory)
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
Let me start with the contents of the "float" file. As i understand the contents of the file is
"
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...
"
That's a single line file. I would suggest to split this single line to multiple lines to better understand the output of the below commands.
Anyway i will explain to you output of each commands for your "float" file.
Question 1) --
$ cat h1.awk
NR>=2&&NR<=5{ print NR " " $0 }
$ awk -f h1.awk float
FUNCTION :
----------
Now , in "awk" scripts we have some predefined variables with predefined meanings. "awk" processes each line of a file one by one.
Now "NR" variable means the number of records currently being processed i.e NR for first line would be "1", similarly "2" for second line and
so on.
Now content of "h1.awk" says " NR>=2&&NR<=5{ print NR " " $0 }"
so the print statement would work only for record no. 2 , 3, 4, 5. Also "$0" means the current line being processed.
Now in your case the file "float" has only one line so the command doesn't give any output because the output will be printed
only for line 2 to 5. If you split your contents into multiple lines then you can see the difference.For now you will get no output.
OUTPUT :
--------
Question 2) --
$) awk '{print NF, $NF }' float
FUNCTION:
---------
Now like "NR", "NF" means number of fields in the current line being processed. Also i explained that "$0" means the contents of current line.
Similarly "$1" , "$2" ... represents individual field(words) of the current line separated by default separator " " (space) .
So for each line "$NF" will represent the last word of the current line because "NF" represents the total number of fields in the line.
Considering all the contents to be in the same line, below is the output.
"33" is the total number of fields and "all..." is the last field of the line.
OUTPUT :
--------
33 all...
Question 3) --
$ cat h2.awk
BEGIN { print "Start to scan file", FILENAME}
{print NR NF}
END { print "END"}
$) awk -f h2.awk float
OUTPUT :
--------
Start to scan file
1 33
END
FUNCTION :
----------
Now first and last line of the awk script is obvious as they get printed inside the 'BEGIN' and 'END' section of script which are
executed at the beginning and end of the script respectively.
"FILENAME" is another variable which prints the current file being passed and processed by the program, in this case "float".
Now for each line of "float" file the print statement in the middle gets executed , "NR" represents "number of record" and "NF"
represents "number of fields in the current line". In this case it is "1" and "33".
Question 4) --
$)sed '/./d' float
OUTPUT:
-------
It doesn't give any output.
FUCNCTION :
----------
"d" option with sed deletes all the lines matching the pattern . Now the pattern is specified by the middle expression enclosed
between "/" & "/"
i,e "."
Now the escape character "" means the "." character should not be treated as a special character and should be treated as "."
character only. Hence the expression means removing all the lines which has "." in it . You can add further lines with no "." in it and
can check the output.
Question 5) --
$ ls *.awk | awk '{ print "mv " $1 " " $1 ".new" }' | sh
OUTPUT :
--------
The output of the above command can be seen by executing "ls" command.
$) ls
h1.awk.new h2.awk.new
FUNCTION :
----------
Let me take it piece by piece.
Following would be the Output of "ls *.awk" command if executed separately :
h1.awk
h2.awk
It lists each file whose name matches ends with ".awk" in separate lines.
Now following is the output of ls *.awk | awk '{print "mv " $1 " " $1 ".new" }'
mv h1.awk h1.awk.new
mv h2.awk h2.awk.new
For each line "$1" is the name of the file from the output of "ls *.awk" command. "mv" command renames any file or directory .
"mv $1 " " $1 ".new" "
so $1 is the filename and "$1.new" is the new filename. But print doesn't execute the "mv" command it just forms the string and passes it on to
the "sh" command.
Now once the output is passed on to the "sh" command it executes it . "sh" is the shell that executes each line of command passed to it .
Hence all the files whose name ends with ".awk" is relaced by file ending with ".awk.new".
Question 6) --
$ mkdir test1 test2 test3
$ ls -l | grep '^d' | awk '{print "cp -r " $9 " " $9 ".new"}' | sh
OUTPUT :
--------
see the output of above command by executing the ls command. The command creates a copy of each directory with a new name appended with ".new".
i.e.
test1.new test2.new test3.new
FUNCTION :
----------
Sample output of ls -l :
[xxx@localhost testing_dir]$ ls -l
total 44
-rw-rw-r--. 1 xxx xxx 173 Feb 19 06:55 float
-rw-rw-r--. 1 xxx xxx 32 Feb 19 06:47 h1.awk.new
-rw-rw-r--. 1 xxx xxx 88 Feb 19 06:29 h2.awk.new
drwxrwxr-x. 2 xxx xxx 4096 Feb 19 06:33 test1
drwxrwxr-x. 2 xxx xxx 4096 Feb 19 06:33 test2
drwxrwxr-x. 2 xxx xxx 4096 Feb 19 06:33 test3
If you see the beginning letter for each directory entry it is 'd' denoting 'directory'.
Now " grep '^d' " command greps only those records which starts with 'd'. "^" character means comparison is done
in the beginning of each line.
Now if you see the awk command "$9" means the ninth field of each line passed to it . The 9th field of each line is the directory/filename .
Hence ,
"cp -r " $9 " " $9 ".new"
means,
cp -r test1 test1.new
cp -r test2 test2.new
cp -r test3 test3.new
"-r " option is used with "cp" command to copy the directories.
"sh" command executes each "cp" commands.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.