Looping the files for getting the file size information


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping the files for getting the file size information
# 1  
Old 07-30-2018
Looping the files for getting the file size information

Am using Linux 3.10

years.txt is input source file which has the list of year like below
Code:
2013
2014
2015
2016

Other Input files are

Code:
XX_TEST1_YR2016_01012018.csv
XX_TEST1_YR2015_01012018.csv
XX_TEST1_YR2014_01012018.csv
XX_TEST1_YR2013_01012018.csv

XX_TEST2_YR2016_01012018.csv
XX_TEST2_YR2015_01012018.csv
XX_TEST2_YR2014_01012018.csv
XX_TEST2_YR2013_01012018.csv


XX_TEST3_YR2016_01012018.csv
XX_TEST3_YR2015_01012018.csv
XX_TEST3_YR2014_01012018.csv
XX_TEST3_YR2013_01012018.csv

So right now the below code is giving the output for only Tes1 and does not loop Test2 and Test3.

Code:
YR13_01012018.ZIP,2013 Test1,65,8
YR14_01012018.ZIP,2014 Test1,112,96K
YR15_01012018.ZIP,2015 Test1,100,5.5K

Please let me know how to make it work for Test2 and Test3 files also.

#####################code###############

Code:
DT=01012018

while read YEAR; do
for i in XX_*YR${YEAR}_${DT}.csv; do
if [ ${YEAR} -lt 2016 -a "$i" = "XX_TEST1_YR${YEAR}_${DT}.csv" ];
then 
    printf "YR`echo ${YEAR} | cut -c3,4`_${DT}.ZIP,${YR} Test1,65,`ls -lh $i | awk '{ print $5 }'`\n"  >> log.txt
if [ ${YEAR} -lt 2016 -a "$i" = "XX_TEST2_YR${YEAR}_${DT}.csv" ];
then 
    printf "YR`echo ${YR} | cut -c3,4`_${DT}.ZIP,${YR} Test2,112,`ls -lh $i | awk '{ print $5 }'`\n"  >> log.txt
if [ ${YEAR} -lt 2016 -a "$i" = "XX_TEST3_YR${YEAR}_${DT}.csv" ];
then 
    printf "YR`echo ${YR} | cut -c3,4`_${DT}.ZIP,${YR} Test3,100,`ls -lh $i | awk '{ print $5 }'`\n"  >> log.txt
fi
fi
fi
   done
done < years.txt

#############################################

Last edited by vgersh99; 07-30-2018 at 11:47 AM.. Reason: code tags, please!
# 2  
Old 07-30-2018
Your if constructs need to be rearranged, as if the first if conditions is true, the others in the then branch won't come true any more. Try



Code:
while read YEAR
  do for i in XX_*YR${YEAR}_${DT}.csv
    do  if [ ${YEAR} -lt 2016 -a "$i" = "XX_TEST1_YR${YEAR}_${DT}.csv" ]
          then  printf "YR`echo ${YEAR} | cut -c3,4`_${DT}.ZIP,${YR} Test1,65,`ls -lh $i | awk '{ print $5 }'`\n"
        fi
        if [ ${YEAR} -lt 2016 -a "$i" = "XX_TEST2_YR${YEAR}_${DT}.csv" ]
          then  printf "YR`echo ${YR} | cut -c3,4`_${DT}.ZIP,${YR} Test2,112,`ls -lh $i | awk '{ print $5 }'`\n"
        fi
        if [ ${YEAR} -lt 2016 -a "$i" = "XX_TEST3_YR${YEAR}_${DT}.csv" ]
          then  printf "YR`echo ${YR} | cut -c3,4`_${DT}.ZIP,${YR} Test3,100,`ls -lh $i | awk '{ print $5 }'`\n"
        fi
      done
   done < file
YR13_01012018.ZIP, Test1,65,0
YR_01012018.ZIP, Test2,112,0
YR_01012018.ZIP, Test3,100,0
YR14_01012018.ZIP, Test1,65,0
YR_01012018.ZIP, Test2,112,0
YR_01012018.ZIP, Test3,100,0
YR15_01012018.ZIP, Test1,65,0
YR_01012018.ZIP, Test2,112,0
YR_01012018.ZIP, Test3,100,0

Your output might need some polishing, too, as it seems.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-31-2018
Thanks a lot RudiC. It worked and appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get information from one files, based on data from other file

Hello. I am trying to get some info from log file. I have fileA , which contains all the country prefixes (the file contains one column and "n" rows ). And i have fileB, which contains huge data of phone numbers (the file contains one column and "n" rows). What i want to do is, to count... (7 Replies)
Discussion started by: dragonfly85
7 Replies

2. Shell Programming and Scripting

Creating a Third File from Information Contained in Two Files

In advance, I appreciate any help or tips. I apologize for not providing examples of scripts I have already tried, the reason is that I am new to programming and do not really no where to start. I think this is possible with awk, but do not know how to go about learning how to write the script... (2 Replies)
Discussion started by: awc228
2 Replies

3. Shell Programming and Scripting

While or For with looping contructs? to create files from contents of a text file

Hi so far I created this script: vi loop.beta.sh for i in `cat extract.filenames.tabc` do echo $i done>$i === This is the original text file. $ more tabc.txt -rwx------- 1 alice staff 1586 2010-11-05 02:27 request-key .conf -rwx------- 1 ted staff 126 ... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

4. UNIX for Dummies Questions & Answers

looping through file and creating seperate files

Hi, My first post!! I have a files with header, something like this Header_Row AMC|D1|D2|D2 AAO|D3|D4|D5 AMC|D6|D7|D8 AAO|D9|D10|D11 . . . . . and millions fo records thereafter like this. I want to read the above file in a loop and write the lines having AMC into another... (1 Reply)
Discussion started by: amitbakre
1 Replies

5. Homework & Coursework Questions

File Looping - Looking for executable files - Need help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Hello all, I have posted here before and really was blown away by the amount of assistance I received. I was able to finish my homework without a problem! But, yet again, our... (6 Replies)
Discussion started by: Bob07
6 Replies

6. Shell Programming and Scripting

Merging information from multiple files to a single file

Hello, I am new to unix and need help with a problem. I have 2 files each containing multiple columns of information ie; File 1 : A B C D E 1 2 3 4 5 File 2 : F G 6 7 I would like to merge the information from File 2 to File 1 so that the data reads as follows; File 1: A... (4 Replies)
Discussion started by: crunchie
4 Replies

7. UNIX for Dummies Questions & Answers

List size information...

hi all, i want to list only the size information and name of the file(s) and directories in current directory .. (2 Replies)
Discussion started by: sonu_pal
2 Replies

8. Shell Programming and Scripting

To remove files with 0 File Size

Dear All, I want to remove files having size of (0) that are generated once script is completed. Here is the code but i m not sure about /usr/bin do i need to write /usr/bin, or the path of my script /tmp/test please find below the script logic #!/usr/bin/ksh for i... (9 Replies)
Discussion started by: jojo123
9 Replies

9. Shell Programming and Scripting

Files with 0 file size and created today

hi, I am building a script to identify those files created/modified today and with file size 0. I am able to find the files with 0 file size and created/modified in last 24 hrs as shown below but not today (current date), I tried using (touch -t time filenm) but in my version of unix at work it... (7 Replies)
Discussion started by: rushs682
7 Replies

10. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies
Login or Register to Ask a Question