[SOLVED] Unix style to ms-dos style line terminator one-liner converter.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Unix style to ms-dos style line terminator one-liner converter.
Hi: I want to convert a whole batch of plain ASCII text files, so the vim command would be a little too much work. Sed or tr, preferably the latter, could do the job, I think. Exactly how?
Let's say 'foo file_1 file_2' converts file_1 into file_2 and I have these files in directory ./
Code:
$ ls -1
text01.unix
..........
textNN.unix
$
Then I need to use foo only once and produce the output
Code:
$ ls -1 *dos
text01.dos
.........
textNN.dos
$
Pipes and wildcards are a solution. BUT dos2unix won't allow me the use of wildcards (-n option). Plus, why use such a complex program as its manual shows it is when tr can accomplish the same job in the most straightforward way?
But any average linux user can solve the problem with tr in a matter of a couple of minutes. Why to be so hermetic?
No idea what you mean by "hermetic" in this context. And yes, any 'average linux user' can solve this in a couple of minutes with either sed or tr. So since you know that, why ask? There are many ways, from a simple 'for' type loop, to running a find and piping it into 'exec dos2unix'.
As rtmistler asked, have you looked this up or tried any command attempts?? See the "Question Guidelines" link in my posting signature.
But any average linux user can solve the problem with tr in a matter of a couple of minutes. Why to be so hermetic?
Is this in reply to my post, #4? Either case, perhaps you could explain what your concern is more clearly. Are you not an average Linux user yourself?
For myself, I was answering your original question where you said you'd prefer "the latter", meaning tr.
Quote:
Originally Posted by stf92
Hi: I want to convert a whole batch of plain ASCII text files, so the vim command would be a little too much work. Sed or tr, preferably the latter, could do the job, I think. Exactly how?
If your concern is that myself or someone else hasn't ejected the answer directly for you, I'll refer to the LQ Guidelines about how to ask an effective question and note that you haven't described what you've tried and where you are stuck. Instead you appear to have asked for a handout of a command which you could easily look up, if not derive by some command line experimentation.
We're not paid support, or a help desk, we are all the same volunteers as you are.
Slackware includes a program called fromdos, which can do this.
I'd do the globbing with the shell, with "find", or with xargs (in decreasing order of preference), if fromdos itself doesn't take wildcards. It would take me much less than "a couple of minutes". But then I'm not exactly what counts as an "average Linux user" these days.
For myself, I was answering your original question where you said you'd prefer "the latter", meaning tr.If your concern is that myself or someone else hasn't ejected the answer directly for you, I'll refer to the LQ Guidelines about how to ask an effective question and note that you haven't described what you've tried and where you are stuck. Instead you appear to have asked for a handout of a command which you could easily look up, if not derive by some command line experimentation.
That is precisely the point. If I see what the poor guy needs is the ls command, why not tell him: Sir, just do an ls. Are we in a classroom?
Anyway, the problem as stated in post #3 can be broken into two parts. Part (a) Implement the foo command. This will be a certain sequence of linux commands, including tr.
Part (b)
Process all files in this directory ending in .unix using the foo command.
I feel more confident with part (b) and will try to provide a solution (to (b)). Provided someone cares to give me the part (a) solution. Is that fair game?
That is precisely the point. If I see what the poor guy needs is the ls command, why not tell him: Sir, just do an ls. Are we in a classroom?
Nope; because there you'd get told to use your textbook. Here, you agreed to the LQ Rules and "Question Guidelines", that indicate your willingness to do basic research first, and not just ask for handouts.
Quote:
Anyway, the problem as stated in post #3 can be broken into two parts. Part (a) Implement the foo command. This will be a certain sequence of linux commands, including tr.
Part (b)
Process all files in this directory ending in .unix using the foo command.
I feel more confident with part (b) and will try to provide a solution (to (b)). Provided someone cares to give me the part (a) solution. Is that fair game?
No, sorry. There are *THOUSANDS* of examples on how to batch-run commands. You've even been handed their names/examples (as in a "for" loop, piping a find into "exec", etc.), but haven't tried any of them. We see what you need and you were told what to use; past that, it is now up to *YOU* to do/try things.
Hi: I want to convert a whole batch of plain ASCII text files, so the vim command would be a little too much work. Sed or tr, preferably the latter, could do the job, I think. Exactly how?
With 3,890 posts on this forum, one might hope that you
did your homework first
showed us the commands you tried and how they failed
showed at least a rudimentary willingness to start learning
I see none of the above. Can you please try on all three counts? We'll take it from there.
I'm not on a Slackware machine right now, but I assume this applies:
Quote:
fromdos converts ASCII and Unicode UTF-8 text files from the DOS format to the Unix format, while todos converts them from the Unix format to the DOS format.
The programs accept multiple filenames and wildcards as their arguments.
Anyway, the problem as stated in post #3 can be broken into two parts. Part (a) Implement the foo command. This will be a certain sequence of linux commands, including tr.
Part (b)
Process all files in this directory ending in .unix using the foo command.
Part (a) seems simple:
1. Read next char
2. If the char is not equal to \n write it
else write \r\n
3. Goto 1.
The file is treated sequentially, and tr can do this. So, reading the manual is mandatory.
As to part (b) I presently will see what I can do, assuming foo is up and running.
I agree. Why cover all of the tools that could be used to BUILD a solution when the solution in hand is "use the right tool"? There are tools for this purpose, two have been recommended. There is no need to "reinvent the horse". WHY BOTHER, when the tool has already been built, tested, optimized, and folded into the repos for distribution more than ten years ago?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.