To cut the file name from the ls


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To cut the file name from the ls
# 1  
Old 05-07-2017
To cut the file name from the ls

Hi All,

Im bit stuck s in writing shell script which downloads the file from the SFTP server

Script first list the available files over server and check the today's file in the list.
if file available then transfer function gets called and file downloading starts.
iam able to list out the available files found the today's file in below format.
Code:
available_file_variable=`grep $source_file $listing_file | sort -u`


Code:
+ echo -rw-rw---- 1 vgif_VGL vgif_VGL 32298503 May 06 19:12 UAT.pVV.SID.VGL.D170506.T110027.AMA.MAS.FTP.DATA.gz


now I need to pass the actual filename (
Quote:
UAT.pVV.SID.VGL.D170506.T110027.AMA.BVS.FTP.DATA.gz
) to the variable by which transfer function gets initated.

so here i have the value in available_file_variable =
Code:
-rw-rw---- 1 vgif_VGL vgif_VGL 32298503 May 06 19:12 UAT.pVV.SID.VGL.D170506.T110027.AMA.BVS.FTP.DATA.gz

i need to cut only filename from the available file variable not file permission ,timestamp etc and set to the transfer_file means available file variable >>transfer_file.

how i can do it by cut or awk to set transfer_file = UAT.pVV.SID.VGL.D170506.T110027.AMA.BVS.FTP.DATA.gz
# 2  
Old 05-07-2017
You could correct the variable, by using it like this:
Code:
${var##* }

But that will only work if the files on the ftp server never contain spaces..
Correctly parsing ls -l output can sometimes be surprisingly difficult, because it may vary because if it is younger or older than 6 months or maybe even the locale setting..

I think best would be to adjust the script so that ls -1 is used instead of ls -l (ls minus one vs. ls minus el).

Last edited by Scrutinizer; 05-07-2017 at 03:33 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-07-2017
Hi ,
Sorry didnt get you , my simple requirement is i have value in variable this
Code:
-rw-rw---- 1 vgif_VGL vgif_VGL 32298503 May 06 19:12 UAT.pVV.SID.VGL.D170506.T110027.AMA.MAS.FTP.DATA.gz

i need to fetch only file name
Code:
UAT.pVV.SID.VGL.D170506.T110027.AMA.MAS.FTP.DATA.gz

please advise.
# 4  
Old 05-07-2017
What I meant was you could pass:
Code:
${available_file_variable##* }

instead of
Code:
${available_file_variable}

What I was trying to say is that this should work, if the file name does not contain spaces.

--
You could also change the command substitution to something like this:
Code:
available_file_variable=$(awk -v f="$source_file" "$listing_file" '$0~f {print $NF}' | sort -u)

or
Code:
available_file_variable=$(awk -v f="$source_file" "$listing_file" '$0~f && !A[$NF]++{print $NF}')

Also, providing the file name does not contain space characters...

--
But best would be to adjust the script so that it only stores the file names in the file $listing_file, my suggestion was to use ls -1 rather than ls -l to produce $listing_file
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-07-2017
Well all you need once you got your ls -l output is a pipe with awk to get the 9th field: e.g. with ls -l ->
Code:
 ls -l|awk '{print $9}'

This User Gave Thanks to vbe For This Post:
# 6  
Old 05-07-2017
Quote:
Originally Posted by vbe
Well all you need once you got your ls -l output is a pipe with awk to get the 9th field: e.g. with ls -l ->
Code:
 ls -l|awk '{print $9}'

That will sometimes work with the right locale and if a time six months ago results in the same number of fields and if filenames do not contain spaces.
Otherwise it may fail.

It will also sometimes fail if owner or group name are too long, causing owner and group fields to "blend", which results in fewer fields than provisioned.

Parsing ls -l output is unreliable, unless the variability of the output is known precisely.

Last edited by Scrutinizer; 05-07-2017 at 07:37 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-07-2017
Thank you to both, could manage to do it via awk.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

4. Shell Programming and Scripting

Help required the cut the whole contents from one file and paste it into new file

Hi, First of all sincere apologies if I have posted in a wrong section ! Please correct me if I am wrong ! I am very new to UNIX scripting. Currently my problem is that I have a code file at the location /home/usr/workarea/GeneratedLogs.log :- Code :- (Feb 7, 571 7:07:29 AM),... (4 Replies)
Discussion started by: acidburn_007
4 Replies

5. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

6. Shell Programming and Scripting

cut lines from log file and save it another file

Dears, i want cut the lines from a log file. Example of the log file as follows.. May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMDeliverContrUtil isDeliverSM FINE: May 27, 2011 5:54:51 PM com.huawei.ivas.utilities.sm.client.SMUtil addSysUpMsgLog INFO: . The message content... (1 Reply)
Discussion started by: tonypalokkaran
1 Replies

7. Shell Programming and Scripting

Creating file from an existing file using CUT, is it the best option?

Dear All, I have a requirement in which i have to load a file placed in FTP location onto my database. The process i'll follow is as below: 1) Get the files using FTP. 2) Create the desired load files as i have to load only 19 fields out of the 104 available in the file. The fields i require... (7 Replies)
Discussion started by: abhishekakaomi
7 Replies

8. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question