Avatar of enthuguy
enthuguy
Flag 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.
Shell ScriptingLinuxUnix OS

Avatar of undefined
Last Comment
enthuguy

8/22/2022 - Mon
ozo

#!/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
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
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
enthuguy

ASKER
Thanks very much!!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes