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


Gotchas and Tips Prefixing $ for variables



Download 125.91 Kb.
Page56/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   52   53   54   55   56   57   58   59   60
Learn GNU AWK

Gotchas and Tips

Prefixing $ for variables


Some scripting languages like bash require a $ prefix when you need the value stored in a variable. For example, if you declare name='Joe' you'd need echo "$name" to print the value. This may result in using $ prefix and other bashisms in awk as well when you are a beginner. To make it a bit worse, awk has the $N syntax for accessing field contents, which could result in false comprehension that all variables need $ prefix to access their values. See also unix.stackexchange: Why does awk print the whole line when I want it to print a variable?.
$ # silently fails, $word becomes $0 because of string to numeric conversion $ awk -v word="cake" '$2==$word' table.txt $ # works when the variable is used correctly $ awk -v word="cake" '$2==word' table.txt blue cake mug shirt -7 $ # here 'field' gets replaced with '2' and hence $2 is printed $ awk -v field=2 '{print $field}' table.txt bread cake banana

Dos style line endings


As mentioned before, line endings differ from one platform to another. On Windows, it is typically a combination of carriage return and the newline character and referred as dos style line endings. Since GNU awk allows multicharacter RS, it is easy to handle. See stackoverflow: Why does my tool output overwrite itself and how do I fix it? for a detailed discussion and various mitigation methods.
$ # no issue with unix style line ending $ printf 'mat dog\n123 789\n' | awk '{print $2, $1}' dog mat 789 123 $ # dos style line ending causes trouble $ printf 'mat dog\r\n123 789\r\n' | awk '{print $2, $1}' mat 123 $ printf 'mat dog\r\n123 789\r\n' | awk '{sub(/$/, ".")} 1' .at dog .23 789 $ # use \r?\n if you want to handle both unix and dos style with same command $ # note that ORS would still be newline character only $ printf 'mat dog\r\n123 789\r\n' | awk -v RS='\r\n' '{print $2, $1}' dog mat 789 123 $ printf 'mat dog\r\n123 789\r\n' | awk -v RS='\r\n' '{sub(/$/, ".")} 1' mat dog. 123 789.

Download 125.91 Kb.

Share with your friends:
1   ...   52   53   54   55   56   57   58   59   60




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

    Main page