grep -o:保持输入行格式

时间:2022-01-08 08:56:57
$ echo "abca\ndeaf" | grep -o a  
a
a
a

I am looking for the output:

我在找输出:

aa
a

Or perhaps

或者

a a
a

or even

甚至

a<TAB>a
a

(this is a very very simplified example)

(这是一个非常简单的例子)

I just want it not to throw away the line grouping.

我只是想让它不要抛弃线分组。

4 个解决方案

#1


0  

If you're willing to use perl:

如果您愿意使用perl:

$ echo $'abca\ndeaf' | perl -ne '@m = /a/g; print "@m\n"'
a a
a

#2


3  

You can do it with sed by removing any character that isn't a:

你可以通过去掉任何不是a的字符来使用sed:

echo "abca\ndeaf" | sed 's/[^a]//g' 
aa
a

It can't be done with grep alone.

它不能单独使用grep。

#3


3  

@sudo_O's answer shows how to do this with single-character strings. The difficulty level is raised if you want to match longer strings.

@sudo_O的回答显示了如何使用单字符字符串来实现这一点。如果您想匹配较长的字符串,难度级别将会提高。

One way to do it is by parsing the output of grep -n -o, like so:

一种方法是解析grep -n -o的输出,如下所示:

$ cat mgrep
#!/bin/bash

# Print each match along with its line number.
grep -no "$@" | {
    matches=()  # An array of matches to be printed when the line number changes.
    lastLine=   # Keep track of the current and previous line numbers.

    # Read the matches, with `:' as the separator.
    while IFS=: read line match; do
        # If this is the same line number as the previous match, add this one to
        # the list.
        if [[ $line = $lastLine ]]; then
            matches+=("$match")

        # Otherwise, print out the list of matches we've accumulated and start
        # over.
        else
            (( ${#matches[@]} )) && echo "${matches[@]}"
            matches=("$match")
        fi

        lastLine=$line
    done

    # Print any remaining matches.
    (( ${#matches[@]} )) && echo "${matches[@]}"
}

Example usage:

使用示例:

$ echo $'abca\ndeaf' | ./mgrep a
a a
a
$ echo $'foo bar foo\nbaz\ni like food' | ./mgrep foo
foo foo
foo

#4


1  

Based off John Kugelman's solution, this one works with one input file and

基于John Kugelman的解决方案,这个解决方案可以处理一个输入文件,然后就可以了

grep -on abc file.txt | awk -v RS='[[:digit:]]+:' 'NF{$1=$1; print}'

#1


0  

If you're willing to use perl:

如果您愿意使用perl:

$ echo $'abca\ndeaf' | perl -ne '@m = /a/g; print "@m\n"'
a a
a

#2


3  

You can do it with sed by removing any character that isn't a:

你可以通过去掉任何不是a的字符来使用sed:

echo "abca\ndeaf" | sed 's/[^a]//g' 
aa
a

It can't be done with grep alone.

它不能单独使用grep。

#3


3  

@sudo_O's answer shows how to do this with single-character strings. The difficulty level is raised if you want to match longer strings.

@sudo_O的回答显示了如何使用单字符字符串来实现这一点。如果您想匹配较长的字符串,难度级别将会提高。

One way to do it is by parsing the output of grep -n -o, like so:

一种方法是解析grep -n -o的输出,如下所示:

$ cat mgrep
#!/bin/bash

# Print each match along with its line number.
grep -no "$@" | {
    matches=()  # An array of matches to be printed when the line number changes.
    lastLine=   # Keep track of the current and previous line numbers.

    # Read the matches, with `:' as the separator.
    while IFS=: read line match; do
        # If this is the same line number as the previous match, add this one to
        # the list.
        if [[ $line = $lastLine ]]; then
            matches+=("$match")

        # Otherwise, print out the list of matches we've accumulated and start
        # over.
        else
            (( ${#matches[@]} )) && echo "${matches[@]}"
            matches=("$match")
        fi

        lastLine=$line
    done

    # Print any remaining matches.
    (( ${#matches[@]} )) && echo "${matches[@]}"
}

Example usage:

使用示例:

$ echo $'abca\ndeaf' | ./mgrep a
a a
a
$ echo $'foo bar foo\nbaz\ni like food' | ./mgrep foo
foo foo
foo

#4


1  

Based off John Kugelman's solution, this one works with one input file and

基于John Kugelman的解决方案,这个解决方案可以处理一个输入文件,然后就可以了

grep -on abc file.txt | awk -v RS='[[:digit:]]+:' 'NF{$1=$1; print}'