1

I am not a UNIX guy but understand enough to gin up small scripts here and there except for this one where I may personally be not able to devote enough time.

I have about 2000 files in a directory on which the following need to be done:

  1. Each file has about 3000 records but all are on one line of each file separated by Ctrl M newline character. These need to be separated out.
  2. Each file has corresponding date it was generated in the first line starting from 8th char to 14th char. This date needs to be used to rename the file to XXX_YYYYMMDD_AAA.txt
  • "These need to be separated out" Would you clarify? Do you want the ^M removed or what? – John1024 Oct 5 '14 at 22:44
1

Most systems include a tool called dos2unix which you can "gin up" in a script to process the files that you need to perform this operation on.

If the files are all in a directory you can use find to locate them and then operate on each one individually like so:

$ find . -type f -exec dos2unix {} +

Example

Say I had this directory structure

$ tree
.
|-- afile
|-- dir1
|   `-- afile
`-- dir2
    `-- afile

We can use our find technique to confirm that all the files are "DOS" files with the CRLF line termination.

$ find . -type f -exec file {} +
./dir2/afile: ASCII text, with CRLF line terminators
./afile:      ASCII text, with CRLF line terminators
./dir1/afile: ASCII text, with CRLF line terminators

And repair everything like so:

$ find . -type f -exec dos2unix {} +
dos2unix: converting file ./dir2/afile to Unix format ...
dos2unix: converting file ./afile to Unix format ...
dos2unix: converting file ./dir1/afile to Unix format ...

Resulting in just Unix files:

$ find . -type f -exec file {} +
./dir2/afile: ASCII text
./afile:      ASCII text
./dir1/afile: ASCII text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.