2

I have either a text file or Textarea input which needs to be scanned for commands which need a required format.
The commands must be in the following order and aren't string sensitive however integers must be of length 1.

1) START 2,4,GO
2) Like
3) ATE, SWAM, SING, DANCE

Note,the 3rd command has to be one of those 4 words. What's the best way to do this? Should I make an array and then use regexp?

An example:

Random text 
ignore this
StArt 2,4,Go
Like
aTE
WORKed
NewCommand again 
StarT 9,1,Go
Like
Swam

Hence, this file has 2 commands. Based on the commands I will do an action. Output is void. i.e.) a method like regexp might be used and then when a command is read it will call a method which will map the methods like function Ate(e) {console.log("ATE")}

handlebears
  • 2,168
  • 8
  • 18
Noob2321
  • 41
  • 2
  • 1
    You have shown the sample input. Can you please show the corresponding sample output? Do you just want that non-command lines are removed? Do you want the three lines of each command to be joined on one line? Or what? – Enlico Nov 17 '19 at 18:40
  • @EnricoMariaDeAngelis I just need to trigger an action once those series of commands are met and then parse those commands into a function and then ill do an action depending on what was typed in command 1 and 3 – Noob2321 Nov 17 '19 at 19:36
  • My total lack of JS does not allow me to help you further on this, but someone else will certainly be up to it. – Enlico Nov 17 '19 at 19:44
  • @EnricoMariaDeAngelis I need it done using a file too so if you can do this in like a .csv or txt file I'd be happy :) – Noob2321 Nov 17 '19 at 19:46
  • Then my answer shows how to "filter" `yourfile` using `sed`. The output of that `sed` command is `yourfile` deprived of the lines which are not commands. However, seriously, given my zero knowledge of JS I cannot even guess if my answer makes any sense for your application. – Enlico Nov 17 '19 at 19:49

1 Answers1

0

The following sed program only prints the relevant lines:

sed -n '/^start [0-9],[0-9],go$/I{N;N;/\nlike\n\(ate\|swam\|sing\|dance\)/Ip}' yourfile

Note that the I flag at the end of the address /…/ (or of the s/…/…/ command) is not portable, as explained in a comment to this answer. If you need portability, you have to change like to [lL][iI][kK][eE] and so on.

Enlico
  • 23,259
  • 6
  • 48
  • 102