2

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}"
3
  • Why don't you just run the sed command on the whole file with sed -n 's/.../.../p'? Commented May 10, 2017 at 19:08
  • Sorry, not sure what you mean, unfortunately I don't use shell scripting a lot. Commented May 10, 2017 at 19:46
  • man sed should help clarify. Commented May 10, 2017 at 19:46

4 Answers 4

4
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.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work too

grep "^tests" ${config_file} | sed -e "s/^tests= //"

Comments

0

How about grep and cut?

all_selected_specs=$(grep "^tests=" "$config_file" | cut -d= -f2-)

Comments

0

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...).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.