awk function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk function
# 1  
Old 07-29-2015
awk function

hi,

I have used awk command to delimit my variable. But this that not worked.
Could you please let me know what need to be changed in my awk command

Input:
Code:
home/unix>cat test.txt
DAILY.JOB CHENNAI,8388
DAILY.JOB BANGLORE,3848
DAILY.JOB TRICHY,9489
DAILY.JOB TIRUPUR,8409

code

Code:
while read word
do
location_id=`awk -F '[ ,]' '{print $2}' $word`
sales=``awk -F '[ ,]' '{print $3}'$word`
  case $location_id in
    chennai|banglore)
      function_value_1($location_id,$sales) ;;
      function_value_1 put_area_loc_value ;;
    salem|trichy||kovai)
      function_record_delete ;;
      function_value_1($location,$sales) ;;
    tirupur)
      function_record_delete ;;
      function_value_1($location,$sales) ;;
    *)
      echo "location is out of the range $location_id" ;;
  esac

Output Expected:

The below input variable need to be passed as argument in the loop.
Location: ->Banglore
Saales -> 3848

---------- Post updated at 11:09 PM ---------- Previous update was at 10:46 PM ----------

its works if i am using cut command. Not sure how to do in awk command

Code:
while read word
do
sales=echo "$word" | cut -d',' -f2
lot=echo "$word" | cut -d' ' -f2
locaton_id=echo "$lot" | cut -d',' -f1

  case $location_id in
    chennai|banglore)
      function_value_1($location_id,$sales) ;;
      function_value_1 put_area_loc_value ;;
    salem|trichy||kovai)
      function_record_delete ;;
      function_value_1($location,$sales) ;;
    tirupur)
      function_record_delete ;;
      function_value_1($location,$sales) ;;
    *)
      echo "location is out of the range $location_id" ;;
  esac

# 2  
Old 07-29-2015
In this case you do not need awk to extract the information.
i.e.
Code:
#!/bin/bash

while IFS='[, ]' read job location sales; do
    echo $location
    echo $sales
done < test.txt


Quote:
while read word
do
location_id=`awk -F '[ ,]' '{print $2}' $word`
sales=``awk -F '[ ,]' '{print $3}'$word`
case $location_id in
chennai|banglore)
function_value_1($location_id,$sales) ;;
function_value_1 put_area_loc_value ;;
salem|trichy||kovai)
function_record_delete ;;
function_value_1($location,$sales) ;;
tirupur)
function_record_delete ;;
function_value_1($location,$sales) ;;
*)
echo "location is out of the range $location_id" ;;
esac
Somethings in red.
Case matters:
chennai is not the same that CHENNAI
banglore is not the same that BANGLORE
etc

