'select only a word that is part of colon

I have a text file using markup language (similar to wikipedia articles)

cat test.txt
This is a sample text having: colon in the text. and there is more [[in single or double: brackets]]. I need to select the first word only.
and second line with no [brackets] colon in it.

I need to select the word "having:" only because that is part of regular text. I tried

grep -v '[*:*]' test.txt

This will correctly avoid the tags, but does not select the expected word.



Solution 1:[1]

A combined solution using sed and awk:

sed 's/ /\n/g' test.txt | gawk 'i==0 && $0~/:$/{ print $0 }/\[/{ i++} /\]/ {i--}'
  • sed will change all spaces to a newline
  • awk (or gawk) will output all lines matching $0~/:$/, as long as i equals zero
  • The last part of the awk stuff keeps a count of the opening and closing brackets.

Another solution using sed and grep:

sed -r -e 's/\[.*\]+//g' -e 's/ /\n/g' test.txt  | grep ':$'
  • 's/\[.*\]+//g' will filter the stuff between brackets
  • 's/ /\n/g' will replace a space with a newline
  • grep will only find lines ending with :

A third on using only awk:

gawk '{ for (t=1;t<=NF;t++){ 
            if(i==0 && $t~/:$/) print $t; 
            i=i+gsub(/\[/,"",$t)-gsub(/\]/,"",$t) }}' test.txt
  • gsub returns the number of replacements.
  • The variable i is used to count the level of brackets. On every [ it is incremented by 1, and on every ] it is decremented by one. This is done because gsub(/\[/,"",$t) returns the number of replaced characters. When having a token like [[][ the count is increased by (3-1=) 2. When a token has brackets AND a semicolon my code will fail, because the token will match, if it ends with a :, before the count of the brackets.

Solution 2:[2]

The square brackets specify a character class, so your regular expression looks for any occurrence of one of the characters * or : (or *, but we said that already, didn't we?)

grep has the option -o to only print the matching text, so something lie

grep -ow '[^[:space:]]*:[^[:space:]]*' file.txt

would extract any text with a colon in it, surrounded by zero or more non-whitespace characters on each side. The -w option adds the condition that the match needs to be between word boundaries.

However, if you want to restrict in which context you want to match the text, you will probably need to switch to a more capable tool than plain grep. For example, you could use sed to preprocess each line to remove any bracketed text, and then look for matches in the remaining text.

sed -e 's/\[.*]//g' -e 's/ [^: ]*$/ /' -e 's/[^: ]* //g' -e 's/ /\n/' file.txt

(This assumes that your sed recognizes \n in the replacement string as a literal newline. There are simple workarounds available if it doesn't, but let's not go there if it's not necessary.)

In brief, we first replace any text between square brackets. (This needs to be improved if your input could contain multiple sequences of square brackets on a line with normal text between them. Your example only shows nested square brackets, but my approach is probably too simple for either case.) Then, we remove any words which don't contain a colon, with a special provision for the last word on the line, and some subsequent cleanup. Finally, we replace any remaining spaces with newlines, and (implicitly) print whatever is left. (This still ends up printing one newline too many, but that is easy to fix up later.)

Alternatively, we could use sed to remove any bracketed expressions, then use grep on the remaining tokens.

sed -e :a -e 's/\[[^][]*\]//' -e ta file.txt |
grep -ow '[^[:space:]]*:[^[:space:]]*'

The :a creates a label a and ta says to jump back to that label and try again if the regex matched. This one also demonstrates how to handle nested and repeated brackets. (I suppose it could be refactored into the previous attempt, so we could avoid the pipe to grep. But outlining different solution models is also useful here, I suppose.)

If you wanted to ensure that there is at least one non-colon character adjacent to the colon, you could do something like

... file.txt |
grep -owE '[^:[:space:]]+:[^[:space:]]*|[^[:space:]]*:[^: [:space:]]+'

where the -E option selects a slightly more modern regex dialect which allows us to use | between alternatives and + for one or more repetitions. (Basic grep in 1969 did not have these features at all; much later, the POSIX standard grafted them on with a slightly wacky syntax which requires you to backslash them to remove the literal meaning and select the metacharacter behavior... but let's not go there.)

Notice also how [^:[:space:]] matches a single character which is not a colon or a whitespace character, where [:space:] is the (slightly arcane) special POSIX named character class which matches any whitespace character (regular space, horizontal tab, vertical tab, possibly Unicode whitespace characters, depending on locale).

Awk easily lets you iterate over the tokens on a line. The requirement to ignore matches within square brackets complicates matters somewhat; you could keep a separate variable to keep track of whether you are inside brackets or not.

awk '{ for(i=1; i<=NF; ++i) {
        if($i ~ /\]/) { brackets=0; next }
        if($i ~ /\[/) brackets=1;
        if(brackets) next;
        if($i ~ /:/) print $i }' file.txt

This again hard-codes some perhaps incorrect assumptions about how the brackets can be placed. It will behave unexpectedly if a single token contains a closing square bracket followed by an opening one, and has an oversimplified treatment of nested brackets (the first closing bracket after a series of opening brackets will effectively assume we are no longer inside brackets).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2