LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to read part of filename (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-read-part-of-filename-652549/)

fatgoth 06-30-2008 07:22 AM

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.

colucix 06-30-2008 07:49 AM

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

bigrigdriver 06-30-2008 07:59 AM

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


colucix 06-30-2008 09:30 AM

bigrigdriver, sorry for correcting you, but the basename command does not work by dot as separator but by slash only.

Nylex 06-30-2008 02:24 PM

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.

chrism01 06-30-2008 09:49 PM

Or

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

bookmarks

radhikamody 02-26-2012 07:05 AM

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?

lithos 02-26-2012 07:25 AM

Quote:

Originally Posted by radhikamody (Post 4612304)
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

radhikamody 02-26-2012 08:51 AM

i need to edit the same file. cannot create a new file.
how do i go about it in this case

druuna 02-26-2012 09:08 AM

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.

radhikamody 02-26-2012 09:21 AM

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!


All times are GMT -5. The time now is 02:38 AM.