Mismatch:
Extra `
Extra |
$location vs $location_id

Functions:
A function in the shell is called as function_value_1 $location $sales. Not function_value_1($location,$sales)

Last edited by Aia; 07-29-2015 at 01:50 AM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 07-29-2015
thanks for your correction.

In my code this condition is not working. not sure my syntax is wrong. any inputs please.

Syntax chek :

Code:
if [[ $loc_count > 1]]; then

shell script code

Code:
cat shell.sh
#!/bin/sh
while IFS='[,]' read locationinp sales; do
location_id=`echo $locationinp | cut -d' ' -f2`
sqlplus -S /NOLOG << EOF
CONNECT xxxx/password@xxxxdbt
set echo off
set heading off
set feedback off
set pagesize 0
CONNECT xxxx/password@xxxxdbt
SET head off
select count(*) into :loc_count from location_count where location_id='$location_id' and sales=$sales;

if [[ $loc_count > 1]]; then

delete from location_count where location_id='$location_id' and sales=$sales;

exit

EOF

done < queryout.txt

# 4  
Old 07-29-2015
Quote:
thanks for your correction.

In my code this condition is not working. not sure my syntax is wrong. any inputs please.

Syntax chek :


cat shell.sh
#!/bin/sh
while IFS='[,]' read locationinp sales; do
location_id=`echo $locationinp | cut -d' ' -f2`
sqlplus -S /NOLOG << EOF
CONNECT xxxx/password@xxxxdbt
set echo off
set heading off
set feedback off
set pagesize 0
CONNECT xxxx/password@xxxxdbt
SET head off
select count(*) into :loc_count from location_count where location_id='$location_id' and sales=$sales;
if [[ $loc_count > 1]]; then
delete from location_count where location_id='$location_id' and sales=$sales;
exit
EOF

done < queryout.txt
Hello Arun,

I think you need to close the if condition properly as follows.
Code:
 if [[ $loc_count > 1]]; then
 delete from location_count where location_id='$location_id' and sales=$sales;
 exit
 fi

Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-29-2015 at 06:00 AM.. Reason: Added quote section now
# 5  
Old 07-29-2015
I'm not sure sqlplus has an if command.
# 6  
Old 07-29-2015
Yes, agree with RudiC.

Hello Arun,

Please take sqlplus's output into a file. Then you can get value from there into a variable then you can easily use if condition to check it's value and execute the next appropriate query. This all can be done within a single script. Kindly try the same and let us know what issue you face or successful outputs.


Thanks,
R. Singh

Last edited by RavinderSingh13; 07-29-2015 at 07:11 AM..
# 7  
Old 07-29-2015
hi gurus,

If satement is getting failed eventhough 1=1 becomes false due to the file format. is there any make is correct. please let me out
Code:
home/unix>cat bit.txt

                                             1


Output :
+ + cat bit.txt
out=
                                             1
+ [[
                                             1                                   = 1 ]]
+ echo failire

code

out=`cat chkbit.txt`
if [[ $out = 1 ]];
then
echo "sucess"
else
echo "failure


sqlplus -S xxx/yy@db <<EOF
        SET head off
        SET serveroutput on
        spool bit.txt
        select package.metric_function('$location_id',$sales) from dual;
        spool off
exit
EOF


Last edited by arun888; 07-29-2015 at 05:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

System function in awk

Hello Friends, I have written a script like below, I aimed to move some CDR files (call data record) whose the last field is "1" (NF=1 ) from a spesific directory to a new directory Field Seperator is pipe. If the directory does not exitst i should create it. I will give the script two... (5 Replies)
Discussion started by: EAGL€
5 Replies

3. Shell Programming and Scripting

awk function

Hi all, I need to have informations in a URL : https://www.autolib.eu/stations/ Valors I need are in bold: {"charging_status": "nonexistent", "rental_status": "future", "subscription_status": "nonexistent", "station_id": 791, "address": "10 rue de Rome, 93110 Rosny-sous-Bois", "lat":... (3 Replies)
Discussion started by: roulitto
3 Replies

4. Shell Programming and Scripting

Awk-using group function

Hi, I have file with below format and sample data - File is pipe delimited Col1|col2|Account|Bal1|Bal2 1|2|1|10|5 1|2|2|10|2 1|3|3|10|3 I want output as SUM|1|2|2|20|7 SUM|1|3|1|10|3 Can anyone give me awk command (4 Replies)
Discussion started by: sanranad
4 Replies

5. Shell Programming and Scripting

AWK Function syntax

Hi, I would like to know what is the correct syntax to perform a function in awk. Although I have seen several examples, not get it to work, this is what I'm trying: #!/bin/bash awk function multi (number) { return number * 3 } print multi (4)Thanks (2 Replies)
Discussion started by: Godie
2 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

MODE function in awk

Hello, Can someone pls help me with some statistical calculation in awk In excel there is a statistical function called "Mode". How Mode works: MODE returns the most frequently occurring, or repetitive, value in array or range. Eg if we have 5 numbers in 5 different columns... (12 Replies)
Discussion started by: Needhelp2
12 Replies

8. Shell Programming and Scripting

external function in awk

Hi all, I have a basic doubt. Is there any way to use external functions (i.e. functions not defined in AWK), in AWK. I have a shell script in which I'm using a AWK snippet. In this snippet I'm calling a function defined in the shell script. But the AWK snippet is not working. I figured that... (5 Replies)
Discussion started by: kamel.seg
5 Replies

9. Shell Programming and Scripting

Help with the function awk

Hi I am trying to create a modify a txt file via a sh script and I'm not sure how to do it. I have this: data1a#data2a#data3aµ data1b#data2b#data3bµ data1c#data2c#data3cµ and I want to have this (more or less) data1a data2a data3a data1b data2b data3b data1c data2c data3c I know... (5 Replies)
Discussion started by: Morgwen
5 Replies

10. Shell Programming and Scripting

awk with function ?? please, help :(

Here is my test.in file Case Modify 10001 20002 30003 40004|Report Create 3417176211|Case Modify 10002 20002 30003 40004| Script: Remove.ksh This script to remove $1 which I type in: $ cat test.in Case Modify 10001 20002 30003 40004|Report Create 3417176211|Case Modify 10002 20002 30003... (0 Replies)
Discussion started by: sabercats
0 Replies
Login or Register to Ask a Question