Extract the filename and write to .txt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the filename and write to .txt
# 8  
Old 08-26-2015
Hello Ram_Kumar_BE,

Following may help you in same, you can Thank you button on left of every post to thank any user in this forum Smilie
Code:
for i in /tmp/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
     echo $i
done
/tmp/SASApp_STPServer_2015-08-25_tmptcmsaslva2_67891.log
/tmp/SASApp_STPServer_2015-08-25_tmptcmsaslva2_78967.log

EDIT: Following is the explanation for same.
Code:
for i in /tmp/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"     ######## it is a for loop where i is a variable and 
                                                                                                  ######## date +%Y-%m-%d --date "1 day ago" is LIKE--> date +%Y-%m-%d gives output (2015-08-26) in YYYY-MM-DD format, and complete command date +%Y-%m-%d --date "1 day ago" give 1 day before date
                                                                                                  ######## So i in /tmp/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"  means if any file satisfies the regex then condition is TRUE
do
     echo $i                                                                                      ######## If any results falls under above mentioned then print variable named i's value which is the filename
done

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-26-2015 at 09:16 AM.. Reason: Added explaination for solution as per user's request
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 08-26-2015
Why not - assuming a recent shell - try
Code:
ls -1 SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))* > /tmp/file.txt

# 10  
Old 08-27-2015
Hi Ravinder,

When I ran the script below, I got error as syntax error near unexpected token `do

My script name is list_file.sh and I ran as sh list_file.sh

Code:
#!/bin/sh
for i in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
  echo $i
done

echo $i >> output_file.txt


Also how to get script log as .log file in same location?

Thanks in advance for your advise.

---------- Post updated at 03:47 AM ---------- Previous update was at 03:45 AM ----------

When I ran the following command in command prompt, I got error as printf: `(': invalid format character.

Code:
ls -1  /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))* > file.txt

---------- Post updated at 03:48 AM ---------- Previous update was at 03:47 AM ----------

Hi RudiC,

When I ran the following command in command prompt, I got error as printf: `(': invalid format character.

Code:
ls -1 /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))* > file.txt


Last edited by radoulov; 08-27-2015 at 10:22 AM.. Reason: Code tags
# 11  
Old 08-27-2015
Please use code tags as required by forum rules!

Do you use a "recent" shell? What be it's version? With GNU bash, version 4.3.30(1), it yields
Code:
ls -1 SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))*
SASApp_STPServer_2015-08-26_tmptcmsaslva2_67891.log

# 12  
Old 08-27-2015
I'm using SSH secure shell and it's version is 3.2.9
# 13  
Old 08-27-2015
That's the communication tool used to connect to the remote machine. On the remote machine, what shell are you running?
# 14  
Old 08-27-2015
I don't know what you're asking. I open SSH secure shell and then I navigate to the tab 'open terminal window' to write/submit my script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing .dat.gz to .txt.gz in filename

Hi, I tried below method; mv -v /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz \ $(echo /oracle1/scr/tilki/willsendtilkiNew/VOICE-MO_$nfname.gz | tr 'dat' 'txt'); nfame variable has the string "dat" . I need to rename files like below; ASIS: 20140902103700_100319.dat.gz... (8 Replies)
Discussion started by: snr_silencer
8 Replies

2. Windows & DOS: Issues & Discussions

Check dir for overly path+filename and list in txt

Well since Windows always laments over some of my files having a too long "path+filename" and it gets in the way of copying complete directory structures I would love to have a DOS Script that helps me with finding those. I already tried DCSoft Long Filename Finder but that is neither DOS based... (3 Replies)
Discussion started by: pasc
3 Replies

3. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. UNIX for Dummies Questions & Answers

Add a new column to txt file containing filename

I would like help adding a new column to a large txt file (~10MB) that contains the filename. I have searched other posts but have not found an adequate solution. I need this extra column so I can concatenate >100 files and perform awk searches on this large file. My current txt file look... (4 Replies)
Discussion started by: kellywilliams
4 Replies

6. Shell Programming and Scripting

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (1 Reply)
Discussion started by: johannd
1 Replies

7. Shell Programming and Scripting

How to unzip Filename.txt.Z

Hii I am having a file in Unix which is filename.txt.Z How can i take of the Z to read the file Please help Regards Laxmi (2 Replies)
Discussion started by: laxmi1166
2 Replies

8. Shell Programming and Scripting

[PERL] Cannot stat or move filename - £££F3AERO££.txt

Scenario: Users drop files into a directory which is regularly polled by my PERL process. On detecting a file my process will move it from the poll dir to a working directory. A user created a file with a £ symbol in the filename and my process now fails. e.g £££F3AERO££.txt ... (1 Reply)
Discussion started by: thefal9
1 Replies

9. Shell Programming and Scripting

write filename as first line in a txt file

Could anyone very kindly help me a simple way to perform the - perhaps - very trivial task of writing the name of a file as first line of that file which is in txt format? And would be possible to do this recursively for some thousands files in the XY directory? And, again, add to the simple... (3 Replies)
Discussion started by: mjomba
3 Replies

10. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies
Login or Register to Ask a Question