Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

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

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.
Avatar of ozo
ozo
Flag of United States of America image

#!/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
Avatar of enthuguy

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much!!