Cutting and renaming files

I have some files such as:

abc.dat.xyz1234.001
abc.dat.xyz1234.002
abc.dat.xyz1234.003
and so on.

The files are pipe delimited as:
1|abc|hty|!
2|def|hju|!
and so on.

I would like to remove the first column value and the delimiter from all rows in the file. So the files need to look like this:
abc|hty|!
def|hju|!

I know I can do it using cut but I also need to rename the files as:
abc.dat.xyz1234.001 becomes abc.dat.00
abc.dat.xyz1234.002 becomes abc.dat.01
and so on.

Most important, the number of files is not fixed. The only difference between the file names is the last digit. Is there a simple way to do this?
soccerplayerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
Could you clarify an ambiguity in your request?

The two-digit string you asked for at the end of the new file name, is that simply sequential? Or is it expected to be mathematically related (as in: "one less than the numerical value of") to the final digit sequence of the old file?
soccerplayerAuthor Commented:
I am not clear about your question but the files need to be:
00
01
02
and so on.

The files comes in as:
001
002
003
so the output is one less.
It is sequential with 3 digits when they are coming. They need to be increasing sequential starting with 00 (two digits) when they are changed.

Does that answer your question?
ozoCommented:
#!bin/bash
n=100;
for f in abc.dat.xyz1234.??? ; do cut -d'|' -f 2- $f>${f%.*}.${n:1}; let ++n ; done
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

soccerplayerAuthor Commented:
ozo, thank you for your solution. It almost works but I need to rename the files as:

abc.dat.xyz1234.001 becomes abc.dat.00
abc.dat.xyz1234.002 becomes abc.dat.01

After running your script, the files are becoming:
abc.dat.xyz1234.00
abc.dat.xyz1234.01

They need to be:
 abc.dat.00
 abc.dat.01

I mentioned this in my first post.
ozoCommented:
for f in abc.dat.xyz1234.??? ; do cut -d'|' -f 2- $f>${f%.*.*}.${n:1}; let ++n ; done

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
soccerplayerAuthor Commented:
Thanks.
soccerplayerAuthor Commented:
ozo, can you please explain your solution a little?

for f in abc.dat.xyz1234.??? ; do cut -d'|' -f 2- $f>${f%.*.*}.${n:1}; let ++n ; done

I understand this part:
for f in abc.dat.xyz1234.??? ;
You are asking it to go through all files with the 3 number wildcard at the end.

Can you please explain this part?
do cut -d'|' -f 2- $f>${f%.*.*}.${n:1};

Thanks.
ozoCommented:
${f%.*.*} is $f with the last .*.* pathname expansion removed
${n:1} is $n with the first character removed
soccerplayerAuthor Commented:
ozo, thank you very much for your response.

So in this example:
abc.dat.xyz1234.001
$f = abc.dat.xyz1234.001

1) do cut -d'|'  
means for every row remove the | and anything to its left, right?

2)  What is the
-f 2-
for?

3)$(n:1)
How does this become 00?

4)
$f>${f%.*.*}
means  abc.dat.xyz1234.001 becomes abc.dat, right? Does the % remove the .*.*?

5)
let ++n
means increment the file counter, right? What is the counter value that it starts with?

Thanks a lot.
ozoCommented:
man cut
...
     The list option argument is a comma or whitespace separated set of numbers and/or number ranges.  Number ranges consist of a number, a dash (`-'), and a second number and select the fields or columns from the first
     number to the second, inclusive.  Numbers or number ranges may be preceded by a dash, which selects all fields or columns from 1 to the last number.  Numbers or number ranges may be followed by a dash, which selects
     all fields or columns from the last number to the end of the line.
...
     -f list
             The list specifies fields, separated in the input by the field delimiter character (see the -d option.)  Output fields are separated by a single occurrence of the field delimiter character.
ozoCommented:
#!bin/bash
n=100
for f in abc.dat.xyz1234.??? ; do cut -d'|' -f 2- $f>${f%.*.*}.${n:1}; let ++n ; done
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.