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
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
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.
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
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 .
./main
asd asd
asd asd
asd asd asd