Required help in shell scripting

Iam new to shell scripting,i have a text file in my $home directory mentioned below.

201249_BAN_EXCL.TXT

I need to refer the above file in my automated script but the number 201249 used to change every week.How can i refer to this file everyt time without renaming it manually?

Thanks in advance.

Hi JRoyal,

Could you please explain how you are getting this value. Which code you are running which is giving you this text file name.Can you please tell us about your requirement in detail.

Thanks

This would get you the latest file:

ls *_BAN_EXCL.TXT|tail -1

In a script, you could use it like this:

$(ls *_BAN_EXCL.TXT|tail -1)

Above code will not get the latest file.

for getting the latest file, you can use it like

ls -1rt *.txt | tail -1

In the above code -1rt --> its numeric 1 ( not a letter L )

 
ls -lrt *.txt | awk '{a=$NF}END{print a}'

In the above command i used a lower case L

1 Like

itkamaraj, the command will give you the latest file as each file starts with the date patern YYYYWW.

In addition to what Subbeh and Itkamaraj said you also need to specify this home directory path where this text file exist in your script if you run the script from some other path.

Depending on what your system and date version is,

$ date +%Y%V
201252

will give you the respective file name prefix.

1 Like

You have help me very well,Thanks a lot for your help!

Thanks,
Jroyal