When it comes to command line text processing, from an abstract point of view, there are three major pillars



Download 125.91 Kb.
Page43/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   39   40   41   42   43   44   45   46   ...   60
Learn GNU AWK

Exercises

Exercises will also include functions and features not discussed in this chapter. Refer to gawk manual: Functions for details.

a) For the input file scores.csv, sort the rows based on Physics values in descending order. Header should be retained as the first line in output.
$ awk ##### add your solution here Name,Maths,Physics,Chemistry Ith,100,100,100 Cy,97,98,95 Lin,78,83,80 Er,56,79,92 Ort,68,72,66 Blue,67,46,99
b) For the input file nums3.txt, calculate the square root of numbers and display in two different formats. First with four digits after fractional point and next in scientific notation, again with four digits after fractional point. Assume input has only single column positive numbers.
$ awk ##### add your solution here 1.7720 64.8151 27.8747 568.3414 $ awk ##### add your solution here 1.7720e+00 6.4815e+01 2.7875e+01 5.6834e+02
c) Transform the given input strings to the corresponding output shown. Assume space as the field separators. From the second field, remove the second : and the number that follows. Modify the last field by multiplying it by the number that was deleted from the second field. The numbers can be positive/negative integers or floating-point numbers (including scientific notation).
$ echo 'go x:12:-425 og 6.2' | awk ##### add your solution here go x:12 og -2635 $ echo 'rx zwt:3.64:12.89e2 ljg 5' | awk ##### add your solution here rx zwt:3.64 ljg 6445
d) Transform the given input strings to the corresponding output shown. Assume space as the field separators. Replace the second field with sum of the two numbers embedded in it. The numbers can be positive/negative integers or floating-point numbers (but not scientific notation).
$ echo 'f2:z3 kt//-42\\3.14//tw 5y6' | awk ##### add your solution here f2:z3 -38.86 5y6 $ echo 't5:x7 qr;wq<=>+10{-8764.124}yb u9' | awk ##### add your solution here t5:x7 -8754.12 u9
e) For the given input strings, extract portion of the line starting from the matching location specified by shell variable s till the end of the line. If there is no match, do not print that line. The contents of s should be matched literally.
$ s='(a^b)' $ echo '3*f + (a^b) - 45' | ##### add your solution here (a^b) - 45 $ s='\&/' $ # should be no output for this input $ echo 'f\&z\&2.14' | ##### add your solution here $ # but this one has a match $ echo 'f\&z\&/2.14' | ##### add your solution here \&/2.14
f) Extract all positive integers preceded by - and followed by : or ; and display all such matches separated by a newline character.
$ s='42 foo-5; baz3; x-83, y-20:-34; f12' $ echo "$s" | awk ##### add your solution here 5 20 34
g) For the input file scores.csv, calculate the average of three marks for each Name. Those with average greater than or equal to 80 should be saved in pass.csv and the rest in fail.csv. The format is Name and average score (up to two decimal points) separated by a tab character.
$ awk ##### add your solution here $ cat fail.csv Blue 70.67 Er 75.67 Ort 68.67 $ cat pass.csv Lin 80.33 Cy 96.67 Ith 100.00
h) For the input file files.txt, replace lines starting with a space with the output of that line executed as a shell command.
$ cat files.txt sed -n '2p' addr.txt ----------- wc -w sample.txt =========== awk '{print $1}' table.txt ----------- $ awk ##### add your solution here How are you ----------- 31 sample.txt =========== brown blue yellow -----------
i) For the input file fw.txt, format the last column of numbers in scientific notation with two digits after the decimal point.
$ awk ##### add your solution here 1.3 rs 90 1.35e-01 3.8 6.00e+00 5.2 ye 8.24e+00 4.2 kt 32 4.51e+01
j) For the input file addr.txt, display all lines containing e or u but not both.
Hint — gawk manual: Bit-Manipulation Functions.

$ awk ##### add your solution here Hello World This game is good Today is sunny

Download 125.91 Kb.

Share with your friends:
1   ...   39   40   41   42   43   44   45   46   ...   60




The database is protected by copyright ©ininet.org 2024
send message

    Main page