简体   繁体   中英

use awk to detect regex patterns and print by line?

I have a text file in this format [ONE testing 1 2 3] [TWO lorem ipsum] [ONE 123]

I want to print `[ONE.+]` line by line.

An example output would be

[ONE testing 1 2 3]
[ONE 123]

I've tried awk '/\\[ONE.+\\]/ { print $1 }' but it didn't work. Can anyone teach me why? And what the correct way is?

awk works line by line, so the expression is only matched once per line. To do it in awk, you can use the match function in a loop. You'd also have to modify your regex to be less greedy, since your expression doesn't magically stop at the first ].

It might be easier to just use grep:

echo  "[ONE testing 1 2 3] [TWO lorem ipsum] [ONE 123]" | grep -o '\[ONE[^]]*\]'

You can try something like this

sed -re 's/(\[ONE[^\[]*\])/\n\1\n/g' temp.txt

Input

[ONE testing 1 2 3] [TWO lorem ipsum] [ONE 123]

Output

[ONE testing 1 2 3]
 [TWO lorem ipsum] 
[ONE 123]

If you want to remove the column with TWO then

sed -re 's/(\[ONE[^\[]*\])()/\n\1\n/g; s/(\[[^ONE][^\[]*\])//g' temp.txt

Output

[ONE testing 1 2 3]

[ONE 123]

If this is part of something bigger:

BEGIN { 
# Change the field-separator, from default blank, to the end-marker 
# for each "field"
    FS = "] "
}
# Get rid of lines which can't possibly match
!/\[ONE/ { next
    }
{
# Test and report each of three fields for starting with [ONE,
# "closing" the field with FS, except for the last which will 
# already be "closed"
if ( $1 ~ /^\[ONE/ ) {
    print $1 FS
    }
if ( $2 ~ /^\[ONE/ ) {
    print $2 FS
    }
if ( $3 ~ /^\[ONE/ ) {
    print $3
    }
}

The "if"s can be replaced by one in a loop if you are so inclined, but watch for the final one, as the FS (field separator) is not needed (unless you have a trailing blank in your data).

"awk" by default takes as a 'single space' as the separator and the 'print $1' command tries to retrieve the first value separated by the default separator.

Try this out :

Let there is a text file named as 'test.txt' containing the three lines.

cat test.txt

[ONE testing 1 2 3]

[TWO lorem ipsum]

[ONE 123]

grep -h '[ONE*' test.txt

[ONE testing 1 2 3]

[ONE 123]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM