Help in extracting fields from a file

I have an input file with contents like:

203969 OrdAcctCycChg           USAGE_DAEMON1   
203970 OrdAcctCycChg           USAGE_DAEMON2   
203971 OrdAcctCycChg           USAGE_DAEMON3   
203972 OrdAcctCycChg           USAGE_DAEMON4   

I need to extract variables in first column ('203969','203970','203971'...) and numerical digit of the third column ('1','2','3','4' etc..) and store those variables in $1 and $2 variables inside a loop for every record.

Can any one suggest me the way to do it
:confused:

 
nawk '{print $1,substr($3,length($3),1)}' inputfile | while read a b
do
echo $a #your first variable value
echo $b #your second variable value
done
1 Like

Thanks for the quick reply...
It almost worked but may be little change needed here..
The input file have more than 3columns here..
Let me show you an example record:

203969 OrdAcctCycChg                            USAGE_DAEMON1                       
20101001 07091693 BILLING_CYCLE_CHANGE                              2 SKORUKON                      
20101001 07092076 ACCT_NBR|0030063224|prev_CYCLE_CD|VA|new_CYCLE_CD|27|ACCT_CYCLE_EFF_DT|20101001|  
07092076 20101001                           P                              

the above one only a single record from the input file:

when I ran the code you have given..it is displaying the output as follows:

203969
1
20101001
E
20101001
|
07092076
P

I only need first two fields (in bold),skip all other data.
can it be modified to do so?

??? not clear

 
awk '/OrdAcctCycChg/ {print $1,substr($3,length($3),1)}' inputfile | while read a b
do
echo $a #your first variable value
echo $b #your second variable value
done
1 Like

ThankYou...It worked for me:)

can it be independent of the field string 'OrdAcctCycChg',the because the file contents may vary and not always the field will be OrdAcctCycChg.
Can you please help me in this case:(

Something like this?

awk '{if($2 ~ "^[a-zA-Z]"){print $1,substr($3,length($3),1)}}' input_file

--ahamed

Yes Ahamed...I'll try this once

Please note I have made a change in my previous post!

awk '{if($2 ~ "^[a-zA-Z]"){print $1,substr($3,length($3),1)}}' input_file

--ahamed

Hi Ahamed..
I thnk there is some syntax problem with this..It is throwing syntax error while running:

awk ' { if ( $2 ~ "[^a-zA-Z]" ) {print $1,substr($3,length($3),1)}}' ./output.txt | while read a b
do
echo $a
echo $b 
done

output is :

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

which is your OS? If solaris, please use nawk
Also, please use the updated code!

--ahamed

1 Like

Yeah it worked...Thankyou verymuch for the quick response:)
I have one more doubt..
Iam running a sql select command inside shell script and redirecting its output to a file.
is there any way to wait until the query completes (like wait command) its processing and redirecting the output to the file.
Because the code is trying to access the file before the query completion and am getting ambigous errors because of it>
Am pasting here the code for your reference.
Please provide me a way to get rid of this problem

sqlplus -s /nolog |&
print -p "connect $db_user/$db_pwd@$db_sid;"
print -p "SPOOL output.txt;"
while read var_ack_party_name
do
print -p "set pagesize 0 feedback off verify off heading off echo off linesize 100 \n"
print -p "select *  from bus_event where ack_party_name like 'MOVE_USAGE_DAEMON%' and event_data_text like '%"$var_ack_party_name"%';"
done <./$1.txt

nawk '{if($2 ~ "^[a-zA-Z]"){print $1,substr($3,length($3),1)}}'  ./output.txt | while read a b

Please help me to understand with some examples of input files or anything??

here the input file ($1.txt)contents are like:

0030063224
0030063278
0030063234

The select command takes each record from the above file and run the query and redirects the output to output.txt file.

The query output is redirected to file:output.txt
its contents are like
203969 OrdAcctCycChg DAEMON1
203456 OrdAcctCycChg DAEMON1

Now what you have to do??

the code am using is :

sqlplus -s /nolog |&
print -p "connect $db_user/$db_pwd@$db_sid;"
print -p "SPOOL output.txt;"
while read var_ack_party_name
do
print -p "set pagesize 0 feedback off verify off heading off echo off linesize 100 \n"
print -p "select *  from bus_event where ack_party_name like 'MOVE_USAGE_DAEMON%' and event_data_text like '%"$var_ack_party_name"%';"
done <./$1.txt

nawk '{if($2 ~ "^[a-zA-Z]"){print $1,substr($3,length($3),1)}}'  ./output.txt | while read a b

the output.txt file contains the query output.
Before the query completion,the code is trying to access output.txt file and am getting cannot open ./output.txt file error.
Can we modify the code to wait for the query execution to get complete and output.txt file created here?

use sleep command to stop executing the script for a while so that output.txtwill be available to access.

done <./$1.txt
sleep 180 && echo " Waiting for the query execution..."
nawk '{if($2 ~ "^[a-zA-Z]"){print $1,substr($3,length($3),1)}}'  ./output.txt | while read a b

I have used it but it is not working.
even it is not printing the message in echo.:wall:

Hi Rajesh,
1.Please try to give one filename instead of ./$1.txt
2.Please find out how long the query execution process takes time so that we need to set the Sleep time command.
3.Suspend the script using sleep for a while then execute the nawk or awk.