LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-09-2022, 05:02 PM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Re-arrange a string - bash or awk?


I have a file name like this
Code:
Book Title (author)
and I want to swap this around
Code:
(author) Book Title
I think awk should be able to do this, but I cannot figue out how to do it
Then I thought a bash scripy might do it, again not too sure what to do.


I have about 1,000 files to be processed.


If i could get the position of the brackets I could chop that out and then prepend it to the begining, well thats my plan for the bash script.
 
Old 06-09-2022, 05:31 PM   #2
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Okay I can do this
Code:
jonke@charlie:~$ echo $x
abc murders (Agatha Christie).epub
jonke@charlie:~$ y=${x%(*}
jonke@charlie:~$ echo $y
abc murders
jonke@charlie:~$ z=${x:${#y}}
jonke@charlie:~$ echo $z
(Agatha Christie).epub
jonke@charlie:~$ a=${z%.*}
jonke@charlie:~$ echo $a
(Agatha Christie)
jonke@charlie:~$ echo $a $y
(Agatha Christie) abc murders
But there must be better more elegant way

Last edited by GPGAgent; 06-10-2022 at 09:47 AM.
 
Old 06-09-2022, 07:35 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,151

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Quote:
Originally Posted by GPGAgent View Post
But there must be better more elegant way
Several.
Filenames with whitespace in them will be the bane of your life - FWIW I change them all to underscore.

Mangling the string that represents the filename is merely the first step - you still need to feed it all to mv. Better option is to see if you have the perl version of rename - it can do the whole deal, for all files, in a single command with a little regex.
 
Old 06-09-2022, 11:20 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
You could use regex if you know the format is always the same:
Code:
x='abc murders (Agatha Christie).epub'
reg='([^(]+)(\([^)]+\))'

[[ "$x" =~ $reg ]] && echo done

echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
or maybe an awk:
Code:
awk -F'[()]' '{print "title - ",$1;print "author - ",$2}' <<<'abc murders (Agatha Christie).epub'
 
Old 06-09-2022, 11:23 PM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by GPGAgent View Post
I have a file name like this
Code:
Book Title (author)
and I want to swap this around
Code:
(author) Book Title
I think awk should be able to do this, but I cannot figue out how to do it
Then I thought a bash scripy might do it, again not too sure what to do.
The guts of something that receives each filename could use something like
Code:
IN="$*"
TITLE=$( echo ${IN} | cut -d'(' -f1 )
AUTHOR=$( echo ${IN} | cut -d'(' -f2 | cut -d')' -f1 )
OUT="(${AUTHOR}) ${TITLE}"
Using "$IN" and "$OUT", you can likely build a "mv" command to rename the files to the new format.

But... as someone pointed out, spaces in filenames will drive you crazy. I often run a script on the files or directory tree to replace spaces with underscores. Downside? Yes, Occasionally, I'll find that some of the files refer to the "spaced" version of other files and/or directories. Fixing those when encountered, though, seems easier than having to account for spaces in every single utility I have to write to deal with them. Maybe take this as an opportunity to make a new format like, say:
Code:
Author_Name--The_Title_of_the_Book    or
Author_Name:The_Title_of_The_Book

Last edited by rnturn; 06-09-2022 at 11:25 PM. Reason: Typos
 
Old 06-10-2022, 01:21 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,350
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
Quote:
Originally Posted by syg00 View Post
Better option is to see if you have the perl version of rename - it can do the whole deal, for all files, in a single command with a little regex.
I, too, would turn to the perl version of rename since it can do both Perl regular expressions and perl expressions (scripting) in the transformation of the name:

Code:
rename -n -v 's/^(.*)\s+(\([^)]*\))\.epub/$2 $1/' *.epub

Last edited by Turbocapitalist; 06-10-2022 at 08:37 AM. Reason: typo in regex
 
Old 06-10-2022, 08:36 AM   #7
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Hi All, Plenty to check out here, many thnaks.

And yes white space is pain (PITA).

Perl looks a good bet, I don't know perl so this will teach me something new.

I'll leave this open for a bit and post my final solution when I have it and yes I do have a backup.

laters folks
 
Old 06-10-2022, 08:51 AM   #8
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,382

Rep: Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761
A bash solution is a little more verbose.
Code:
# For recursion
# topdirectory=$HOME/some/directory
# cd $topdirectory
# shopt -s globstar
# for f in **/*.epub; do
for f in *.epub; do
  [[ "$f" =~ ([^(]+) (\([^)]+\))(.*) ]] && echo mv "$f" "${BASH_REMATCH[2]} ${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
done
Remove the 'echo' if happy.

PS - Damn this new code parsing stuff at LQ (It just swapped in a hyphen for tilde). "$f" =~ should read "$f" ="tilde character"
 
1 members found this post helpful.
Old 06-10-2022, 09:04 AM   #9
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by allend View Post
A bash solution is a little more verbose.
Code:
# For recursion
# topdirectory=$HOME/some/directory
# cd $topdirectory
# shopt -s globstar
# for f in **/*.epub; do
for f in *.epub; do
  [[ "$f" =~ ([^(]+) (\([^)]+\))(.*) ]] && echo mv "$f" "${BASH_REMATCH[2]} ${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
done
Remove the 'echo' if happy.

PS - Damn this new code parsing stuff at LQ (It just swapped in a hyphen for tilde). "$f" =~ should read "$f" ="tilde character"

Perfect, many thanks
 
Old 06-10-2022, 09:58 AM   #10
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,382

Rep: Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761
Thanks go to @grail, who first introduced me to the power of BASH_REMATCH. I just fixed the space character handling.
 
Old 06-10-2022, 10:21 AM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by allend View Post
PS - Damn this new code parsing stuff at LQ (It just swapped in a hyphen for tilde). "$f" =~ should read "$f" ="tilde character"
Huh? Only hyphens in your post are after "shopt " and "PS "; the tildes are correct both inside and outside the code block.

 
1 members found this post helpful.
Old 06-10-2022, 10:36 AM   #12
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by boughtonp View Post
Huh? Only hyphens in your post are after "shopt " and "PS "; the tildes are correct both inside and outside the code block.
That's what I saw (thought)!


~
-
 
Old 06-11-2022, 05:55 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,151

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
There is the small issue of pre-existing target filenames tho' ...
 
1 members found this post helpful.
Old 06-12-2022, 07:51 AM   #14
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by syg00 View Post
There is the small issue of pre-existing target filenames tho' ...
For this reason I'll copy the file with it's new name to a folder called PROCESSED
 
  


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] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
arrange data in columns side by side using awk vjramana Programming 8 10-19-2010 05:42 AM
need help in bash scripting...need to arrange variables in column wise manya Programming 7 11-07-2009 01:54 AM

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

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