Visit Our UNIX and Linux User Community


How to print the missing fields outside the for loop in Korn shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the missing fields outside the for loop in Korn shell?
# 1  
Old 12-28-2015
How to print the missing fields outside the for loop in Korn shell?

I have 2 for loop in my program , first one will list files based on timestamp and second one list the files based on type(RPT / SUB_RPT).Here is my code:
Code:
#!/bin/ksh

STG_DIR=/home/stg

for pattern in `find $STG_DIR -type f -name 'IBC*csv' | awk -F'[-.]' '{print $(NF-1)}' | sort -u`
do
    echo "Time Stamp: $pattern"
    for file_set in `find $STG_DIR -type f -name "IBC*${pattern}*" | awk -F'/' '{print$5}'`
    do
        echo "File Type:$file_set"

        #echo $file
        cd $STG_DIR

        file_part1=`echo $file_set | awk -F'[-.]' '{print $(NF-2)}'`
        file_name=${file_part1}_`date '+%Y%m%d'`_`date '+%H%M%S'`.txt
         #echo "Created file: $file_name"
    done    
    sleep 1    
done    
exit 0

Output for the above script is:
Quote:
Time Stamp: 0201506
File Type:UNAPPLIED_CASH_311_SUB_RPT-0201506.csv
File Type:UNAPPLIED_CASH_311_RPT-0201506.csv
Time Stamp: 0201507
File Type:UNAPPLIED_CASH_311_RPT-0201507.csv
Time Stamp: 1407278
File Type:UNAPPLIED_CASH_311_SUB_RPT-1407278.csv
Can you please guide me on how to print if a type of file is not present for a time stamp. In above case I would like to print UNAPPLIED_CASH_311_SUB_RPT-0201507.csv is not present for 0201507.
# 2  
Old 12-29-2015
Quote:
Originally Posted by ann15
I have 2 for loop in my program , first one will list files based on timestamp and second one list the files based on type(RPT / SUB_RPT).Here is my code:
Code:
#!/bin/ksh

STG_DIR=/home/stg

for pattern in `find $STG_DIR -type f -name 'IBC*csv' | awk -F'[-.]' '{print $(NF-1)}' | sort -u`
do
    echo "Time Stamp: $pattern"
    for file_set in `find $STG_DIR -type f -name "IBC*${pattern}*" | awk -F'/' '{print$5}'`
    do
        echo "File Type:$file_set"

        #echo $file
        cd $STG_DIR

        file_part1=`echo $file_set | awk -F'[-.]' '{print $(NF-2)}'`
        file_name=${file_part1}_`date '+%Y%m%d'`_`date '+%H%M%S'`.txt
         #echo "Created file: $file_name"
    done    
    sleep 1    
done    
exit 0

Output for the above script is:
Code:
ime Stamp: 0201506
File Type:UNAPPLIED_CASH_311_SUB_RPT-0201506.csv
File Type:UNAPPLIED_CASH_311_RPT-0201506.csv
Time Stamp: 0201507
File Type:UNAPPLIED_CASH_311_RPT-0201507.csv
Time Stamp: 1407278
File Type:UNAPPLIED_CASH_311_SUB_RPT-1407278.csv

Can you please guide me on how to print if a type of file is not present for a time stamp. In above case I would like to print UNAPPLIED_CASH_311_SUB_RPT-0201507.csv is not present for 0201507.
Why only report that UNAPPLIED_CASH_311_SUB_RPT-0201507.csv is not present for 0201507? Why not also report that UNAPPLIED_CASH_311_RPT-1407278.csv is missing for 1407278?

And, there is no reason to invoke date twice in the code marked in red above. Invoking date twice is less efficient, runs slower, and can produce anomalous results (giving a date on one day and a time from the next day if the script is run close to midnight). You might want to change:
Code:
        file_name=${file_part1}_`date '+%Y%m%d'`_`date '+%H%M%S'`.txt

to:
Code:
        file_name=$file_part1$(date '+_%Y%m%d_%H%M%S.txt')

But, of course, since nothing is ever done with the variable file_name and nothing is ever done with the variable file_part1 except for its use in creating the variable file_name, maybe it doesn't matter???
# 3  
Old 12-30-2015
On top of what Don Cragun said, how do you know which file types (here: UNAPPLIED_CASH_311_SUB_RPT-0201506.csv and UNAPPLIED_CASH_311_RPT-0201506.csv) to expect? Are they listed in a file? Somewhere/somehow else? Or is (RPT / SUB_RPT) just put in a variable?

Previous Thread | Next Thread
Test Your Knowledge in Computers #381
Difficulty: Medium
Solaris 9 is registered as UNIX 03 compliant on 32-bit and 64-bit x86 (X86-64) and SPARC systems
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to print python array in shell script loop.

I am unable to loop print a python string array in my unix shell script: ~/readarr.sh '{{ myarr }}' more readarr.sh echo "Parameter 1:"$1 MYARRAY= $1 IFS= MYARRAY=`python <<< "print ' '.join($MYARRAY)"` for a in "$MYARRAY"; do echo "Printing Array: $a" done Can you... (10 Replies)
Discussion started by: mohtashims
10 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

Korn Shell Loop Problems

Very new to the Korn Shell, but I've been looking up loops online and it seems this should work. I'm just trying to convert an ip range in variables $A and $B and iterate the individual ip's out to new lines. Unfortunately I get {152..155} instead of 152, 153, 154, and 155. # for i in {$A..$B};... (8 Replies)
Discussion started by: Azrael
8 Replies

5. Shell Programming and Scripting

[Solved] Issue with using for loop as for in {2..6} in korn shell

Hi i have to cut columns 2 to 6 from a file and assign it to arrays , The following code works for ctcol in 2 3 4 5 6; do set -A a$ctcol $(cut -d, -f $ctcol test_file) done how ever this does not work for ctcol in {2..6}; do set -A a$ctcol $(cut -d, -f $ctcol test_file)... (4 Replies)
Discussion started by: 100bees
4 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

8. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

9. Shell Programming and Scripting

Help With Constructing A Korn Shell Search Loop

Hello All, I am a statistician and I am very new to the world of ksh programming. Daily, I analyze millions of rows of data and land information to DB2 tables. I have recently been asked to develop a ksh script to FTP an export file containing line item data from the production environment to the... (2 Replies)
Discussion started by: jonesdk5
2 Replies

10. Shell Programming and Scripting

Korn Shell Loop question

I'm needing help with assigning variables inside a while loop of ksh script. I have an input text file and ksh script below and I'm trying to create a script which will read the input file line by line, assign first and second word to variables and process the variables according to the contents. ... (4 Replies)
Discussion started by: stevefox
4 Replies

Featured Tech Videos