LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2018, 10:08 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
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?
 
Old 09-21-2018, 10:15 AM   #2
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,109

Rep: Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178Reputation: 4178
IMHO, this is better

https://slackbuilds.org/repository/14.2/misc/dos2unix/

there is also one version embedded in busybox that you should have already available if you are using an initrd at /boot/initrd-tree/bin/dos2unix
 
1 members found this post helpful.
Old 09-21-2018, 11:53 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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?
 
Old 09-21-2018, 12:18 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Yes, sed or tr can do the job. There are some recent LQ threads about that as well as plenty of info on the web about it.

Have you looked this up or tried any command attempts?
 
Old 09-21-2018, 12:26 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
But any average linux user can solve the problem with tr in a matter of a couple of minutes. Why to be so hermetic?
 
Old 09-21-2018, 12:35 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,661

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by stf92 View Post
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.
 
Old 09-21-2018, 12:40 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by stf92 View Post
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 View Post
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.
 
1 members found this post helpful.
Old 09-21-2018, 12:48 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,237

Rep: Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321
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.

Last edited by dugan; 09-21-2018 at 12:52 PM.
 
2 members found this post helpful.
Old 09-21-2018, 01:13 PM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by rtmistler View Post
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?

Last edited by stf92; 09-21-2018 at 01:15 PM.
 
Old 09-21-2018, 01:20 PM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,661

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by stf92 View Post
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.

Not unlike things you've been asked before:
https://www.linuxquestions.org/quest...pl-4175630651/
https://www.linuxquestions.org/quest...nk-4175630650/

Last edited by TB0ne; 09-21-2018 at 01:22 PM.
 
1 members found this post helpful.
Old 09-21-2018, 01:33 PM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by stf92 View Post
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.
Sir or madam, just use the dos2unix command.

Or as Dugan said:
Quote:
Originally Posted by dugan View Post
Slackware includes a program called fromdos, which can do this.
 
1 members found this post helpful.
Old 09-21-2018, 01:39 PM   #12
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559

Rep: Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106
Quote:
Originally Posted by stf92 View Post
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
  1. did your homework first
  2. showed us the commands you tried and how they failed
  3. 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.
 
3 members found this post helpful.
Old 09-21-2018, 02:44 PM   #13
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,237

Rep: Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321Reputation: 5321
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.
https://www.systutorials.com/docs/linux/man/1-fromdos/
 
Old 09-21-2018, 08:23 PM   #14
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by stf92 View Post
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.

Last edited by stf92; 09-22-2018 at 12:44 AM.
 
Old 09-21-2018, 09:01 PM   #15
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,667

Rep: Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711
Quote:
Originally Posted by rtmistler View Post
Sir or madam, just use the dos2unix command.
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?
 
  


Reply



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
[SOLVED] Marking a partition bootable (MS-DOS style)? stf92 Slackware 10 08-27-2017 09:06 PM
convert DOS style nardo Linux - Newbie 3 05-20-2011 01:48 PM
scripting to convert DOS '\' to UNIX style slashes poonamkhirolia Linux - Newbie 2 12-17-2010 09:35 AM
e.g., BSD style (Slackware) vs. SystemV style startup scripts haertig Slackware 5 01-03-2009 10:52 PM
Compiling kernel Debian style or Native style ? Raynus Debian 1 06-16-2008 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 AM.

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
Open Source Consulting | Domain Registration