for loop other file with same name and different extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop other file with same name and different extension
# 1  
Old 10-02-2012
for loop other file with same name and different extension

Hi Friends,

I am using the following command

Quote:
$ ls (before for loop)
1.bed
2.bed
Code:
for i in `ls $PWD`; do cat $i > test && mv test $i; done

Quote:
$ ls (after for loop)
1.bed
2.bed

But, I want to execute another command and write the output to another file with different extension but same name, like this

Quote:
$ ls (after for loop using new code)
1.bed
1.html
2.bed
2.html
I tried using this

Code:
for i in `ls $PWD`; do cat $i > $i.html; done

But, this doesn't work

Any thoughts
# 2  
Old 10-02-2012
ls already lists the contents of the current directory; there's no need to use $PWD.

The safest way to generate a lexicographically-sorted list of a directory's contents for a for-loop is to use * and pathname expansion (aka globbing). Using ls without any options is both pointless and fragile.

A file extension can be omitted with the following parameter expansion operation: ${i%.*}

That should get you there.

Regards,
Alister

Last edited by alister; 10-02-2012 at 01:17 PM..
# 3  
Old 10-02-2012
There could be a better way to this..but you can try if this works..

Code:
for i in `ls $PWD`
do 
cat $i > $i.html
new_file=`echo $i.html | awk -F"." '{print $1"."$3}'`
mv $i.html $new_file
done


Last edited by Nishi_Licious; 10-03-2012 at 03:28 PM.. Reason: Adding Code tags
This User Gave Thanks to Nishi_Licious For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Remove extension in loop

Dear all sorry for bothering you wityh dumb question but I am stucked with an issue. Well, I am trying to loop over files in folder, make an operation and rewrite the output. Example: INPUT file1.txt file2.txt file3.txtMy command (doesn't work!!) for file in /path/to/*.txt do command... (13 Replies)
Discussion started by: giuliangiuseppe
13 Replies

3. UNIX for Dummies Questions & Answers

File Extension Missing in file-$var.csv

I'm afraid this is a silly question but I can't figure it out. I have a script like so... echo "Enter DRDL Signature Version Number" read DRDL_Number mv signature_output.csv SERVICE_OBJECTS_S-$DRDL_Number.csv The resultant filename does not contain the .csv as follows.... (3 Replies)
Discussion started by: Cludgie
3 Replies

4. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

5. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: billpeter3010
4 Replies

6. Shell Programming and Scripting

Getting the file extension

I have a file n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod and I want to get the extension. At the moment I have set filextension = `echo $f | awk 'BEGIN {FS="."} {print $2}'` which of course does not work as there is a point in varp0.25 (13 Replies)
Discussion started by: kristinu
13 Replies

7. 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

8. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

9. UNIX for Dummies Questions & Answers

About the file extension

Hi everyone, Can we know that which type of file is there without opening a file thanks in advance.... kunal patil (3 Replies)
Discussion started by: kunalpatil09
3 Replies

10. UNIX for Dummies Questions & Answers

Get the name of the file excluding the extension

How to get the name of the file by excluding the extention of file name? For example, my filename is 'test.txt'. I want to get only the name 'test' but not the extention .txt. (2 Replies)
Discussion started by: vinay123
2 Replies
Login or Register to Ask a Question