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



Download 125.91 Kb.
Page49/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   45   46   47   48   49   50   51   52   ...   60
Learn GNU AWK

Summary


This chapter covered various examples of working with multiple records. State machines play an important role in deriving solutions for such cases. Knowing various corner cases is also crucial, otherwise a solution that works for one input may fail for others.
Next chapter will discuss use cases where you need to process a file input based on contents of another file.

Exercises


a) For the input file sample.txt, print a matching line containing do only if the previous line is empty and the line before that contains you.
$ awk ##### add your solution here Just do-it Much ado about nothing
b) Print only the second matching line respectively for the search terms do and not for the input file sample.txt. Match these terms case insensitively.
$ awk ##### add your solution here No doubt you like it too Much ado about nothing
c) For the input file sample.txt, print the matching lines containing are or bit as well as n lines around the matching lines. The value for n is passed to the awk command via the -v option.
$ awk -v n=1 ##### add your solution here Good day How are you Today is sunny Not a bit funny No doubt you like it too $ # note that first and last line are empty for this case $ awk -v n=2 ##### add your solution here Good day How are you Just do-it Today is sunny Not a bit funny No doubt you like it too
d) For the input file broken.txt, print all lines between the markers top and bottom. The first awk command shown below doesn't work because it is matching till end of file if second marker isn't found. Assume that the input file cannot have two top markers without a bottom marker appearing in between and vice-versa.
$ cat broken.txt top 3.14 bottom --- top 1234567890 bottom top Hi there Have a nice day Good bye $ # wrong output $ awk '/bottom/{f=0} f; /top/{f=1}' broken.txt 3.14 1234567890 Hi there Have a nice day Good bye $ # expected output $ ##### add your solution here 3.14 1234567890
e) For the input file concat.txt, extract contents from a line starting with ### until but not including the next such line. The block to be extracted is indicated by variable n passed via the -v option.
$ cat concat.txt ### addr.txt How are you This game is good Today is sunny ### broken.txt top 1234567890 bottom ### sample.txt Just do-it Believe it ### mixed_fs.txt pink blue white yellow car,mat,ball,basket $ awk -v n=2 ##### add your solution here ### broken.txt top 1234567890 bottom $ awk -v n=4 ##### add your solution here ### mixed_fs.txt pink blue white yellow car,mat,ball,basket
f) For the input file ruby.md, replace all occurrences of ruby (irrespective of case) with Ruby. But, do not replace any matches between ```ruby and ``` lines (ruby in these markers shouldn't be replaced either).
$ awk ##### add your solution here ruby.md > out.md $ diff -sq out.md expected.md Files out.md and expected.md are identical

Download 125.91 Kb.

Share with your friends:
1   ...   45   46   47   48   49   50   51   52   ...   60




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

    Main page