LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-24-2011, 11:47 AM   #1
Allis
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Rep: Reputation: Disabled
Help with sed and parentheses.


So I'm trying to something similar to

http://www.linuxquestions.org/questi...ng-sed-812975/

e.g.
Test (Remove) --> Test

Here is what I was trying

#for i in *\(*\)*; do j=`echo $i |sed -e 's/[()]//g'`; mv "$i" "$j"; done

for f in *; do fn=`echo $f|sed 's/\([[:alnum:]]*\)[[:space:]]*(.)\(\..*\)/\1\2/'`; mv $f $fn; done

The first one only removes parentheses, so I don't want that one. I tried the second one but it just won't work; it does nothing. I don't really understand whats wrong.

Thank you.
 
Old 12-24-2011, 12:07 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
How about this, using the Perl rename command:

Code:
IFS='' find -type f -exec rename 's/ ?\(.*\)\ ?//' {} \;
What's going on here:

(1) Set the input field separator temporarily to nothing (to get around white space wrongly delimiting the filenames.)
(2) Put the rename command in a find command, to enact the command on the entire current directory tree.
(3) The regular expression 's/ ?\(.*\)\ ?//' replaces matches of (0-1 spaces)(anything in parentheses)(0-1 spaces) with the empty string.

HTH

Last edited by jhwilliams; 12-24-2011 at 12:11 PM.
 
1 members found this post helpful.
Old 12-24-2011, 12:28 PM   #3
Allis
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you that is very helpful! I was wondering is it possible to run both a bash script and a perl script in one file?

Or do you know how I can remove the first 3 or X chars from the file name using perl?

Last edited by Allis; 12-24-2011 at 12:35 PM.
 
Old 12-24-2011, 12:53 PM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Well, that's not actually Perl, just a call to a remote Perl script. It's all bash. But yes, if you wanted to script it you could:

Code:
#!/bin/bash

# First command, get rid of all of the () stuff:
IFS='' find -type f -exec rename 's/ ?\(.*\)\ ?//' {} \;

# Okay, now remove the first 3 chars (or replace with 3 with however many you want.)
IFS='' find -type f -exec rename 's/^.{3}//' {} \;
 
1 members found this post helpful.
Old 12-24-2011, 03:16 PM   #5
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
You can also try using bash's exended globbing patterns.

Code:
for f in *\(*\)*; do echo "${f//\(*([[:alnum:]])\)}"; done
For more advanced work, you can use real regexes inside of [[..]]. The BASH_REMATCH array stores any captures.

Code:
name='keepthis(remove this)keepthis.txt'
re='^([^(]+)[(][^)]+[)](.*)$'

[[ $name =~ $re ]] && echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"

# output: keepthiskeepthis.txt
More bash string manipulation techniques here:

http://mywiki.wooledge.org/BashFAQ/100
 
  


Reply



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] Need to add parentheses to 111,000 lines of txt? Mojojo Linux - Newbie 4 09-26-2011 09:45 PM
what is the meaning of underline that before parentheses? skiron.liu Programming 1 06-22-2010 09:35 AM
using sed with parentheses in perl beebop Linux - General 2 07-21-2005 10:41 PM
Emacs config and parentheses jrdioko Linux - Software 0 07-16-2005 03:14 PM
sed error: "parentheses not balanced" - But they are! Travis86 Programming 2 08-12-2004 07:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:18 AM.

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