LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-12-2008, 03:11 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
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
 
Old 08-12-2008, 03:16 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,818

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
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
 
Old 08-12-2008, 03:35 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 08-12-2008, 04:07 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
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

Last edited by ZAMO; 08-12-2008 at 04:38 AM. Reason: Removing my own meaningless words
 
Old 08-12-2008, 04:25 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 08-12-2008, 04:26 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,818

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
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....
 
Old 08-12-2008, 04:57 AM   #7
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
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.
 
Old 08-12-2008, 05:09 AM   #8
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.
 
Old 08-12-2008, 05:11 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,818

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
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_
 
Old 08-12-2008, 05:52 AM   #10
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
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
 
Old 08-12-2008, 09:08 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,818

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to call commands from another file qipman Linux - General 3 06-06-2008 09:15 AM
How do I call a .sh script from a kickstart file? below_average Linux - Software 2 01-17-2008 10:46 AM
error from .php file call in cron just_me_then Linux - Newbie 1 01-07-2007 03:52 AM
call a php file petenyce Linux - Newbie 3 11-23-2005 04:21 AM
allocate file blocks via sys call jwstric2 Programming 1 10-13-2004 03:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration