help with a script

Hi All!

I am new into unix (solaris) can you you help in writing a script to remove the last 3 characters of all files in a specified directory?

Your help will be appreciated

FR

Please show us what you've tried so far.

Code to remove last three chars from a file

sed 's/\(.*\)...$/\1/g' input_file > output_file

Extend it for your requirement!

--ahamed

Hi !

Thank you for your quick reply.
perhaps I did not explain myself clearly, what I meant was to remove the last characters of all FILENAMEs in a directory, NOT the last 3 characters of the contents of the file.

$ ls -ltr | sed 's,...$,,g'

Hi,
You can try with below code

ls -1 |  sed 's,...$,,g'

Regards,
Haris

Hi
It only works when you apply the "sed" code, once you do ls -lrt, the files are back to what they were initially

ls -lrt
total 17504
-rw-r--r--   1 root       sys        4191536 Aug 24 15:49 EMSCH2-RTR-0822-0812.6873:00
-rw-r--r--   1 root       sys            606 Oct 12 12:04 nd0_jan.conf
-rw-r--r--   1 root       sys         555722 Oct 12 12:04 nd0_jan.txt
-rw-r--r--   1 root       sys            549 Oct 12 12:04 nd0_jan1.conf
-rw-r--r--   1 root       sys        4191536 Oct 12 13:50 EMSC5-RTR-0822-0812.6873:00
prep03[296]/tmp/fr/testing #ls -ltr | sed 's,...$,,g'
total 17
-rw-r--r--   1 root       sys        4191536 Aug 24 15:49 EMSCH2-RTR-0822-0812.6873
-rw-r--r--   1 root       sys            606 Oct 12 12:04 nd0_jan.c
-rw-r--r--   1 root       sys         555722 Oct 12 12:04 nd0_jan.
-rw-r--r--   1 root       sys            549 Oct 12 12:04 nd0_jan1.c
-rw-r--r--   1 root       sys        4191536 Oct 12 13:50 EMSC5-RTR-0822-0812.6873
prep03[297]/tmp/fr/testing #ls -lrt
total 17504
-rw-r--r--   1 root       sys        4191536 Aug 24 15:49 EMSCH2-RTR-0822-0812.6873:00
-rw-r--r--   1 root       sys            606 Oct 12 12:04 nd0_jan.conf
-rw-r--r--   1 root       sys         555722 Oct 12 12:04 nd0_jan.txt
-rw-r--r--   1 root       sys            549 Oct 12 12:04 nd0_jan1.conf
-rw-r--r--   1 root       sys        4191536 Oct 12 13:50 EMSC5-RTR-0822-0812.6873:00

Try this first without the colored part:

ls | sed 's/\(.*\).../mv & \1/' | sh

HI Franklin52!

Your tip it worked fine. thanks a lot

FR

---------- Post updated at 03:02 PM ---------- Previous update was at 02:59 PM ----------

Hi Franklin52

only worked when I added " | sh " bit

Indeed, to move the files actually you have to pipe the commands to sh.