pattu
1
Hi,
I would like to know how to search for a pattern in a file and assign it to a variable using sed.
For example, my hope.txt
address1=123 street,abc city,xyz
address2=456 street,abc city,xyz
In my script I would like to search for patter "address1" and assign the complete line into a variable "A1". How do I do that?
Thanks,
Use the right tool, which in your case is awk, not sed.
pattu
3
Thanks, How do I do that using awk?
danmero
4
Why sed only, is this a homework ? .. anyway
var=$(sed '/address1/s/.*=\(.*\)/\1/p;d' file)
---------- Post updated at 07:43 PM ---------- Previous update was at 07:34 PM ----------
hmm... I didn't seen that you are open for other solution.
Shell only:
pattern=address1
var=""
while IFS=\= read pat adr
do
case $pat in
$pattern ) var=$adr;break;;
esac
done< file
echo $var
or, something like:
pat=address1
addr=$(sed -n "s/^$pat=//p" infile)