Help with rearranging file with script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with rearranging file with script
# 1  
Old 10-30-2016
Ubuntu Help with rearranging file with script

Hi Guys

I normally do thins with a Windows program but I am trying to rearrange a filename based on delimiters in Ubuntu.

Example
Code:
v017 __ Detective Academy Q #133 Murder in the Village Of Suspension Bridges &&& Part 9.cbz

=
Code:
Detective Academy Q v017 #133 Murder in the Village Of Suspension Bridges -  Part 9

Code:
vTBD __ Gintama #609 Old Men Etch Things They Can't Forget Into Their Wrinkles

=
Code:
Gintama vTBD #609 Old Men Etch Things They Can't Forget Into Their Wrinkles

Sorry if this doesn't make sense I have been trying to figure this out for awhile. Any help would be appreciated


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-30-2016 at 11:40 AM.. Reason: Added CODE tags.
# 2  
Old 10-30-2016
Hi,

see if this helps.

Code:
#!/bin/bash

for i in v*;
do
tmp=$(echo "$i" | sed -e 's/\&\&\&/-/' -e 's/__//')
tmp2=${tmp%.*}
tmp3=$(echo $tmp2 | sed -e 's/^\(v[0-9a-zA-Z]\{3\}\)\(.*\)\(#[0-9]\{3\}\)/\2\1 \3/')
echo "new name is: ${tmp3}"
# mv "${i}" "${tmp3}"
done

Once you have verified echo output, uncomment mv command.
This User Gave Thanks to greet_sed For This Post:
# 3  
Old 10-30-2016
Thank you. That works perfectly. Any chance I can get it to work on files inside subfolders
# 4  
Old 10-30-2016
Hi,

Can you try the below one?

Code:
find . -name "v*" | while read i
do
tmp=$(echo "$i" | sed -e 's/\&\&\&/-/' -e 's/ __ //' -e 's/\.[a-z]*$//' -e 's/\(v[0-9a-zA-Z]\{3\}\)\(.*\)\(#[0-9]\{3\}\)/\2\1 \3/')
echo "new name is: ${tmp}"
#mv "${i}" "${tmp}"
done

Note: code assumes 's/\.[a-z]*$' extension has alphabets. Modify it as per your file extensions .
This User Gave Thanks to greet_sed For This Post:
# 5  
Old 10-30-2016
Works brilliantly except for when the title starts with an A

Code:
vNA __ Autophagy Regulation #074 The withdrawal
utophagy Regulation vNAA #074 The withdrawal

Really appreciate your help


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-30-2016 at 11:45 AM.. Reason: Added CODE tags.
# 6  
Old 10-30-2016
It is not the "A" - it is the three chars that are expected after the "v" but you supply just 2, so it steals the next char (here: "A") as well - see the "vNAA" in the middle of the name.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-30-2016
Cheers I can fix that at my end Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rearranging the column

I have a text file like this, I would like to rearrange the first column (Name) according to the third column(percentage)in descending order. I mean methionine with the highest percentage should be the first one to appear under the name column. But I also want to exclude the headers from this... (2 Replies)
Discussion started by: cathum
2 Replies

2. Shell Programming and Scripting

Need Help in rearranging the content of a file comma seperated

I have a file with the below content a = test1 b = test2 a = test3 b= test4 c = test6 b = test5 d = test7 d = test9 Need the output to be as follows a = test1,test3 b = test2, test5 c = test6 d = test7, test9 (4 Replies)
Discussion started by: iron_michael86
4 Replies

3. Shell Programming and Scripting

Rearranging of values as desired

i have a file as below :- 100 D 22 100 T 33 100 C 89 101 C 55 101 D 44 102 D 88 103 T 22 103 C 13 output format :- <number> <D value> <C Value> <T Value> if no value then zero. I want output as :- 100 22 33 89 101 44 55 0 102 88 0 0 103 0 13 22 (3 Replies)
Discussion started by: satishmallidi
3 Replies

4. Shell Programming and Scripting

Counting and rearranging the rows

Hi, I have a file that I re-arranged using awk and unix commands to produce a file that looks like this: JOE JOE JOE JOE JOE BOB BOB HI HI HI I want to count how many of the same rows there are and print it on the second column while only maintaining the original name once. The... (5 Replies)
Discussion started by: phil_heath
5 Replies

5. Shell Programming and Scripting

Doubt with rearranging file through awk

Filename1.xml NO 2012-11-16 02:00:27 20121115/pathname/ asia Filename1.rec YES 2012-11-16 01:20:24 20121115/pathname asia FIleName2.xml YES 2012-11-16 01:20:25 20121115/pathaname asia if the file content are... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

6. Shell Programming and Scripting

Rearranging the colums in a tab delimited file

I want to rearrange some of my columns in my dat file; how do i do this using a script Suppose, I have an input file like this: BASENAME STREETTYPE PREFIX SUFFIX HOUSENUMBER BUILDUP ORDER8 ORDER2 ORDER1 ISOCOUNTRYCODE POSTALCODE SILVER LAKE RD NW 1135 NEW BRIGHTON RAMSEY MINNESOTA USA 55112... (4 Replies)
Discussion started by: ramky79
4 Replies

7. Shell Programming and Scripting

Rearranging

Hello, I spent all day trying to write a script and cannot find the solution :( I have plenty files looking like this: several hundred lines precede the following interesting Bla xxx: Blub = -7537.37687 Blub = -100.644746 Blub = -3247.61954 . . . Blub = 1324.82567 Blub =... (2 Replies)
Discussion started by: tempestas
2 Replies

8. UNIX for Dummies Questions & Answers

Rearranging whole columns

Hello all, I have a text file that is arranged: name 3 7 2 9 5 jim a d e g k max d g u x g rob f w v k o This is just an example as my real file has >1000 individuals and >64,000 columns. I need to rearrange the file so that the columns appear in numerical order so that name... (3 Replies)
Discussion started by: doobedoo
3 Replies

9. Shell Programming and Scripting

Rearranging columns

Hi, I have an input file as follows : input.txt abcdTXXqwe axdfSYYrew dasgTXXqwt gtfsTYYwer gadfSXXerw gwerSYYTXX Now I have to get four output files. output1.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as XX output2.txt... (5 Replies)
Discussion started by: sudhamacs
5 Replies

10. Shell Programming and Scripting

rearranging the data in file (from columnwise to rowwise)

Hi I have one file which is having data like 10201 10202 10205 10206 10207 10208 10209 10210 10211 10213 10215 10801 10802 11406 11415 11422 11426 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11530 11604 11608 11611 11717 11718 11719 11722 11725... (3 Replies)
Discussion started by: reldb
3 Replies
Login or Register to Ask a Question