famedoro
famedoro

Reputation: 1241

How I can extract, in Linux, a string contained in the files by associating it with the name of the file that contains it

I have a set of files in a directory:

file1.jdl
file2.jdl
...
filen.jdl

each file can contains such a string, for example the file1.jdl contains

runexe EXE1 $str1 str2 ...
...other stuff
runexe FILE_EXE2 abc ... ...
...other stuff

How can I extract, using a script, a list containing a pair of data like this, without file path and without the other strings that could be present after the first string next to runexe ?

file1.jdl EXE1 
file1.jdl FILE_EXE2
...
filen.jdl ...

Upvotes: 0

Views: 59

Answers (1)

Jetchisel
Jetchisel

Reputation: 7791

You could loop through the files.

#!/usr/bin/env bash

cd /path/to/file/ || exit

for file in *.jdl; do
  awk '/EXE/{print FILENAME, $0}' "$file"
done

That would be fine if you don't need to do a recursive search, otherwise you need to use find or the shell option globstar

Upvotes: 1

Related Questions