question about awk

Hello,

I have a file in variable $Dbprp which content :


hostname1_1511_db1.world
hostname2_1512_db2.world
hostname3_1513_db3.world
hostname4_1514_db4.world
hostname5_1515_db5.world

For each line, I search to get three following variables
host = hostname1
port = 1511
base = db1

I try this, but the result for base is hostname1_1511_db1.world. How can I resolve?

for db in `cat $Dbprp`
do
       
        [ ! $db ] && db="error listener.ora" # pb get db on listener.ora files
        echo $db

        host=`echo $db | awk -F'_' '{ print $1 }'`
      port=`echo $db | awk -F'_' '{ print $2 }'`
        base=`echo $db | awk -F'_' '{ print $3 }'| sed 's/.world//g'`

done

Thanks

Regards
bibi92Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
That is not the result I get.
How did you determine that the result for base is hostname1_1511_db1.world?
0
bibi92Author Commented:
Hello,

With this command :
echo "insert into test values (''' || $host || '''' || ',' || '''' || $port || '''' || ',' || '''' || $base|| '''' ||',null,trunc(sysdate));" >> $WorkFile

Thanks
0
ozoCommented:
When I do
        host=`echo $db | awk -F'_' '{ print $1 }'`
      port=`echo $db | awk -F'_' '{ print $2 }'`
        base=`echo $db | awk -F'_' '{ print $3 }'| sed 's/.world//g'`  ## simpler to use base=`echo $db | awk -F'[_.]' '{ print $3 }'`
echo "insert into test values (''' || $host || '''' || ',' || '''' || $port || '''' || ',' || '''' || $base|| '''' ||',null,trunc(sysdate));" >> $WorkFile

Open in new window

I get
insert into test values (''' || hostname1 || '''' || ',' || '''' || 1511 || '''' || ',' || '''' || db1|| '''' ||',null,trunc(sysdate));
insert into test values (''' || hostname2 || '''' || ',' || '''' || 1512 || '''' || ',' || '''' || db2|| '''' ||',null,trunc(sysdate));
insert into test values (''' || hostname3 || '''' || ',' || '''' || 1513 || '''' || ',' || '''' || db3|| '''' ||',null,trunc(sysdate));
insert into test values (''' || hostname4 || '''' || ',' || '''' || 1514 || '''' || ',' || '''' || db4|| '''' ||',null,trunc(sysdate));
insert into test values (''' || hostname5 || '''' || ',' || '''' || 1515 || '''' || ',' || '''' || db5|| '''' ||',null,trunc(sysdate));

an even simpler loop could be
IFS+=_.
while read  host port base world ; do
  echo "insert into test values (''' || $host || '''' || ',' || '''' || $port || '''' || ',' || '''' || $base|| '''' ||',null,trunc(sysdate));" >> $WorkFile
done < $Dbprp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

bibi92Author Commented:
Sorry but we haven't the same result and for the simple loop :
IFS+=_.:  not found

Thanks
0
ozoCommented:
What do you get in $WorkFile?
Which shell are you using?
0
bibi92Author Commented:
I've requested that this question be deleted for the following reason:

I have found a solution with delete || in insert statement thanks regards
0
ozoCommented:
What is the solution with delete || in insert statement?
What do you get in $WorkFile?
Which shell are you using?
0
bibi92Author Commented:
You are right. I have forgotten I changed the output for insert in Oracle db. Thanks regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.