LinuxQuestions.org
Review your favorite Linux distribution.
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 07-07-2017, 06:18 PM   #16
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,425

Rep: Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054Reputation: 2054

Quote:
Originally Posted by jpollard View Post
using -- doesn't always work, it depends on the utility. It will USUALLY work with the GNU tools, but there are exceptions.
[snip by Sean -- see post #14]
Yet another lesson learned. Thank you very much!! This has been a very educational thread!!
 
Old 07-08-2017, 09:17 AM   #17
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,571

Original Poster
Rep: Reputation: 249Reputation: 249Reputation: 249
Quote:
Originally Posted by jpollard View Post
The simpler solution is "./-xxxx"
This does not address my question: I asked about replacing a string, not specifying set of files.
 
Old 07-08-2017, 04:49 PM   #18
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,907

Rep: Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510
Quote:
Originally Posted by RandomTroll View Post
This does not address my question: I asked about replacing a string, not specifying set of files.
You asked.
Quote:
Sometimes I want to use rename on a group of files to replace a string that begins with a -. No kind of quoting keeps rename from treating the - as an option instead of an argument.
Since you included the second sentence, we answered. The strings you were dealing with were file names beginning with "-", which is the option indicator.

You have been give two - use the option "--" which many utilities use to indicate "no options follow". This works most of the time - but it doesn't work ALL the time. Thus I gave you the alternative of starting the file string with "./" which provides the same file using relative addressing, and does not start with "-".

Without that second sentence, you would not have been given any of the answers because using string substitution is a simple shell operation; also with multiple solutions (variable substitution, substrings, string concatenation). But that is a different problem.

Since you indicated problems with file names and options - that is a different problem, and why you got these answers instead.
 
Old 07-09-2017, 12:01 AM   #19
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,571

Original Poster
Rep: Reputation: 249Reputation: 249Reputation: 249
Quote:
Originally Posted by jpollard View Post
The strings you were dealing with were file names beginning with "-"
No. The strings were substrings in file names, the substrings beginning with -.

Quote:
Originally Posted by jpollard View Post
I gave you the alternative of starting the file string with "./" which provides the same file using relative addressing, and does not start with "-".
I just tried.
Code:
rename ./-2016 ./_2016 *.html
does not replace -2016 with _2016. Your solution applies only to the names of the target files, not the issue I raised.

Quote:
Originally Posted by jpollard View Post
Since you indicated problems with file names and options - that is a different problem, and why you got these answers instead.
I indicated no problem with file names but with the way rename treats arguments that happen to be in file names.
 
Old 07-09-2017, 03:10 AM   #20
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 363Reputation: 363Reputation: 363Reputation: 363
From man rename, 'expression' seems to be (sort-of anyway) a 'mini-regex',
so it can't handle ./whatever (tho the 3rd arg, the filenames, can be ./stuff)
Those 2 expression replacement operate on just the name, not shell directory syntax!
Like sed s/expression/replacement/

rename -v -- - _ -
worked for me, and renamed my `mkdir -- -` to a dir named underscore

The -- is needed by getargs/getopts, to stop 'switch processing'.

So, to 'null-out' the first dash/hyphen, try: rename -v -- - '' <normal shell filename pattern>
First check: ls -d -- <that patttern, like -myODDfileORdirName>

errors&all, but this shows 'proof' (on Slackware)
Code:
login as: user
user@127.0.0.1's password:
Last login: Wed Mar  1 09:15:15 2017
Linux 4.4.14.
user@darkstar:~$ ls
PACKAGES.TXT  myjunk  z  zfind  zloc
user@darkstar:~$ mkdir -- -mydir; touch -myfile1 my-file2 myfile3- myfile4--
touch: invalid option -- 'y' EDITNOTE: needed -- so -my... NOT switches!
Try 'touch --help' for more information.
user@darkstar:~$ touch -- -myfile1 my-file2 myfile3- myfile4--
user@darkstar:~$ ls
-mydir/   PACKAGES.TXT  myfile3-   myjunk  zfind
-myfile1  my-file2      myfile4--  z       zloc
user@darkstar:~$ rename -- -v - '' *my*
user@darkstar:~$ ls #ooops, -- stopped the -v AND made -v THE expr!
-mydir/   PACKAGES.TXT  myfile3-   myjunk  zfind
-myfile1  my-file2      myfile4--  z       zloc
user@darkstar:~$ rename -v --  - '' *my*
`-mydir' -> `mydir'
`-myfile1' -> `myfile1'
`my-file2' -> `myfile2'
`myfile3-' -> `myfile3'
`myfile4--' -> `myfile4-'
user@darkstar:~$ ls
PACKAGES.TXT  myfile1  myfile3   myjunk  zfind
mydir/        myfile2  myfile4-  z       zloc
user@darkstar:~$
Hmmm.../ooops: the regex 'magic' seems to be more than I thought:
Code:
user@darkstar:~$ rm myf*; rmdir mydir
user@darkstar:~$ mkdir -- -mydir; touch -- -myfile1 my-file2 myfile3- myfile4--
user@darkstar:~$ rename -v --  './-' '' ./*my*
`./-mydir' -> `mydir'
`./-myfile1' -> `myfile1'
user@darkstar:~$
Ok, overSolved Then there's always util-linux/rename.c

Last edited by Jjanel; 07-09-2017 at 04:32 AM.
 
  


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
sed search for a string, duplicate original and replace the string Jykke Programming 17 06-29-2016 10:14 AM
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
Shell script, recursive rename/string replace? lrall Linux - General 1 07-27-2010 10:12 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

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

All times are GMT -5. The time now is 08:38 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