how to search a string in a variable and get part of the variable in bash

enthuguy
enthuguy used Ask the Experts™
on
HI Friends,

1. below is the sample input.txt, which I loop thru line by line using while / do loop
2. how to search a string in each line and get part of the variable and concat it

Would like to search for "asset", "description", "arg1", "arg2", "arg3", "arg4", "arg5" and get the value, then concat with name/value pair

e.g final output what I would like to have in a variable
var='asset="task1"&description="description of task1"&arg1="some arg1"&arg2="some arg2"&arg3="some arg3"&arg4="some arg4"&arg5="some arg5"'


input.txt
job_1_name=job1
job_1_asset="task1"
job_1_dependent_on="none"
job_1_status="not started"
job_1_description="description of task1"
job_1_arg1="some arg1"
job_1_arg2="some arg2
job_1_arg3="some arg3"
job_1_arg4=none
job_1_arg5=none


Pls help and thx in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
#!/usr/bash
while read line ; do eval ${line%=*}="'"${line#*=}"'" ; done < input.txt
var="asset=$job_1_asset&description=$job_1_description&arg1=$job_1_arg1&arg2=$job_1_arg2&arg3=$arg3=&arg4=$job_1_arg4&arg5=$job_1_arg5"
echo $var

Author

Commented:
Thanks Ozo, your soluction works fine. but more advance for me :)

It is possible to have $job_1_xxxxx to be generic please. reason next input file will have

job_2_name=job2
job_2_asset="task2"
job_2_dependent_on="none"
job_2_status="not started"
job_2_description="description of task2"
job_2_arg1="some arg1"
job_2_arg2="some arg2
job_2_arg3="some arg3"
job_2_arg4=none
job_2_arg5=none
Most Valuable Expert 2014
Top Expert 2015
Commented:
#!/usr/bash
ifs=$IFS
IFS='='
while read a b ; do eval ${a##*_}\=\'${b}\' ; done < input.txt
IFS=$ifs
var="asset=$asset&description=$description&arg1=$arg1&arg2=$arg2&arg3=$arg3&arg4=$arg4&arg5=$arg5"
echo $var

Author

Commented:
Thanks very much!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial