contentJSON is :-
{"id":"0","name":"inc_timestamp","workspaceId":"37158928","infoJSON":"{a:{\"a\":\"bcd\",\"b\":{\"c\":\"d\"}}}","contentJSON":"{\n  \"tasks\": [\n    {\n      \"name\": \"Input\",\n      \"taskType\": \"executeCustomSQLQueryForIncrementalLoad\",\n      \"id\": 10,\n      \"x\": 95,\n      \"y\": 44,\n      \"inputConnectors\": [],\n      \"outputConnectors\": [\n        {\n          \"nodeID\": 11,\n          \"type\": \"Output\",\n          \"name\": \"\"\n        }\n      ],\n      \"argsMap\": {\n        \"taskId\": 10,\n        \"datasetId\": 49053696,\n        \"deltaColumnName\": \"timestamp\",\n        \"deltaColumnDataType\": \"timestamp\",\n        \"deltaColumnValue\": \"null\",\n        \"primaryKeysList\": [\n          \"id\"\n        ]\n      },\n      \"datasetId\": 49053696\n    },\n    {\n      \"name\": \"Output\",\n      \"taskType\": \"saveToES\",\n      \"id\": 11,\n      \"x\": 453,\n      \"y\": 44,\n      \"inputConnectors\": [\n        {\n          \"nodeID\": 10,\n          \"type\": \"Input\",\n          \"name\": \"\"\n        }\n      ],\n      \"outputConnectors\": [],\n      \"argsMap\": {\n        \"bizvizcubeId\": 46759937,\n        \"cfg\": {\n          \"es.mapping.id\": \"id\"\n        }\n      }\n    }\n  ],\n  \"connections\": [\n    {\n      \"source\": {\n        \"taskId\": 10,\n        \"connectorIndex\": 0\n      },\n      \"dest\": {\n        \"taskId\": 11,\n        \"connectorIndex\": 0\n      }\n    }\n  ],\n  \"isIncrementalLoadModeWorkflow\": 1\n}\n"}
wfid is : 49086475
finally wfid is : 49086475
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.136s
[INFO] Finished at: Tue Oct 24 15:30:48 IST 2017
[INFO] Final Memory: 33M/392M
[INFO] ------------------------------------------------------------------------

I required to grep 49086475 which changes every time I execute my program.

Please guide me to capture the text after finally wfid is :.

  • 4
    The description of the "grep" family of commands is: print lines matching a pattern; therefore the verb "to grep" must mean something like copy to the output one or more lines from the file. So what is wrong with just fgrep 'finally wfid is :'? – Marc van Leeuwen Oct 24 '17 at 11:40
  • 1
    Two suggestions. One - use appropriate tools for the job - if it's json use something that understands json; and second - when you provide input examples, provide something that is complete, because your input looks like mess and incomplete. – Sergiy Kolodyazhnyy Oct 24 '17 at 23:23
  • @SergiyKolodyazhnyy: It looks like only the first entry in the file is JSON. – Dubu Oct 27 '17 at 7:40

Use awk:

awk '/finally wfid is :/{print $NF}'

... which prints the last field of the line containing finally wfid is :.

  • 1
    A minor clarification, use: awk '/finally wfid is :/{print $NF}' <filename> or cat <filename> | awk '/finally wfid is :/{print $NF}'. – pa4080 Oct 24 '17 at 11:29

Use grep in this way:

grep -oP 'finally wfid is : \K.*' <filename>
  • The explanation from the source answer:

    With grep and the -P for having PCRE (Interpret the pattern as a Perl-Compatible Regular Expression) and the -o to print matched pattern alone. The \K notify will ignore the matched part come before itself.

Or you can use sed in this way:

sed -n 's/finally wfid is : //p' <filename>
  • This command will print only the lines that contain the string finally wfid is :, but it will be substituted with empty string.

Despite all the good solutions posted already, I'll add one that only uses grep. I often end up doing things this way because of my inability to remember sed and awk commands.

grep 'finally wfid is' input_file | grep -o '[0-9]*'

The first grep will take the correct line. The second one uses the -o argument to take only matching part of the line, which here is anything numerical.


Just do:

grep "finally wfid is" <filename> | awk '{print $5;}'

This will output the number which is in that line alone. If you want the whole line just omit the awk.


It seems to me that all the answers posted so far have missed the point of the question.  Or, perhaps, I have.  I understand the question to mean

I need to find the text (eight-digit number) that appears after finally wfid is : and then grep the file for that text.

The posted answers do the first part fine, but don’t finish the job.  IMHO, muru’s answer is the best; I’ll build on that:

grep "$(awk '/finally wfid is :/{print $NF}' contentJSON)" contentJSON

where $(command) captures the output from command (in this case, the awk) and uses it in the outer command (in this case, grep).  I assume from the question that the filename is contentJSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.