Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX - Use the testfile1.txt to carry out the following tasks and write the resu

ID: 3705444 • Letter: U

Question

UNIX - Use the testfile1.txt to carry out the following tasks and write the results in a single file named as your <LastName<FirstName>.txt and upload:

7.    Using a sed command, add .00 to all the numbers in the last column.
8.    Using an awk command, print first and last columns only with last column printed first.
9.    Using an awk command, count the number of ford cars and print that number.
10. Using an awk command, add a header line with Make, Model, Year, K Miles, and Price aligned with the columns followed by a dashed line and then all of the data in the file with the following changes: the last column includes a $ sign followed by the number expressed as a floating point number with two digits following the decimal point i.e., 2500 appears as $2500.00, and so on.

testfile1.txt

Explanation / Answer

7. add .00 to all the numbers in the last column

                    $ sed 's/<[0-9]>/.00&/' ./testfile1.txt> lstnamefrstnme.txt

   in this output file lstnamefrstname.txt you can change with your name.This will replace all numbers by adding .00 like this,2500 will changed to 2500.00

                          plym fury 1970 73 2500.00

                         chevy malibu 1999 60 3000.00

                          ford mustang 1965 45 10000.00

                          volvo s80 1998 102 9850.00

                          ford thundbd 2003 15 10500.00

                          chevy malibu 2000 50 3500.00

                          bmw 325i 1985 115 450.00

                          honda accord 2001 30 6000.00

                          ford taurus 2004 10 17000.00

                          toyota rav4 2002 180 750.00

                          chevy impala 1985 85 1550.00

                          ford explor 2003 25 9500.00

8.print first and last columns only with last column printed first

                      awk -F '|' '{print $1, $5}' testfile1.txt

This will print first and last columns.For printing last column first do the following,

                  awk -F '|' '{print $5, $1}' testfile1.txt

it will print like this,

                           2500.00           plym

                           3000.00           chevy

                          10000.00          ford

                          9850.00            volvo

                          10500.00          ford

                        3500.00           chevy

                          450.00             bmw

                          6000.00           honda

                          17000.00          ford

                          750.00             toyota

                          1550.00           chevy

                           9500.00          ford

9.count the number of ford cars and print that number

       awk 'match($1,/ford/,b){a[b[0]]++}END{for(i in a)print i ,a[i]}' testfile1.txt

    or use

       awk '{count[$1]++} END {for (ford in count) print ford, count[ford]}' testfile1.txt

it will output

   ford 4

10.

last column includes a $ sign ,for this the following code is used,

                    sed -e 's/^/$/' testfile1.txt > lstnamefrstnme.txt

this will output

                         plym fury   1970     73     $2500.00

                         chevy malibu 1999 60   $3000.00

                          ford mustang 1965 45 $10000.00

                          volvo s80 1998 102 $9850.00

                          ford thundbd 2003 15 $10500.00

                          chevy malibu 2000 50 $3500.00

                          bmw 325i 1985 115 $450.00

                          honda accord 2001 30 $6000.00

                          ford taurus 2004 10 $17000.00

                          toyota rav4 2002 180 $750.00

                          chevy impala 1985 85 $1550.00

                          ford explor 2003 25 $9500.00

add a header line with Make, Model, Year, K Miles, and Price aligned with the columns followed by a dashed line

   awk 'BEGIN {print "Make Model Year K Miles Price"} {print}' testfile1txt > lstnamefrstnme.txt

this code will print headers for the table.for dashed lines under header use this code

                  awk '{print} NR==1 {gsub(/[^ ]/, "-"); print}'

                        Make Model Year K Miles Price

                        ------   -------- ------- ---------- --------

                         plym fury   1970     73     $2500.00

                         chevy malibu 1999 60 $3000.00

                          ford mustang 1965 45 $10000.00

                          volvo s80 1998 102 $9850.00

                          ford thundbd 2003 15 $10500.00

                          chevy malibu 2000 50 $3500.00

                          bmw 325i 1985 115 $450.00

                          honda accord 2001 30 $6000.00

                          ford taurus 2004 10 $17000.00

                          toyota rav4 2002 180 $750.00

                          chevy impala 1985 85 $1550.00

                          ford explor 2003 25 $9500.00