- 1 year ago
Escaping nested double quotes in awk
Comment
1 / 1
In AWK, you can escape nested double quotes by using the backslash () character before each double quote that you want to treat as a literal character. Here's an example of how to do it:
Let's say you have the following input file named "data.txt":
arduino"John" "Doe" "25"
"Alice" "Smith" "30"
"Bob" "Johnson" "28"
And you want to add double quotes around each field using AWK:
bashawk '{ for (i=1; i<=NF; i++) { $i = "\"" $i "\"" } }1' data.txt
Explanation:
- The
for
loop iterates through each field (NF is the number of fields in the current record). $i = "\"" $i "\""
: This statement adds a double quote at the beginning and end of each field, effectively escaping the nested double quotes.
Output:
arduino"John" "Doe" "25"
"Alice" "Smith" "30"
"Bob" "Johnson" "28"
As you can see, each field in the input file is now enclosed in double quotes. The backslash () before each double quote ensures that the double quote is treated as a literal character rather than a string delimiter in AWK.