HELP - NEED Small Script ( or single awk Command)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers HELP - NEED Small Script ( or single awk Command)
# 1  
Old 09-14-2015
HELP - NEED Small Script ( or single awk Command)

Hi All

REQUIREMENT:-
==========

There is one folder named /data/ds/dpr_ukdw_sys/working/unixfile

In that folders there are files are like below
Code:
CODS_ACTMZ_TRANS_ALL_20150911.TXT 
CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT 
CODS_ACTMZ_REF_NHA_ALL_20150911.TXT 
CODS_ACTMZ_REF_NHP_ALL_20150911.TXT 
CODS_ACTMZ_REF_CTR_BR_MIL_20150911.TXT 
CODS_ACTMZ_REF_CTR_IN_MIL_20150911.TXT 
CODS_ACTMZ_REF_CTR_PE_MIL_20150911.TXT 
CODS_ACTMZ_REF_CTR_RE_MIL_20150911.TXT

Where (20150911) is order date , so when the above files are created, I need to run a Unix script on top of it to display the following

Please note , Oder date should be one of the parameter for the script

OUTPUT REQUIRED :-
==============
Code:
CODS_ACTMZ_TRANS_ALL_20150911.TXT <space> 1213KB(size of the file) <space> TRANSACTIONS ( since it is TRANS_ALL is the keyword)

CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT <space> 1213KB(size of the file) <space> CTR_ACCOUNT ( since it is CTR_ACT is the keyword)

CODS_ACTMZ_REF_NHA_ALL_20150911.TXT <space> 1213KB(size of the file) <space> ACCOUNT ( since it is NHA_ALL file)

TRANS_ALL==> TRANSACTIONS
CTR_ACT==> CTR_ACCOUNT
Above are the Hardcode values which I need to check and display

Please HELP , new to UNIX !!
Thanks a lot in advance

Last edited by rbatte1; 09-15-2015 at 07:16 AM..
# 2  
Old 09-14-2015
Hello Nagarjuna4347,

Welcome to forums, please use code tags as per forum rules while using Inputs/codes/commands in your posts as per forum rules. Now coming on to your requirement, as you have shown us only TRANS_ALL, CTR_ACT, REF_NHA" for TRANSACTIONS, CTR_ACCOUNT, ACCOUNT respectively. Following code may help you in same. If you have more conditions then please provide us your same input with expected sample output too with code tags.
Code:
ls -lhtr CODS_ACTMZ* | awk -vs1="TRANS_ALL" -vs2="CTR_ACT" -vs3="REF_NHA" '{if($NF ~ s1){print $NF OFS $5 OFS "TRANSACTIONS"};if($NF ~ s2){print $NF OFS $5 OFS "CTR_ACCOUNT"};if($NF ~ s3){print $NF OFS $5 OFS "ACCOUNT"}}'

In my case I have created some test files so following is the output.
Code:
CODS_ACTMZ_REF_NHA_ALL_20150911.TXT 0 ACCOUNT
CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT 146 CTR_ACCOUNT
CODS_ACTMZ_TRANS_ALL_20150911.TXT 515 TRANSACTIONS

Thanks,
R. Singh
# 3  
Old 09-14-2015
Thanks a lot Ravinder

One more thing to mention , apologies I was not clear earlier about my requirement

there is a date on the file , if you notice CODS_ACTMZ*20150911 , which need to be variable mate
Directory will have files of many dates

Basically , small script requirement when passed date as parameter should give following result... Can you please help me with small script? and then below result should be writting into a text file as output

Code:
CODS_ACTMZ_REF_NHA_ALL_<date i am passing>.TXT <size> <date i am passing again> <Hardcode values based on file like Account/Transacions>

# 4  
Old 09-15-2015
Hello Nagarjuna4347,

No need to apologies, you can hit thank you button for any helpful post by any user in this forum to thank anyone. If I understood your requirement correctly then following may help you in same.
Code:
cat check_dates_files.ksh
VAL=$1
if [[ $VAL == "" ]]
then
        echo "No variable DATE passed, so exiting the script. FORMAT of variable should be YYYYMMDD form."
        exit;
fi
 
ls -lhtr CODS_ACTMZ_*${VAL}*TXT | awk -vs1="TRANS_ALL" -vs2="CTR_ACT" -vs3="REF_NHA" '{if($NF ~ s1){print $NF OFS $5 OFS "TRANSACTIONS"};if($NF ~ s2){print $NF OFS $5 OFS "CTR_ACCOUNT"};if($NF ~ s3){print $NF OFS $5 OFS "ACCOUNT"}}'

Above script name is check_dates_files.ksh give proper permissions to it and run it, in my case I have created some test files as follows.
Code:
-rw-r--r-- 1 Singh1 Singh1    0 Sep 14 06:45 CODS_ACTMZ_REF_NHP_ALL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 14 06:45 CODS_ACTMZ_REF_NHA_ALL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 14 06:45 CODS_ACTMZ_REF_CTR_RE_MIL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 14 06:45 CODS_ACTMZ_REF_CTR_PE_MIL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 14 06:45 CODS_ACTMZ_REF_CTR_IN_MIL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 14 06:45 CODS_ACTMZ_REF_CTR_BR_MIL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1  146 Sep 14 12:49 CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1  515 Sep 14 12:51 CODS_ACTMZ_TRANS_ALL_20150911.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 15 02:14 CODS_ACTMZ_REF_CTR_RE_MIL_20150912.TXT
-rw-r--r-- 1 Singh1 Singh1    0 Sep 15 02:14 CODS_ACTMZ_REF_CTR_PE_MIL_20150912.TXT

