LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-12-2012, 11:53 PM   #1
pooppp
Member
 
Registered: May 2008
Posts: 32

Rep: Reputation: 15
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
 
Old 07-13-2012, 12:44 AM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 07-13-2012, 01:06 AM   #3
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by anomie View Post
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`
 
Old 07-13-2012, 05:57 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 07-13-2012, 09:29 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by pooppp View Post
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.
 
Old 07-13-2012, 09:40 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 07-15-2012, 05:02 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-16-2012, 03:45 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by pixellany View Post
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
 
Old 07-16-2012, 03:55 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You are right---he wants to search on file names.
 
Old 07-16-2012, 08:31 PM   #10
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
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
 
Old 07-17-2012, 09:36 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] search and replace string with special character perl or sed. Noobux Programming 4 05-21-2012 03:16 PM
shell script to find the sum of numbers associated with each character in a string Aagam Linux - Newbie 22 02-07-2012 08:59 AM
Shell script to remove new line after numeric string grooper Linux - General 2 11-27-2011 11:30 AM
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 07:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration