3

Example :

Filename : testest

cat testest
asd ddd asd fgg
ert tttt ert
asss    ffff
asd asd ff

Search string : asd

Desired output :

asd asd
asd asd
chriskelly
  • 7,526
  • 3
  • 32
  • 50
aitgallon
  • 43
  • 2

3 Answers3

0

It's tricky because there's no general way of expressing 'not pattern' in sed. More on this here. However, you can express not asd like this: /\(\b\([^a]..\|.[^s].\|..[^d]\|\S\{1,2\}\|\S\{4,\}\)\b\)

So removing them looks like this: sed -e 's/\(\b\([^a]..\|.[^s].\|..[^d]\|\S\{1,2\}\|\S\{4,\}\)\b\)/ /g'. I.e. doesn't start with a, 2nd letter isn't s, 3rd letter isn't d. Also, not shorter, neither longer than 3 chars.

But you'll have multiple white spaces so you'll have to remove them too: sed -e 's/\s\+/ /g'

So, a one-liner: sed -e 's/\(\b\([^a]..\|.[^s].\|..[^d]\|\S\{1,2\}\|\S\{4,\}\)\b\)/ /g' -e 's/\s\+/ /g'

Imho you rather do it with perl because that supports negative lookaheads.

Community
  • 1
  • 1
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
0

For the details you've provided, the following might be just fine:

arr=()
while IFS=: read -r n w; do
    if [[ "${arr[n]}" ]]; then
        w=" $w"
    fi

    arr[n]="${arr[n]}$w"

done < <(grep -ohn "asd" input_file)

printf '%s\n' "${arr[@]}"

If there are more constraints on the input ( it is very large, for example ), consider using a different, faster solution. awk might be the right tool for the task.


Let's test it ( script name is sof )

input_file:

asd ddd asd fgg
ert tttt ert
asss    ffff
asd asd ff

Output:

./sof
asd asd
asd asd
Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26
0

Sample data

cat test.log
asd ddd asd fgg
ert tttt ert
asss    ffff
asd asd ff
asd asd asd

#Script

    while read line
    do
    echo $line |grep -o asd |xargs

    done <test.log |grep .

Execution result

   ./main
    asd asd
    asd asd
    asd asd asd
P....
  • 17,421
  • 2
  • 32
  • 52