Shell script/Perl Script to remove the string until it finds special character '_'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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)
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.
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.
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
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Rep:
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.
.
Last edited by tonyfreeman; 07-16-2012 at 08:33 PM.
Reason: regular user
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.