Reputation: 12518
I have a configuration file and need to parse out some values using bash
Ex. Inside config.txt
some_var= Not_needed
tests= spec1.rb spec2.rb spec3.rb
some_other_var= Also_not_needed
Basically I just need to get "spec1.rb spec2.rb spec3.rb" WITHOUT all the other lines and "tests=" removed from the line.
I have this and it works, but I'm hoping there's a much more simple way to do this.
while read run_line; do
if [[ $run_line =~ ^tests=* ]]; then
echo "FOUND"
all_selected_specs=`echo ${run_line} | sed 's/^tests= /''/'`
fi
done <${config_file}
echo "${all_selected_specs}"
Upvotes: 2
Views: 89
Reputation: 133458
try:
all_selected_specs=$(awk '/^tests/{sub(/.*= /,"");print}' Input_file)
searching for string tests which comes in starting of a line then substituting that line's all values till (= ) to get all spec values, once it is substituted then we are good to get the spec values so printing that line. Finally saving it's value to variable with $(awk...).
Upvotes: 0
Reputation: 42999
How about grep
and cut
?
all_selected_specs=$(grep "^tests=" "$config_file" | cut -d= -f2-)
Upvotes: 0
Reputation: 6110
This should work too
grep "^tests" ${config_file} | sed -e "s/^tests= //"
Upvotes: 2
Reputation: 361585
all_selected_specs=$(awk -F '= ' '$1=="tests" {print $2}' "$config_file")
Using a field separator of "= "
, look for lines where the first field is tests
and print the second field.
Upvotes: 4