how to call a file using the other
Hi all,
I am working on a bash script. I have 2 files in 2 different directories and i have to call a file using the half name of the other. Say I have a file FILE_source_test_20080812.txt in dir A and i have another one with the name FILE_source_test_20080712 in dir B . 1. Is there a way to cut the name FILE_source_test_20080812.txt into FILE_source_test_ and call for other. 2. Some files I have the name as FILE_source_test_test1.20080812.txt , as many parts of the name vary , only the date and .txt remains same. I need some one to init me. Thanks |
Your case 1. doesn't seem to follow. Do you mean cut FILE_source_test_20080812.txt and get FILE_source_test_20080712?
echo FILE_source_test_20080712.txt|cut -d'.' -f1 FILE_source_test_20080712 Case 2. what do you want as output from FILE_source_test_test1.20080812.txt ? its got 2 dots in the name? Maybe echo FILE_source_test_test1.20080712.txt|cut -d'.' -f1 FILE_source_test_test1 |
You can remove suffixes which match a shell glob pattern like this:
Code:
my_variable="FILE_source_test_20080712.txt" Code:
without the .txt it is : FILE_source_test_20080712 |
Thank You both for the reply.
Code:
my_variable="FILE_source_test_20080712.txt" Code:
the shortest part matching _* :FILE_source_test_20080712 Chrism, The file get 2 dots in it and the occurance of _ vary from 1 to 4. so the only constant character , i think is 2(upto 2999) Need your comments. Thanks again :) |
Zamo, I was not attempting to give you the direct solution - just to help you understand that there are tools the shell provides which you can use to achieve what you want. You should read the section of the manual page I cited - this will give you all the information you need to complete the task, and many others like it.
|
In that case my case 2 soln gets what you want. What's the problem?
If you don't think so, please give a clear example of what the input filename is, and exactly what you want.... |
Sorry Matthew Gates,
I did not want to mean what I wrote there in my previous post. As English is not my mother tongue, I used to translate what I feel or mean to say in my mother tongue. Chris, I am working on a script which am using to compare a group of files contents . I have directory A which has ,say 300 files and B also with 300 files. The Files in both places have same name as pre_fix and the difference is the date part attached in the name. FILE_source_test_20080712.txt in A will be find in B as FILE_source_test_20080822.txt. As there are 300 files like this I am trying to CALL a file from A and using its name am trying to call its counterpart in B. |
I think you need:
Code:
filenameB="${filenameA:0:$((${#filenameA}-12))}20080712" Code:
dateA="${filenameA: -12:8}" Code:
[yves@localhost ~]$ filenameA=FILE_source_test_test1.20080812.txt |
See, you've only got one(!) dot in the filenames there...
If you want to get all of the filename before the date >echo FILE_source_test_20080712.txt|cut -d'2' -f1 FILE_source_test_ |
Thanks Chris,
It works. yes In the example i mentioned with only one dot. Practically the files has 3 dots. Thanks for all the Valuable time you spent . Thank you theYinyeti |
Next time, please do us (and yourself) a big favour and give CORRECT examples. As you can see, it makes a huge difference to the answers.
|
All times are GMT -5. The time now is 05:50 PM. |