Combining awk statements

I have a pretty simple script below:

#!/bin/sh

for i in *.cfg
do
temp=`awk '/^InputDirectory=/' ${i}`
input_dir=`echo ${temp} | awk '{ print substr( $0, 16) }'`
echo ${input_dir}
done

As you can see its opening each cfg file and searching for the line that has "InputDirectory=" setting that to the temp var and then performing a substring to get the directory path set.

This seems to work fine, but just wondering if there might be a way to combine the 2 lines where awk is used or if there might a simpler solution.

thanks

How does the lines with "InputDirectory=" look like in the config files?

IMO it could be shortened to:

for i in *.cfg
do
  awk '/^InputDirectory=/{print substr($0,16)}' "$i"
done

or if you do not have a massive number of .cfg files (much faster):

awk '/^InputDirectory=/{print substr($0,16)}' *.cfg

or

awk 'sub(/^InputDirectory=/,x)' *.cfg

or

sed -n 's/^InputDirectory=//p' *.cfg
1 Like

thanks Scrutinizer. your solution is perfect.

Franklin, the line just looks like /path/to/directory