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



Download 125.91 Kb.
Page20/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   16   17   18   19   20   21   22   23   ...   60
Learn GNU AWK

Summary


Regular expressions is a feature that you'll encounter in multiple command line programs and programming languages. It is a versatile tool for text processing. Although the features in awk are less compared to those found in programming languages, they are sufficient for most of the tasks you'll need for command line usage. It takes a lot of time to get used to syntax and features of regular expressions, so I'll encourage you to practice a lot and maintain notes. It'd also help to consider it as a mini-programming language in itself for its flexibility and complexity.

Exercises


a) For the given input, print all lines that start with den or end with ly.
$ lines='lovely\n1 dentist\n2 lonely\neden\nfly away\ndent\n' $ printf '%b' "$lines" | awk ##### add your solution here lovely 2 lonely dent
b) Replace all occurrences of 42 with [42] unless it is at the edge of a word. Note that word in these exercises have same meaning as defined in regular expressions.
$ echo 'hi42bye nice421423 bad42 cool_42a 42c' | awk ##### add your solution here hi[42]bye nice[42]1[42]3 bad42 cool_[42]a 42c
c) Add [] around words starting with s and containing e and t in any order.
$ words='sequoia subtle exhibit asset sets tests site' $ echo "$words" | awk ##### add your solution here sequoia [subtle] exhibit asset [sets] tests [site]
d) Replace the space character that occurs after a word ending with a or r with a newline character.
$ echo 'area not a _a2_ roar took 22' | awk ##### add your solution here area not a _a2_ roar took 22
e) Replace all occurrences of [4]|* with 2 for the given input.
$ echo '2.3/[4]|*6 foo 5.3-[4]|*9' | awk ##### add your solution here 2.3/26 foo 5.3-29
f) awk '/\<[a-z](on|no)[a-z]\>/' is same as awk '/\<[a-z][on]{2}[a-z]\>/'. True or False? Sample input shown below might help to understand the differences, if any.
$ printf 'known\nmood\nknow\npony\ninns\n' known mood know pony inns
g) Print all lines that start with hand and ends with s or y or le or no further character. For example, handed shouldn't be printed even though it starts with hand.
$ lines='handed\nhand\nhandy\nunhand\nhands\nhandle\n' $ printf '%b' "$lines" | awk ##### add your solution here hand handy hands handle
h) Replace 42//5 or 42/5 with 8 for the given input.
$ echo 'a+42//5-c pressure*3+42/5-14256' | awk ##### add your solution here a+8-c pressure*3+8-14256
i) For the given quantifiers, what would be the equivalent form using {m,n} representation?

  • ? is same as

  • * is same as

  • + is same as


Download 125.91 Kb.

Share with your friends:
1   ...   16   17   18   19   20   21   22   23   ...   60




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

    Main page