lio
lio

Reputation: 145

find specific words in text files

I have many text files in different sub directories, whereby each files has the file extension ".pro".

Each file contains of one or more lines where the text "PRO <name>," or "Function <name>," occurs. After the <name> follows a comma followed by variables.

I would like to generate a list consisting of 2 columns:

For the two example files/scripts below the resulting text would be:

<full path>read_scenes_out.pro  PRO READ_SCENES_CLA
<full path>read_scenes_out.pro  PRO PRO READ_TAU_IN
<full path>read_scenes_out.pro  FUNCTION READ_HRIT
<full path>read_scenes_in.pro   PRO READ_SCENES_FLG
<full path>read_scenes_in.pro   PRO READ_SCENES_ELEV
<full path>read_scenes_in.pro   PRO READ_TAU_IN
<full path>read_scenes_in.pro   FUNCTION READ_HRIT2

Since I am a beginner with Linux I cannot solve this myself. The only small success was two find out how to list all full path's of pro files in all subdirectories:

find . | grep ".pro" | sort -z

May be this can be combined with that answer.


Two example files ("..." is a place holder for programming code):


Upvotes: 1

Views: 59

Answers (1)

tripleee
tripleee

Reputation: 189387

Try something like

find . -name '*.pro' \
  -exec grep -Eo '(PRO|FUNCTION)[ ]+[^ ,]+' /dev/null {} +

where the whitespace inside both character classes [...] consists of a space and a tab.

The file name separator output by grep is a colon; it should not be hard to change this by way of a simple sed postprocessing step if it's important. Or maybe switch from grep to Awk to have more control over how the output is generated.

Upvotes: 2

Related Questions