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 |
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 |
Quote:
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` |
Quote:
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. |
Quote:
Code:
egrep -u "file[0-9]+" | sed 's/file//' |
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 |
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
Quote:
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 string manipulation |
You are right---he wants to search on file names.
|
rename to the rescue
Hi ho,
As root on your system, create this file: Code:
/usr/local/bin/rename Code:
#!/usr/bin/perl -w Code:
chmod 555 /usr/local/bin/rename For instance ... in the /tmp/ directory you have this: Code:
[tony@monarch tmp]$ ls -l *file* Code:
[tony@monarch tmp]$ rename 's/^2012.*_//g' *file* . |
@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 09:02 PM. |