Skip to content
Advertisement

grep to read exact data from text file

I have file test1.txt file, i am trying to read variable which is enclosed in double quotes and starting with hyphen for eg: “-color“. i trying to use this grep command cat test1.txt | grep getParm | sed ‘s/getParm(/ /;s/&/ /;s/,/ /;s/”/ /g’ | awk ‘{print $3}’ , where i am not able to read exact data for all variables present in file. for some of variables which is enclosed in if…else condition i don’t get exact data. Please help me to read the data required. i have posted required data in question.

cat test1.txt | grep getParm

 if(!param.getParm(buf, "-logFile"))
   parameters()->getParm(&color, "-color");
   if (param.getParm(&species, "-species"))
   if (param.getParm(&width, "-width"))
   if (param.getParm(&xferLength, "-length"))
   else if (param.getParm(&xferLength, "-LargeTransferLength"))
   if (param.getParm(&cmdLineIoParms->pattern, "-pattern"))
      param.getParm(&cmdLineParams->volume,"-volume");
    cat test1.txt | grep getParm | sed 's/getParm(/  /;s/&/  /;s/,/  /;s/"/  /g' |  awk  '{print  $3}'

    -logFile
    -color
    species
    width
    xferLength
    (param.
    cmdLineIoParms->pattern
    -volume

expected ouput:-

-logFile
-color
-species
-width
-length
-LargeTransferLength
-pattern
-volume

Advertisement

Answer

With GNU grep, positiv lookbehind, a non-greedy * and positiv lookahead:

... | grep -oP '(?<=")-.*?(?=")'

Output:

-logFile
-color
-species
-width
-length
-LargeTransferLength
-pattern
-volume

See: The Stack Overflow Regular Expressions FAQ

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement