How to replace string in variable?

Hello,
I have simple while and for loops in a shell script and I would like to replace some characters in COL2 when I run it. I am on ubuntu 14.04

while read COL1 COL2 COL3 COL4
do
name=$COL2
#cat $name | sed -i "s|_| |g" $name
for i in $COL3 $COL4
do
some codes ...... run $i  b="$name"
done
done<list.txt

list.txt

AA MICHIGAN:_USA_3    http://xx.yy.zz
BB LONDON:_UK_25      http://aa.bb.cc
CC TEXAS:_USA_83         http://11.22.33.44

column2 in list.txt:

MICHIGAN:_USA_3
LONDON:_UK_25
TEXAS:_USA_83

list.txt file is tab seperated and column 2 contains special characters.
As file is common in use with some other scripts, to change the original list.txt file is not a good idea.
How may I change variable in such a case?

Thanks
Boris

--- Post updated at 10:07 AM ---

Hello,
Sorted out now.

while read COL1 COL2 COL3 COL4
do
name2=$COL2
echo $name2 | sed "s|_| |g" - > variable
name=$(cat variable)
for i in $COL3 $COL4
do
some codes ...... run $i  b="$name"
done
done<list.txt

Thank you
Boris

Please please please - state clearly what you're after; don't let people guess what that might be. Would the following paraphrase your task:

Are you aware that there are only three columns in your file?

How far would

while read C1 C2 C3 REST; do echo run 1 ${C2//_/ }; done < list.txt
run 1 MICHIGAN: USA 3
run 1 LONDON: UK 25
run 1 TEXAS: USA 83

get you?

1 Like

Hello Rudic,
Thank you for the warning. COL4 is used in "some codes" field and is not visible. I did not want to confuse you with long codes, and trimmed related fields. Thanks for your understanding
Seems it's okay with redirection of variable to another variable. I know it's silly but I couldn't have found a wise method.

name2=$COL2
echo $name2 | sed "s|_| |g" - > variable
name=$(cat variable)

Thank you so much
Boris

So - is that problem solved now? Then, please add the "solved" tag.

1 Like

Already added.

Thank you
Boris

No, definitely not.

Seems weird,
I see "sorted" phrase.

you're not redirection of variable to another variable - you're redirecting to a file called variable which is a bit of a waste given other alternatives...

2 Likes

Yes, "sorted". See the quote from the "Edit tags" part of the thread:

That's what makes a thread turn light blue in the thread lists...

1 Like

Oh my God,
Sorted and solved .
I am sorry Rudic

Kind regards
Boris

Hello Vgersh99,
Thank you for your comment. I am not surprised when I read your post. I am not coder so my codes are like gramophone needle made by horse stirrup. Please be tolerant. :slight_smile:

Old:

name2=$COL2
echo $name2 | sed "s|_| |g" - > variable

New:

name2=`echo $COL2 | sed "s|_| |g" `

Kind regards
Boris

It's a bit surprising that you refuse to learn and prefer

name2=$COL2
echo $name2 | sed "s|_| |g" - > variable
name=$(cat variable)

to

name=${COL2//_/ }

which would use shell internals only and save

  • a FIFO creation
  • two process creations
  • two file operations

EDIT: admittedly, your latest solution name2=`echo $COL2 | sed "s|_| |g" ` is better than the above cited, but still is more resource intensive (still two processes and a FIFO) than the pure shell solution. Please be aware that the backticks method `...` is deprecated and should be replaced by $(...) .

1 Like