2

I'm trying to use SED to extract text from two words, such as "Account" and "Recognized", and I'd like that the searching be case insensitive. So I tried to use the I parameter, but receive this error message:

cat Security.txt | sed -n "/Account/,/Recognized/pI" | sed -e '1d' -e '$d' sed: -e expression #1, char 24: extra characters after command

jww
  • 97,681
  • 90
  • 411
  • 885
user2965031
  • 61
  • 2
  • 7
  • 1
    Post the content of `security.txt`. – Мона_Сах Aug 27 '16 at 06:31
  • Security.txt can be written in english or italian, like these examples: cat EN_Security.txt | sed -n "/Account/,/Recognized/p" | head Account Activity * Checkpoint Flow Started Saturday, April 16, 2016 at 11:26am UTC+02 ::: % cat EN_Security.txt | sed -n "/Account/,/Recognized/p" | tail * Session updated Wednesday, June 17, 2015 at 2:04pm UTC+02 ::: Recognized Machines – user2965031 Aug 27 '16 at 06:43
  • % cat IT-Sicurezza.txt | sed -n "/account/,/Recognized/p" | head Attività account * Login Venerdì 25 settembre 2015 alle ore 17:55 UTC+02 ::: % cat IT-Sicurezza.txt | sed -n "/account/,/Recognized/p" | tail * Login Martedì 4 febbraio 2014 alle ore 20:28 UTC+01 ::: Recognized Machines – user2965031 Aug 27 '16 at 06:46
  • 2
    add the example to question instead of comment where formatting is a mess – Sundeep Aug 27 '16 at 06:58
  • @user2965031 Why not copy paste the exact text here.. – Мона_Сах Aug 27 '16 at 07:24
  • It's too long. They are log files. – user2965031 Aug 27 '16 at 07:33

3 Answers3

5

Avoid useless use of cat

/pattern/I is how to specify case-insensitive matching in sed

sed -n "/Account/I,/Recognized/Ip" Security.txt | sed -e '1d' -e '$d'

You can use single sed command to achieve the same:

sed -n '/account/I,/recognized/I{/account/I!{/recognized/I!p}}' Security.txt

Or awk

awk 'BEGIN{IGNORECASE=1} /account/{f=1; next} /recognized/{f=0} f' Security.txt

Reference:

Community
  • 1
  • 1
Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • It's very strange, the command sed -n "/account/I,/recognized/Ip" works on "EN-Security.txt" and doesn't work on "IT-Security.txt", even though both contain the sequence "Recognized Machines". – user2965031 Aug 27 '16 at 07:32
  • 1
    @user2965031 that doesn't help in anyway what your problem might be... check out http://stackoverflow.com/help/mcve and edit your question accordingly – Sundeep Aug 27 '16 at 07:37
0

Use:

sed -n "/Account/,/Recognized/Ip"

i.e. change the order to: Ip instead of pI

Dave Grabowski
  • 1,619
  • 10
  • 19
  • Security.txt file sometimes contains both italian sentences (e.g. "Attività account") that english sentences (e.g. "Account Activity" ), and the command that you've suggested, if it's syntactically corrected, it doesn't grab the difference between the words "account" and "Account". – user2965031 Aug 27 '16 at 06:55
  • it should be `/Account/I` as well – Sundeep Aug 27 '16 at 06:59
  • @user2965031 I just focused on the error, but yes, if both words have to be case insensitive then `/Account/I` should be used. – Dave Grabowski Aug 27 '16 at 07:02
-1

You have useless use of cat where you should've fed the file directly to sed. Below could be a way of doing it.

$ cat file.txt 
Some stuff Account sllslsljjs Security.
Another stuff account name and ffss security.
$ sed -nE 's/^.*account[[:blank:]]*(.*)[[:blank:]]*security.*$/\1/pI' file.txt
sllslsljjs 
name and ffss 

The [[:blank:]]* is greedy and will strip the spaces before and after the required text. The -E option enables the use of extended regular expressions.

Мона_Сах
  • 322
  • 3
  • 12