LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   how to call a file using the other (https://www.linuxquestions.org/questions/linux-software-2/how-to-call-a-file-using-the-other-662164/)

ZAMO 08-12-2008 02:11 AM

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

chrism01 08-12-2008 02:16 AM

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

matthewg42 08-12-2008 02:35 AM

You can remove suffixes which match a shell glob pattern like this:
Code:

my_variable="FILE_source_test_20080712.txt"
echo "without the .txt it is :        ${my_variable%.txt}"
echo "the shortest part matching _* : ${my_variable%_*}"
echo "the longest part matching _* :  ${my_variable%%_*}"

which will output:
Code:

without the .txt it is :        FILE_source_test_20080712
the shortest part matching _* : FILE_source_test
the longest part matching _* :  FILE

See the "Parameter Expansion" section of the bash manual page for other handy string manipulation techniques.

ZAMO 08-12-2008 03:07 AM

Thank You both for the reply.

Code:

my_variable="FILE_source_test_20080712.txt"
echo "without the .txt it is :        ${my_variable%.txt}"
echo "the shortest part matching _* : ${my_variable%_*}"
echo "the longest part matching _* :  ${my_variable%%_*}"

This one got me a output close to me .
Code:

the shortest part matching _* :FILE_source_test_20080712
the longest part matching _* : FILE

.Is there a way to cut the filename string before 2008 or the "2" occurrence.

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 :)

matthewg42 08-12-2008 03:25 AM

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.

chrism01 08-12-2008 03:26 AM

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

ZAMO 08-12-2008 03:57 AM

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.

theYinYeti 08-12-2008 04:09 AM

I think you need:
Code:

filenameB="${filenameA:0:$((${#filenameA}-12))}20080712"
In case you want to retrieve the date, the value is:
Code:

dateA="${filenameA: -12:8}"
Code:

[yves@localhost ~]$ filenameA=FILE_source_test_test1.20080812.txt
[yves@localhost ~]$ filenameB="${filenameA:0:$((${#filenameA}-12))}20080712"
[yves@localhost ~]$ echo $filenameB
FILE_source_test_test1.20080712
[yves@localhost ~]$ echo "${filenameA: -12:8}"
20080812

Yves.

chrism01 08-12-2008 04:11 AM

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_

ZAMO 08-12-2008 04:52 AM

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

chrism01 08-12-2008 08:08 PM

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.