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
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.
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...
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.
Old:
name2=$COL2
echo $name2 | sed "s|_| |g" - > variable
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 $(...) .