I am writing a CSH script and attempting to extract text from a source string given a key.
!/bin/csh -f
set source = "Smurfs\n\tPapa\nStar Trek\n\tRenegades\n\tStar Wars\n\tThe Empire Strikes Back\n"
set toFind = "Star Trek"
set regex = "$toFind[\s]*?(.*?)[\s]*?"
set match = `expr $source : $regex`
echo $match
The above code does not work, so I am missing something. I tried placing "Star Trek" directory inside rather than a variable. I should see Regenages as the answer. Had I put "Star Wars" as instead of "Star Trek", I should have seen The Empire Strikes Back.
Google search showed a possible solution using grep, such as
match = `grep -Po '<something>' <<< $source
I did not know what to put for <something>, nor am I an expert in grep.
In the real code, I am reading text from a file. I just simplified things here.
Thoughts?
exprthat supports perl-regex, that will never work. BUT now I am reading your initial problem descrip, "attempting to extract text from a source string given a key.". ?? key/values. Why are you using such an unhelpful solution? why notkey[str]="value"or even justmyKey=Renegades? Ah, " I am reading text from a file." it might have helped to have that near the top of your Q. .....cshtime on converting 2 lines of input into variable assignments, but it seems you have to deal with spaces in your var-names, so nix toStar Trek="Renegade")-; . Doing quick research, I don't see thatcshcan doarr[key]="value"arrays, onlyset arr = (one two three), which are then referenced asecho $arr[1] $arr[3]etc. If you're processing a file with an extenal utility, thesedis good, butawkwill give you much more understandable code. Busy now, so that's all I can come up with now.exprutility only uses basic regexs, but it's not documented inGNU coreutils 8.30version ofman expr. ( maybe ininfo '(coreutils) expr invocation'? ). You do know that usingcshis shell scripting w one hand timed behind your back? OK as a learning challenge, but jobs/work, you'll do much better getting good atbashorzshor something even newer (fish?) (man grepsearch for ERE is the best I can find). Good luck.