LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-06-2011, 12:48 PM   #1
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Rep: Reputation: 3
Split and rename question


I am trying to rename several files after I use a split command on a larger file but I am having trouble with how to accomplish this. My split looks like this:

Code:
split -l 1000 -a 3 file.A1jan.2011.txt file.A1jan.2011.txt
it spits out:

file.A1jan.2011.txtaaa
file.A1jan.2011.txtaab
file.A1jan.2011.txtaac
file.A1jan.2011.txtaad
etc.

I am wanting to rename the files so that they are .txt files but I am wanting to move the first digit and the last digit of the AAA, AAB's to within the file name so that I will know which split it is from. For example the new output name would be:

file.AAjan.2011.txt
file.ABjan.2011.txt

is this something that can be accomplished?

Thanks for any help.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-06-2011, 01:11 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hello coach5779, welcome to LQ,

you may use the rename command
Code:
rename 2011.txt 2011. file.A1*
which deletes the "txt" part and then
Code:
for i in file.A1*; do mv $i $i.txt; done
which appends ".txt" to the files.

Markus
 
Old 01-06-2011, 01:16 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Hi and welcome to LinuxQuestions!
Quote:
Originally Posted by coach5779 View Post
I am wanting to rename the files so that they are .txt files but I am wanting to move the first digit and the last digit of the AAA, AAB's to within the file name so that I will know which split it is from.
Why do you want the first and the third character of the suffix? If there are a lot of pieces, you might end up with files having the same name (therefore overwritten). For example:
Code:
AAA
ABA
ACA
give the same suffix AA instead of AA, BA, CA respectively. Anyway, if you're interested in a 2-digits suffix, use option -a2.

To answer your question, there is not a way to insert the suffix inside the file name using split options. You can change the file name using something like:
Code:
for file in file.A1jan.2011.txt???
do
  suffix=${file##*.txt[a-z]}
  echo mv $file file.${suffix^^}.jan.2011.txt; done
done
This assumes a three digits suffix (as per your example) and the second and third digit extracted. Please note that the lower to uppercase conversion (highlighted in blue) is available since bash version 4. The echo statement is for testing purposes: remove it after you've checked the results and execute the loop again. Hope this helps.
 
Old 01-06-2011, 04:33 PM   #4
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 3
Thanks for the replies. I was able to get it to work using this code:

Code:
i=1
for file in file.A1jan.2011.txt???
do
DT=2011
;
   echo mv $file file.A${i}jan.${DT}.custi; i=$(($i+1)); done
However, when it gets to the 10th file it renames the file A10 . Is there a way to increment the letter (perhaps an if statement or another substitution parameter?) to make it B1-B9 for the 10th-19th files, C1-C9 for the 20th-29th, etc?

Last edited by coach5779; 01-06-2011 at 04:35 PM.
 
Old 01-06-2011, 05:15 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by coach5779 View Post
to make it B1-B9 for the 10th-19th files, C1-C9 for the 20th-29th, etc?
This does not make sense: from B1 to B9 they are 9 items, from 10th to 19th they are 10 items. What do you want to do exactly? I would stick with translating the suffix created by the split command, since it does the job for you.
 
Old 01-06-2011, 05:42 PM   #6
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 3
Well I tried to use the suffix like you said but I am getting the following:

ksh[3]: suffix=${file##*.txt[a-z]}: 0403-011 The specified substitution is not valid for this command.

I don't necessarily have to have the 1st and 3rd digits from the suffix. I know that my largest file will never be broken up in to more than 30 files and I figured it would be easier to have one be static and change the other as a 2 digit runcode.

What I am wanting is for a file with say 300,000 lines split into files of 10,000 records (it would give me 30 files). I would like my output to be something like this:

Files 1 - 9

file.A1jan.2011.txtaaa -> file.A1jan.2011.txt
file.A1jan.2011.txtaab -> file.A2jan.2011.txt
file.A1jan.2011.txtaac -> file.A3jan.2011.txt
file.A1jan.2011.txtaad -> file.A4jan.2011.txt
file.A1jan.2011.txtaae -> file.A5jan.2011.txt
file.A1jan.2011.txtaaf -> file.A6jan.2011.txt
file.A1jan.2011.txtaag -> file.A7jan.2011.txt
file.A1jan.2011.txtaah -> file.A8jan.2011.txt
file.A1jan.2011.txtaai -> file.A9jan.2011.txt

Files 10 - 18
file.A1jan.2011.txtaaj -> file.B1jan.2011.txt
file.A1jan.2011.txtaak -> file.B2jan.2011.txt
file.A1jan.2011.txtaal -> file.B3jan.2011.txt
file.A1jan.2011.txtaam -> file.B4jan.2011.txt
file.A1jan.2011.txtaan -> file.B5jan.2011.txt
file.A1jan.2011.txtaao -> file.B6jan.2011.txt
file.A1jan.2011.txtaap -> file.B7jan.2011.txt
file.A1jan.2011.txtaaq -> file.B8jan.2011.txt
file.A1jan.2011.txtaar -> file.B9jan.2011.txt

etc.

Sorry for the confusion. I am trying to figure this out as I go.
 
Old 01-06-2011, 06:04 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Ok. To get A1, A2, A3 and so on you can use hexadecimal numbers. Given decimal/hexadecimal equivalence:
Code:
161 = A1
162 = A2
...
169 = A9
177 = B1
178 = B2
...
185 = B9
193 = C1
...
you can try something like this:
Code:
#!/bin/bash
ini=161
i=161
DT=2011
for file in file.A1jan.$DT.txt???
do
  echo mv $file file.$(printf '%X' $i)jan.${DT}.txt
  i=$((i + 1 + 7*($i-$ini==8)))
  ini=$((ini + 16*($i-$ini>8)))
done
The printf statement highlighted in blue performs the decimal/hexadecimal conversion with uppercase letters. The grey items are conditioned from the difference of the current value $i and the initial value $ini, in order to switch from 169 to 177, from 185 to 193 and so on. The initial value $ini is updated accordingly.
 
2 members found this post helpful.
Old 01-06-2011, 06:56 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
I would probably point out that your error:
Quote:
ksh[3]: suffix=${file##*.txt[a-z]}: 0403-011 The specified substitution is not valid for this command.
Implies you are using korn shell. This information would probably change some of the advice given so far.
 
1 members found this post helpful.
Old 01-06-2011, 08:24 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by grail View Post
I would probably point out that your error:

Implies you are using korn shell. This information would probably change some of the advice given so far.
Ops.. I didn't notice that. Thank you, grail. The script suggested in my previous post should work in Korn Shell as well.
 
Old 01-06-2011, 08:40 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
@colucix - nice solution and I realise it works out the same, but here is another solution for 'i':
Code:
((i += 1 + 7 * (1 - ((i - 169) % 16 != 0))))
 
Old 01-06-2011, 09:57 PM   #11
coach5779
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 3
This works exactly like I need it. Thanks for the help.
 
  


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
tar + split question 300zxkyle Linux - Software 9 01-14-2012 07:19 AM
dd question - split files Sn7 Linux - Software 5 08-30-2009 05:42 AM
Kernel rename Question. bostonthunder777 Linux - Server 4 01-12-2008 01:55 PM
dd question - split files Sn7 Linux - General 1 09-19-2004 11:15 AM
Batch rename question hellblade Linux - Software 4 05-03-2004 04:57 PM


All times are GMT -5. The time now is 08:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration