LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script/Perl Script to remove the string until it finds special character '_' (http://www.linuxquestions.org/questions/programming-9/shell-script-perl-script-to-remove-the-string-until-it-finds-special-character-_-4175416444/)

pooppp 07-12-2012 11:53 PM

Shell script/Perl Script to remove the string until it finds special character '_'
 
Hi,

I would to search for a value (1 to 120) in for loop on the file name.

eg. 2012-07-12T164536_file119

Looking for perl/shell script for this..

Pls help

Thanks,
poons

anomie 07-13-2012 12:44 AM

Your question is ambiguous. In the example file name you refer to, are the last few bytes the number you're looking to compare against? And are they always preceded by the word "file"?

If yes and yes:

Code:

#!/bin/bash

# Not perfect, but you get the idea
#

for I in * ; do

        [ ! -e "${I}" ] && break

        _file_num="$(sed 's/.*file//' <<<${I})"

        echo "File number is ${_file_num}"

done

exit 0


pooppp 07-13-2012 01:06 AM

Quote:

Originally Posted by anomie (Post 4726691)
Your question is ambiguous. In the example file name you refer to, are the last few bytes the number you're looking to compare against? And are they always preceded by the word "file"?

If yes and yes:

Code:

#!/bin/bash

# Not perfect, but you get the idea
#

for I in * ; do

        [ ! -e "${I}" ] && break

        _file_num="$(sed 's/.*file//' <<<${I})"

        echo "File number is ${_file_num}"

done

exit 0



Yes, it always proceed with file..

I m trying to use the suggested sed command in a parameter -n below and -P is the path contains files. Pls suggest how to use sed command for -n (tried exec and it is not working)

-n exec(sed 's/.*file//' <<<${i}) -P /file_sys1/snaps`

anomie 07-13-2012 05:57 PM

Quote:

Originally Posted by pooppp
Code:

-n exec(sed 's/.*file//' <<<${i}) -P /file_sys1/snaps`

I can't make any sense out of that. It's syntactically invalid, and it looks like part of a find(1) command invocation.

Could you please explain exactly what you intend to do with that statement? Some sample file names (and what you expect to do with those names) would be helpful.

pixellany 07-13-2012 09:29 PM

Quote:

Originally Posted by pooppp (Post 4726661)
the thread title:
Shell script/Perl Script to remove the string until it finds special character '

I would to search for a value (1 to 120) in for loop on the file name.

If I take this at face value, then you could do something like:
Code:

egrep -u "file[0-9]+" | sed 's/file//'
Glancing at the whole thread, I'm not sure what you really want, but I'd encourage you to learn the commands in small bites---make sure you understand how each one works before trying to construct complex scripts.

pixellany 07-13-2012 09:40 PM

I read the title again----it's actually much simpler.

If you really mean: "remove the string until it finds special character "_" "

just do this:
Code:

grep -u "_file.*$" filename

Tinkster 07-15-2012 05:02 PM

Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.

David the H. 07-16-2012 03:45 AM

Quote:

Originally Posted by pixellany (Post 4727596)
I read the title again----it's actually much simpler.

If you really mean: "remove the string until it finds special character "_" "

just do this:
Code:

grep -u "_file.*$" filename

Pix, this filters the contents of the file. But I think he wants to filter the file name itself.

It would help if the OP came back and clarified his needs (show us the exact input and desired output), but my guess is that he wants to remove everything but the final digits of the filename. Once the name is in a variable, that should be easy. No need to use sed or any other external tool.

Code:

for fname in *file[0-9]*; do

        echo "${fname##*_file}"

done

parameter substitution
string manipulation

pixellany 07-16-2012 03:55 AM

You are right---he wants to search on file names.

tonyfreeman 07-16-2012 08:31 PM

rename to the rescue
 
Hi ho,

As root on your system, create this file:

Code:

/usr/local/bin/rename
Here is the content of that file:

Code:

#!/usr/bin/perl -w
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}

Make sure it's executable:

Code:

chmod 555 /usr/local/bin/rename
Now ... as any user (not root) inside the directory where you have all those files that have the date/timestamp_file### naming convention ... you can use "rename" to rename them all using perl regular expression syntax.

For instance ... in the /tmp/ directory you have this:

Code:

[tony@monarch tmp]$ ls -l *file*
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T11191_file111
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T11362_file16
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T12355_file110
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T13386_file13
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T14323_file116
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T14433_file114
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T15793_file113
-rw-rw-r--. 1 tony tony 0 Jul 16 20:16 2012-07-12T164536_file119
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T16453_file117
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T17585_file11
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T23330_file19
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T29726_file119
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T29954_file14
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T31337_file15
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T4150_file18
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T4193_file118
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T55_file12
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T7550_file120
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T7789_file115
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T840_file17
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 2012-07-12T8832_file112

Use the rename program you just created to do a mass rename of these files using perl regex like so:

Code:

[tony@monarch tmp]$ rename 's/^2012.*_//g' *file*
[tony@monarch tmp]$ ls -l *file*
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file11
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file110
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file111
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file112
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file113
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file114
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file115
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file116
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file117
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file118
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file119
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file12
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file120
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file13
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file14
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file15
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file16
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file17
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file18
-rw-rw-r--. 1 tony tony 0 Jul 16 20:19 file19

Some distributions come with this program and is located here: /usr/bin/rename ... some come with a program called 'rename' but it is a compiled program with limited use.

.

David the H. 07-17-2012 09:36 AM

@tonyfreeman

First, the OP never mentioned renaming the files, only searching for a value in the names.

Second, there are already many good renaming tools available in most distributions. The perl package in Debian and Debian-based distros, for example, already includes a rename script ("prename", which will be automatically aliased to "rename" when installed.) that's certainly more robust than the one you posted. You can also get it here, among other places on the web.


All times are GMT -5. The time now is 05:11 PM.