Passinng specific file extension to while loop in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passinng specific file extension to while loop in ksh
# 1  
Old 12-29-2012
Passinng specific file extension to while loop in ksh

hello,

i have the below while loop wherein i am passig list of filenames to be scped. this is in unix ksh -

filenamelist.txt has list of files names, including .dat and .txt files

but i want to pass only the .txt filenames to the while loop so that only .txt files gets scped.

how can i do that in the below while loop

Code:
while read rname
do
  scp $scp_user@$scp_server:${rname} /a/b/c/
done < filenamelist.txt

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by Franklin52; 12-29-2012 at 08:10 AM..
# 2  
Old 12-29-2012
Do you want to scp this whole file
Code:
filenamelist.txt

?

what is inside the file pls print here

---------- Post updated at 10:10 AM ---------- Previous update was at 10:06 AM ----------

Code:
while read rname
do
a=`echo $rname`
scp $scp_user@$scp_server:${a} /a/b/c/
done < filenamelist.txt

also try removing flower barcakets
# 3  
Old 12-29-2012
Quote:
Originally Posted by billpeter3010
hello,

i have the below while loop wherein i am passig list of filenames to be scped. this is in unix ksh -

filenamelist.txt has list of files names, including .dat and .txt files

but i want to pass only the .txt filenames to the while loop so that only .txt files gets scped.

how can i do that in the below while loop

while read rname
do
scp $scp_user@$scp_server:${rname} /a/b/c/
done < filenamelist.txt
Try:
Code:
grep '.txt' filenamelist.txt | while read rname
do
    scp $scp_user@$scp_server:"$rname" /a/b/c/
done

In this context, $rname and ${rname} expand to the same value. If any of the filenames in filenamelist.txt contain a space or a tab character, a character that could be expanded in pathname expansion, or characters that could be interpreted as a command substitution or arithmetic expansion by ksh; you need the double quotes.
# 4  
Old 12-29-2012
Code:
while read rname
do
ext=`echo $rname|awk -F"." '{print $NF}'`
if [[ "$ext" == "txt" ]]
then
scp $scp_user@$scp_server:${rname} /a/b/c/
fi
done < filenamelist.txt


Regards,
Vijay

Last edited by Scott; 12-30-2012 at 01:43 AM.. Reason: Franklin52: Code tags; Scott: Removed link
# 5  
Old 12-29-2012
Code:
#!/bin/sh
cat filenamelist.txt | grep '.txt' >> newfile.txt
while read rname
do
a=`echo $rname`
scp $scp_user@$scp_server:${a} /a/b/c/
done < newfile.txt

input
Code:
pin@vus520[new]$ cat filenamelist.txt
helo.txt
hi.dat
how.txt
me.txt
you.dat

Code:
helo.txt
how.txt
me.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

ksh to find specific infomation in a delimited file

I am using ksh and looking for a simple way to print the entire row when a specific column contains specific infomation. I know I can use grep to find the information however I can not specify the column. File test.txt contents: Abc,223,223,223 efg,354,224,774 hij,354,2230,773... (5 Replies)
Discussion started by: oldman2
5 Replies

5. Shell Programming and Scripting

Zip the file in a loop in ksh 88

Hi Team, Version : Ksh 88 The Requirement is to zip every file in a folder(which is in for loop) and move the zipped file to another folder I tried like the below , but didn't observe any change ( Infact more size ) after the file is zipped . #!/bin/ksh src_dir=/home/etc/Src_dat... (5 Replies)
Discussion started by: smile689
5 Replies

6. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

7. Shell Programming and Scripting

for loop other file with same name and different extension

Hi Friends, I am using the following command for i in `ls $PWD`; do cat $i > test && mv test $i; done But, I want to execute another command and write the output to another file with different extension but same name, like this I tried using this for i in `ls $PWD`; do... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. Shell Programming and Scripting

KSH Script to Execute command from specific column in file

Hi Unix Experts,Team I have a file (say comand_file.prm), The file has a command specified in column 6 (say "./execute_script.sh"). I want to execute this command in my script. I am writing a KSH script to achieve this. Could you please assist me with this. (6 Replies)
Discussion started by: Jeevanm
6 Replies

9. Shell Programming and Scripting

Replace file name extension in loop

Hi! I have this shell script that I need to finish. Currently I need to fix one line to make it work. I need to change a file extension. See this code, is pretty simple. #!/bin/sh # Extensions OLD_EXT=.flv NEW_EXT=.mp4 COUNT_FILES=$(ls -l *$OLD_EXT | grep ^- | wc -l) if ; then ... (8 Replies)
Discussion started by: pulsorock
8 Replies

10. Shell Programming and Scripting

While file exists with specific extension

Hi, I need some help to see if I this is posible with a while loop. I need to run a script once a day that will take all of the *.TXT files in a folder and rename them to a specific file name structure with a .dat extension. I wrote this script, but I get an error that says " line 3: too many... (2 Replies)
Discussion started by: strpwr
2 Replies
Login or Register to Ask a Question