Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-12-2012, 11:53 PM
|
#1
|
|
Member
Registered: May 2008
Posts: 32
Rep:
|
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
|
|
|
|
07-13-2012, 12:44 AM
|
#2
|
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Debian, FreeBSD, Ubuntu (desktop)
Posts: 3,859
Rep: 
|
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
Last edited by anomie; 07-13-2012 at 12:47 AM.
|
|
|
|
07-13-2012, 01:06 AM
|
#3
|
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
Quote:
Originally Posted by anomie
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`
|
|
|
|
07-13-2012, 05:57 PM
|
#4
|
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Debian, FreeBSD, Ubuntu (desktop)
Posts: 3,859
Rep: 
|
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.
|
|
|
|
07-13-2012, 09:29 PM
|
#5
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by pooppp
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.
|
|
|
|
07-13-2012, 09:40 PM
|
#6
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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
Last edited by pixellany; 07-15-2012 at 05:03 PM.
|
|
|
|
07-15-2012, 05:02 PM
|
#7
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
|
|
|
07-16-2012, 03:45 AM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Quote:
Originally Posted by pixellany
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
|
|
|
|
07-16-2012, 03:55 AM
|
#9
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
You are right---he wants to search on file names.
|
|
|
|
07-16-2012, 08:31 PM
|
#10
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
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
|
|
|
|
07-17-2012, 09:36 AM
|
#11
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
@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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:34 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|