Then after running script we will get following.
Code:
./check_dates_files.ksh 20150911
CODS_ACTMZ_REF_NHA_ALL_20150911.TXT 0 ACCOUNT
CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT 146 CTR_ACCOUNT
CODS_ACTMZ_TRANS_ALL_20150911.TXT 515 TRANSACTIONS

Also when not passing any variables then following output will come to user.
Code:
 ./check_dates_files.ksh
No variable DATE passed, so exiting the script. FORMAT of variable should be YYYYMMDD form.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-16-2015
Thanks a lot Ravinder :-) , following is the final script which I have prepared to get output

Code:
VAL=$1
if [[ $VAL == "" ]]
then
echo "No variable DATE passed, so exiting the script. FORMAT of variable should be YYYYMMDD form."
exit;
fi
ls -ltr ACTMZ_*${VAL}*txt | awk -vs1="TRANS_ALL" -vs2="CTR_ACT" -vs3="REF_NHA" -vs4="REF_NHP" -vs5="REF_CTR_BR" -vs6="REF_CTR_IN" -vs7="REF_CTR_PE" -vs8="REF_CTR_RE" '{if($NF ~ s1){print $NF OFS $5 OFS "TRANSACTIONS"};if($NF ~ s2){print $NF OFS $5 OFS "CTR_ACCOUNT"};if($NF ~ s3){print $NF OFS $5 OFS "ACCOUNT"};if($NF ~ s4){print $NF OFS $5 OFS "PARTY"};if($NF ~ s5){print $NF OFS $5 OFS "CTR_BRANCH"};if($NF ~ s6){print $NF OFS $5 OFS "CTR_INSTRUMENT"};if($NF ~ s7){print $NF OFS $5 OFS "CTR_PERSON"};if($NF ~ s8){print $NF OFS $5 OFS "CTR_RECORD"}}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Require single command to start script in multiple servers

I have 9 servers, on each server a script with common name is available. I send a token file to all server from 1 particular server. so when a daemon job checks that token file is available then it triggers the script.. I want to know is there any command or script which I will run/execute on... (16 Replies)
Discussion started by: mirwasim
16 Replies

2. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

3. Shell Programming and Scripting

small sed script on command line.

Can anyone help me get this small sed script to work in shell on the command line? I need it in a one liner really as i want to edit many scripts in a for loop and dont want to have to invoke a separate script each time. #!/bin/sh sed '/mailx\ -s.*$ { i\ #Comment above mailx line ... (5 Replies)
Discussion started by: lavascript
5 Replies

4. UNIX for Dummies Questions & Answers

single output of awk script processing multiple files

Helllo UNIX Forum :) Since I am posting on this board, yes, I am new to UNIX! I read a copy of "UNIX made easy" from 1990, which felt like a making a "computer-science time jump" backwards ;) So, basically I have some sort of understanding what the basic concept is. Problem Description:... (6 Replies)
Discussion started by: Kasimir
6 Replies

5. Shell Programming and Scripting

How to alias an awk command with single and double quotes?

Hi, I am trying to write the following command as an alias in my .bashrc file. bjobs -u all | awk '{if (NR > 1) {username++;}}END{{print"\nJOBS BY USER:\n"} for (i in username) {print username,i;}{print"\n Total Jobs=",NR-1,"\n" }}' The command simply puts how many jobs each user is... (2 Replies)
Discussion started by: jacekmaciek
2 Replies

6. Shell Programming and Scripting

How can i use single quotes for SQL command in shell script

Hi. please help me to write the following query in a shell script. the Query is :select no,salary from emp_info where name='$var_name' the following is my code. #! /bin/sh var_name=$1 sqlplus -s user/pwd@DB << EOF select no,salary from emp_info where name="'$var_name'";... (4 Replies)
Discussion started by: little_wonder
4 Replies

7. Shell Programming and Scripting

awk, perl Script for processing a single line text file

I need a script to process a huge single line text file: The sample of the text is: "forward_inline_item": "Inline", "options_region_Australia": "Australia", "server_event_err_msg": "There was an error attempting to save", "Token": "Yes", "family": "Family","pwd_login_tab": "Enter Your... (1 Reply)
Discussion started by: hmsadiq
1 Replies

8. Shell Programming and Scripting

Multiple pipes toward a single awk command

Hello, I would like to pipe two variables into awk, but I don't know how to do. Each variable, "a" and "b", are in fact a list of data. They are not files. So to get awk to work with it I am using: echo $a | awk 'FNR==NR{print $1}FNR!=NR{print $4}' The above works, but when I am... (5 Replies)
Discussion started by: jolecanard
5 Replies

9. Shell Programming and Scripting

Help with awk script, changing the FS for a single variable

Hi all, im new to awk and would apreciate if you could tell me how to do this, i have a file with several entries like this: 2008-09-09 21:57:45 44 403 CUSTOM_EVENT Upgrade - end1 2008-09-09 21:57:46 45 403 CUSTOM_EVENT Component Check - start... (3 Replies)
Discussion started by: sx3v1l_1n51de
3 Replies

10. Shell Programming and Scripting

Please help to debug a small shell script (maybe AWK problem)?

Hi Buddies, The following is shell scripts which was borrowed from linux box for load average check. it runs good. (this structure is simple, when load average is too high, it will send alert to user) #!/usr/bin/ksh # Set threshold for 1, 5 and 15 minture load avarage # configured for... (4 Replies)
Discussion started by: GreatJerry
4 Replies
Login or Register to Ask a Question