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



Download 125.91 Kb.
Page40/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   36   37   38   39   40   41   42   43   ...   60
Learn GNU AWK

index


The index function is useful when you need to match a string literally in the given input string. This is similar to grep -F functionality of matching fixed strings. The first argument to this function is the input string and the second is the string to be matched literally. The return value is the index of matching location and 0 if there is no match.
$ cat eqns.txt a=b,a-b=c,c*d a+b,pi=3.14,5e12 i*(t+9-g)/8,4-a+b $ # no output because the metacharacters aren't escaped $ awk '/i*(t+9-g)/' eqns.txt $ # same as: grep -F 'i*(t+9-g)' eqns.txt $ awk 'index($0, "i*(t+9-g)")' eqns.txt i*(t+9-g)/8,4-a+b $ # check only the last field $ awk -F, 'index($NF, "a+b")' eqns.txt i*(t+9-g)/8,4-a+b $ # index not needed if entire field/line is being compared $ awk -F, '$1=="a+b"' eqns.txt a+b,pi=3.14,5e12
The return value is also useful to ensure match is found at specific positions only. For example start or end of input string.
$ # start of string $ awk 'index($0, "a+b")==1' eqns.txt a+b,pi=3.14,5e12 $ # end of string $ awk -v s="a+b" 'index($0, s)==length()-length(s)+1' eqns.txt i*(t+9-g)/8,4-a+b
Recall that -v option gets parsed by awk's string processing rules. So, if you need to pass a literal string without falling in backslash hell, use ENVIRON instead of -v option.
$ echo 'a\b\c\d' | awk -v s='a\b' 'index($0, s)' $ echo 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)' a\b\c\d $ echo 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])' a\b\c\d

system


External commands can be issued using the system function. Any output generated by the external command would be as usual on stdout unless redirected while calling the command.
$ awk 'BEGIN{system("echo Hello World")}' Hello World $ wc table.txt 3 15 79 table.txt $ awk 'BEGIN{system("wc table.txt")}' 3 15 79 table.txt $ awk 'BEGIN{system("seq 10 | paste -sd, > out.txt")}' $ cat out.txt 1,2,3,4,5,6,7,8,9,10 $ cat t2.txt I bought two balls and 3 bats $ echo 'f1,t2,f3' | awk -F, '{system("cat " $2 ".txt")}' I bought two balls and 3 bats
Return value of system depends on exit status of the executed command. See gawk manual: Input/Output Functions for details.
$ ls xyz.txt ls: cannot access 'xyz.txt': No such file or directory $ echo $? 2 $ awk 'BEGIN{s=system("ls xyz.txt"); print "Exit status: " s}' ls: cannot access 'xyz.txt': No such file or directory Exit status: 2

Download 125.91 Kb.

Share with your friends:
1   ...   36   37   38   39   40   41   42   43   ...   60




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

    Main page