LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-19-2018, 08:46 AM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Adding CR LF


Is there a small script I can add to an existing script to add CR LF at the end of every row, including the header row?
 
Old 09-19-2018, 08:52 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
There are plenty of text manipulation tools such as awk, tr, and sed which can modify a file in the manner you describe. I would choose sed based on my stronger experience with it.

What is your experience with scripting, as well as any of these tools?

Have you attempted anything as yet?

The most common notations for control characters is to write them in octal, such as \015 and \012, when specifying them to the command to perform the modification.
 
1 members found this post helpful.
Old 09-19-2018, 08:53 AM   #3
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I've done some scripting but not much. Don't know where or how to start
 
Old 09-19-2018, 09:01 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
First I suggest a web search using the term, "append characters to the end of line using sed"

As common as I have used sed, I myself had to look it up.

I typically do search/replace versus add stuff. The hits I've seen though seem very reasonable and I've learned about the $ character for use in search-replace.

Next, suggest you consider reading the My Bash Blog link here, or same from my signature.

While I wrote that to help people debug bash scripts, please note that in the introduction for it I give links to the standard Bash scripting guides for basic and advanced, and I also make the comment that, "Whatever you can type on a command line, you can write in a script." The meaning there is that you can use sed, tr, or awk in the command line to try things out and then you can choose to put that command into a small script, if that is your choice. Noting also that using something like sed, the output typically goes to stdout, thus not modifying your file, and once you have the tuning of the command correct for your requirements, you can use the ">" Input-Ouput redirector to redirect stdout to a final result file where you have the added CR-LF as you wish it to be.
 
1 members found this post helpful.
Old 09-19-2018, 09:04 AM   #5
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I seen something like this....

# sed 's/$/\r/g' file>newfile

but I don't know the functionality of it. I would be inserting code after the file has been moved.
 
Old 09-19-2018, 09:05 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,696

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
In addition, adding CR,LF to the end of lines is basically converting a text file from Unix to DOS format. Searching on how to convert a text file from Unix to DOS format should provide some help.

As stated there are a few ways using tr, sed, awk, perl or the unix2dos script which is typically available in the repositories in most of the major distributions.

Quote:
I would be inserting code after the file has been moved.
Please explain and what type of text file you trying to convert?

Last edited by michaelk; 09-19-2018 at 09:08 AM.
 
2 members found this post helpful.
Old 09-19-2018, 09:06 AM   #7
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Instead of just going to output I would need the file modified
 
Old 09-19-2018, 09:11 AM   #8
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Why doesn't the file already have CR+LF endings? Is it a Linux file? If so, there are applications to convert the file if that is what you want to do.

Can you please give us some more background on what it is that you are doing. Context is king.
 
1 members found this post helpful.
Old 09-19-2018, 09:14 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by trickydba View Post
I seen something like this....

# sed 's/$/\r/g' file>newfile

but I don't know the functionality of it. I would be inserting code after the file has been moved.
Well you should test it. This doesn't change your original file and you can try it several different ways.

You need to read about the sed command.
 
1 members found this post helpful.
Old 09-19-2018, 09:15 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by trickydba View Post
Is there a small script I can add to an existing script to add CR LF at the end of every row, including the header row?
...and...
Quote:
Originally Posted by trickydba
I've done some scripting but not much. Don't know where or how to start
This seems to be a recurring theme; I'm sorry, but you've been asking about scripting for two years now. And this thread:
https://www.linuxquestions.org/quest...er-4175593953/

...from 2016 asks something nearly identical to this. Do you not know where to start after that long asking for/about scripts?

Last edited by TB0ne; 09-19-2018 at 09:59 AM.
 
2 members found this post helpful.
Old 09-19-2018, 10:23 AM   #11
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I apologize for not looking at my previous threads to prevent repeating questions. I'll try to do better.

I did try this but it only added CRLF on the first line:
[QUOTE]
sed -i 'ls/$/\r/' <filename>
[/QUOTE
 
1 members found this post helpful.
Old 09-19-2018, 10:35 AM   #12
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I tried this....

sed -e 'ls/$/\r/' text*

but got an error

sed: -e expression #1, char 2: extra characters after command
 
Old 09-19-2018, 11:02 AM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by trickydba View Post
I tried this....

sed -e 'ls/$/\r/' text*

but got an error

sed: -e expression #1, char 2: extra characters after command
Leave the -e out of that command.


Do not use wildcard characters for now.


Have you confirmed if these are Unix file types and that all you wish to do is convert them to DOS file types? If so, why aren't you considering the utility unix2dos?
 
1 members found this post helpful.
Old 09-19-2018, 11:05 AM   #14
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by rtmistler View Post
Have you confirmed if these are Unix file types and that all you wish to do is convert them to DOS file types? If so, why aren't you considering the utility unix2dos?
To be honest with you, I'm not expecting trickydba to answer my question about this - he/she appears to have ignored my question and the previous comment on the same subject completely.
 
2 members found this post helpful.
Old 09-19-2018, 11:52 AM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
two ways:
unix2dos -- see the man page
(s)ftp ascii file transfer from a (l)unix 'puter to a windows 'puter.

Better yet, try this.

Last edited by scasey; 09-19-2018 at 11:54 AM.
 
1 members found this post helpful.
  


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
Bash script for adding users is running but not adding users Gren Programming 2 04-27-2012 03:53 AM
Adding HD Flab0y352 Linux - General 3 08-13-2007 02:25 AM
Adding more SWAP after adding memory marc1978 Linux - General 6 03-19-2006 07:13 PM
Adding a new HD hywaydave Linux - Newbie 4 05-02-2005 03:22 PM
Adding 2.4.20 to 9.1 datamile Slackware 3 06-08-2004 10:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:38 PM.

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