Sed command to print, replace, add and delete cars

Consider the following file called testfile1.txt:

testfile1.txt
plym  fury  1970  73  2500
chevy malibu  1999  60  3000
ford  mustang 1965  45  10000
volvo s80 1998  102 9850
ford  thundbd 2003  15  10500
chevy malibu  2000  50  3500
bmw 325i  1985  115 450
honda accord  2001  30  6000
ford  taurus  2004  10  17000
toyota  rav4  2002  180 750
chevy impala  1985  85  1550
ford  explor  2003  25  9500

Use the testfile1.txt to carry out the following tasks and write the results

1. Using a sed command, list all chevy cars.

$sed -n '/chevy/p' testfile1.txt

2. Using a sed command, print lines 3 to 6.

$sed -n '3,6p' testfile1.txt

3. Using a sed command, print all cars between the first chevy and the first bmw cars.

$sed -n '2,7p' testfile1.txt

4. Using a sed command, replace and print all occurrences of chevy to Chevrolet.

$sed -n 's/chevy/Chevrolet/; /Chevrolet/p' testfile1.txt

5. Using a sed command, add a line of 20 dashes after each line.

$sed 'a\--------------------' testfile1.txt

6. Using a sed command, delete all ford cars.

$sed '/ford/d' testfile1.txt

7. Using a sed command, add.00 to all the numbers in the last column.

$sed 's/\([0-9][0-9]*\)$/\1\.00/' testfile1.txt

8. Using an awk command, print first and last columns only with last column printed first.

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

9. Using an awk command, count the number of ford cars and print that number.

$awk '{if($1=="ford") ++count} END{print count}' testfile1.txt

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.

$awk 'BEGIN{printf("Make\tModel\tYear\tK Miles\tPrice\n");print "-------------------------------------";}{printf("%-7s\t%-7s\t%d\t%d\t\t$%d.00\n", $1,$2,$3,$4,$5)}' testfile1.txt

Recommended Posts