LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2008, 07:22 AM   #1
fatgoth
LQ Newbie
 
Registered: Jun 2008
Posts: 1

Rep: Reputation: 0
Shell script to read part of filename


I am very new to shell scripting and have a problem that i need to get around.

I am backing up data files and need to initially check if a data file has been backed up or not. I want to check the backup directory for an existing backup file and if it exists do a new backup and delete the old one.

My problem is that the filename to be backed up is in the format of <name>.tar but the backup filenames are in the format of <name>.<date>.tgz.

I need to be able to just test the <name> and not the .<date>.tgz part of the backup filename.

Can anyone point me in the right direction please.
 
Old 06-30-2008, 07:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
To extract the <name> part of the file name, provided the dot separates <name> from <date> you can use parameter substitution in this way:
Code:
#!/bin/bash
cd /path/to/backup/directory
for file in *.tgz
do
  name=${file%%[.]*}
  echo $name
  if [ $name = $current_backup_name ]
  then
    # do new backup here
    # delete older one here
  fi
done
where current_backup_name will be the name of the backup you are currently processing, against which you want to find an existing backup. The ${file%%[.]*} substitution, will strip the longest part of the variable "file" from the back end, matching the pattern .* (dot followed by any number of other characters).

Last edited by colucix; 06-30-2008 at 07:51 AM.
 
Old 06-30-2008, 07:59 AM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,877

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
Use the basename utility to strip everything after the first dot in the filename, leaving the basename.

Example: using <name>.<date>.tgz as the filename, then 'basename <name>.<date>.tgz' yields <name> as the output.

So, in the example given by colucix, you could also use
Code:
for file in *.tgz
do
  name=basename $file
  echo $name
 
Old 06-30-2008, 09:30 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
bigrigdriver, sorry for correcting you, but the basename command does not work by dot as separator but by slash only.
 
Old 06-30-2008, 02:24 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Another way to do it is to use awk, specifying the field separator using the -F option, e.g.

ls *.tgz | awk -F . '{print $1}'

awk splits what it calls records (I think that's the right term) - here the filenames - into fields, separated by some character (the default being whitespace). The bit between the single quotes simply says to print the first field.
 
Old 06-30-2008, 09:49 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Or

echo bookmarks.html|cut -d'.' -f1

bookmarks
 
Old 02-26-2012, 07:05 AM   #7
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
my test file is
today is
hi
him
himself
#end of file
i want to replace the last character of lines starting with hi with the flag variable.
i write the below code.
but it makes the test file blank.
Code:
flag=p
cat test | sed '/^hi/s/.$/$flag/' >test
any suggestions why?
 
Old 02-26-2012, 07:25 AM   #8
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by radhikamody View Post
my test file is
today is
hi
him
himself
#end of file
i want to replace the last character of lines starting with hi with the flag variable.
i write the below code.
but it makes the test file blank.
Code:
flag=p
cat test | sed '/^hi/s/.$/$flag/' >test
any suggestions why?
you're redirecting the output to the same file as input - it goes blank of course

try "cat test | sed '/^hi/s/.$/$flag/' >testout" to test it
 
Old 02-26-2012, 08:51 AM   #9
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
i need to edit the same file. cannot create a new file.
how do i go about it in this case
 
Old 02-26-2012, 09:08 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Use sed's -i option (man sed for details):
Code:
cat test | sed '/^hi/s/.$/$flag/' >test

sed -i "/^hi/s/.$/$flag/" test
Too be honest, even that doesn't meet your requirement: There will always be a new/temporary file created. Even when using -i. The only difference between doing it yourself and using -i: sed is doing it for you.

PS: Next time you have a question don't open an existing 3 year old thread. Create a new thread yourself.

Last edited by druuna; 02-26-2012 at 09:10 AM.
 
Old 02-26-2012, 09:21 AM   #11
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
i was opening a new thread but as the same browser had 3 other tabs opened with linuxquestions, it redirected my post to the first tab in the browser!
 
  


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
read from stdin in a shell script bujecas Linux - General 3 07-03-2009 05:46 PM
shell script:filename containing space ! Dr_Death_UAE Programming 3 12-16-2006 07:46 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 02:06 AM
Shell script to copy file name with part of directory Transition Linux - General 5 01-18-2005 06:40 PM
Making shell script a part of linux image mayureshchitale Linux - Newbie 1 07-06-2004 02:00 AM


All times are GMT -5. The time now is 03:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration