Awk and Special Brackets Delimiters

awk and special brackets delimiters

Just use [][{}] to define that you can use either of these: [, ], { or } as field separators

awk -F"[][{}]" '{print ...}' file

In general, you say -F"[PATTERNS]".

Test

$ echo ".......{INFO1}.....[INFO2]...." | awk -F"[][{}]" '{print $2}'
INFO1
$ echo ".......{INFO1}.....[INFO2]...." | awk -F"[][{}]" '{print $4}'
INFO2

awk error with [ and ] delimiters

This should work:

echo "xxxxx.yyyyy[2].zzzzz" | awk -F '[][]' '{print $2}'
2

Order of ] before [ inside character class is important here.

This shall also work by double escaping [ and ] using alternation regex:

echo "xxxxx.yyyyy[2].zzzzz" | awk -F '\\[|\\]' '{print $2}'
2

OR:

echo "xxxxx.yyyyy[2].zzzzz" | awk -F '[\\[\\]]' '{print $2}'
2

awk: How can I use [[ and ]] as field separators/delimiters?

-F takes a regular expression as an argument. -F '\\[\\[|]]' should be sufficient. The [ needs to be escaped to prevent it from starting a bracket expression, but ] without a matching [ is treated literally.

Awk strings enclosed in brackets

How about this? (Note that multiple character delimiters seem not to be available in GNU awk 4 respectively in the awk version the OP is using.)

pattern='[CURRENT_DATE][THREAD_ID][PROCESS_NAME]Some random text here'
echo $pattern

awk -F '[' '{print substr($3, 1, length($3)-2)}' <<< "$pattern"

AWK match exact string inside square brackets

You are mixing regular expressions and plain strings. [ is a regex special character, but you are not using a regex here, just a literal string comparison. You don't need any escaping at all (though you might want to reverse the usage of single and double quotes for simplicity, unless you are actually using Windows).

awk '$2 == "[403]"' file.txt

In basically all the Unix shells, the double quotes you used don't protect dollar signs, so $2 would be substituted by the shell, probably with nothing, or else with some unrelated string (whatever got passed in as the second command-line argument to the shell).

The -F option, if present, requires an argument; but based on your example data, the default field separator - any sequence of whitespace - should work fine. If you want to force it to e.g. a single space, try -F ' '.

Awk get specific word between special characters

This works:

awk '/^\[/ {sub(/\[/, "");sub(/\].*/,"");print}'

Presuming that the lines have no leading spaces.

You don't need gsub() as there is only a single match on the line.

You could do it with a single gensub(), sed or even better perl but those alternatives may not be available.

regex match square brackets once

Your regex is almost right. Just put the * inside the round brackets (in order to have the whole text inside the only group) and remember to use the ^ and $ delimiters (to avoid matching [[parent]]):

^\[([a-z]*)\]$

awk Extract Text Nth Occurrence Square Brackets (Containing Line Break In File Text)

how to extract the text between [ and ] with the given record number

You may try this gnu-awk command that will work irrespective of presence of line break between bracket pairs

awk -v n=2 -v RS='\\[[^]]*]' 'RT && NR == n {print substr(RT, 2, length(RT)-2)}' file

{G,H,I}

Since we are using custom RS of [...] it will print correct record no matter if 2nd pair of [...] is in first line or second line.

Adding brackets after paricular string

Assume that the file test.txt has multi lines:

name1=v1 v2 v3

name2=v4 v5 v6

...

Do this command while read -r line; do line=${line/=/=(}; line="$line)"; echo $line; done < test.txt

Explain:

  • Replace = by =(
  • Add ) to the end of each line.


Related Topics



Leave a reply



Submit