简体   繁体   中英

sed Unrecognized command

I am trying to match the pattern and print above few lines, using sed i can able to do that but in script i am not able to pass that. i am getting the below it takes arguments as commands how to avoid that.

This is my simple for loop script

#!/bin/sh
for i in `cat test.txt`
do
echo "***************************************************************************"
echo '\n'
echo $i
sed -n "'1,/$i/p'  debug.log | tail -14"
echo '\n'
echo "***************************************************************************"
done

sed -n "'1,/Ref555330/p' debug.log | tail -14"

 Unrecognized command: '1,/Ref555330/p' debug.log | tail -14 

Kindly guide me to solve this thread.

仅对sed命令提供双引号

sed -n "1,/$i/p"  debug.log | tail -14

sed is just not the right tool for anything involving more than 1 line and any time you write a loop in shell just to manipulate text you have the wrong approach. It looks like you have a file named test.txt of regular expressions and you want to get the 14 lines above each RE from some other file debug.log. That's just one simple awk command, something like this (untested since you didn't provide any sample input or expected output):

awk '
NR==FNR { res[$0]; next }
{
    line[FNR] = $0
    for (re in res)
        if ($0 ~ re)
            hit[FNR]
    next
}
END {
    for (i=1; i<=FNR; i++) {
        if (i in hit) {
            print "***************************************************************************"
            for (j=i-14; j<=i; j++)
                print line[j]
            print "***************************************************************************"
        }
    }
}
' test.txt debug.log

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