Change line end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change line end
# 1  
Old 02-05-2018
Change line end

Hello all,

what I would like to have sounds quite simple...

I have a file where the records have a line ending like (CR, LF, CR/LF, LFLF, ...) - so I do not know.

In result I would like to have a file with UNIX line endings -> LF

Maybe I can do it with a sed - but I do not know how to tell the sed that he should take the complete line ending and change it to a LF?

---------- Post updated at 03:19 PM ---------- Previous update was at 03:18 PM ----------

The file could have different line-endings!
# 2  
Old 02-05-2018
Sure a <CR> means a line end in your file? While <LF> (*nix) and <CR><LF> (DOS / Windows) usually do, a <CR> alone frequently means a "local line break" in a field. And <LF><LF> can stand for two lines, one with a text and one empty.

Some sample input data would halp as well.
# 3  
Old 02-05-2018
Normally each record within a file have a line end like <LF>. But sometimes some lines will be changed at Windows platform. Therefor it could happen that you have all different possibilities - as described above.

To place it here in a code-Fragment is not possible, because you won't see the line ends, or?

But the a possible file is like this:

Code:
Discont;23.98;34
Discont;23.09;34
Discont;11.09;54
Discont;11.43;66

or with line visible line Ends:

Code:
Discont;23.98;34\n
 Discont;23.09;34\r\n
 Discont;11.09;54\n
 Discont;11.43;66\n\r\r

The result should be:

Code:
Discont;23.98;34\n
 Discont;23.09;34\n
 Discont;11.09;54\n
 Discont;11.43;66\n

# 4  
Old 02-05-2018
Try
Code:
sed 's/\r//g' file

OR

Code:
sed  's/^M//g' file

The ^M represents the <CR> (single!) char and can be produced (at least in my shell) by pressing ^V (Ctrl V) followed by ^M (Ctrl M).
# 5  
Old 02-05-2018
Yes, you are right. As I see my examples - it would work.

But, what if I have a line - like (I forgot this case):

Code:
Discont;23.09;34\n\r
 Discont;11.09;54\r
 Discont;11.43;66\n

# 6  
Old 02-05-2018
How about
Code:
sed 's/\r\+/\n/g; s/^\n\|\n$//g' file

# 7  
Old 02-05-2018
Thats very good. It seems to be working...
But I have a question to the second command:
Code:
s/^\n\|\n$//g'

You are replacing all LFs to nothing? Then no LF would be there anymore? Ok, it works, but I do not understand it...

Last edited by API; 02-05-2018 at 11:40 AM.. Reason: Wrong language
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

3. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

4. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

5. Programming

concatinate all lines from second line to end of line in perl

I have datafile like ~dta.yunm shhshsgggssssjsksjs sggsjshsjsjssss shshshhshshshs i wish to take only first line and all other lines in concatenated form in second line what should I do??? output like ~dta.yunm shhshsgggssssjsksjssggsjshsjsjssssshshshhshshshs please tell me how... (7 Replies)
Discussion started by: sujit_singh
7 Replies

6. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

7. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

8. Shell Programming and Scripting

Change to user root without end the script

Hello there! I need help: I have this script: #!/bin/bash #chage to root user sudo su #Insert actual date echo -n "Ingrese fecha actual: " #Read the actual date read fecha #clean RAM memory sync ; echo 3 > /proc/sys/vm/drop_caches ; swapoff -a && swapon -a #backup opennms... (2 Replies)
Discussion started by: bobbasystem
2 Replies

9. Shell Programming and Scripting

Using sed command to change end of line

I am looking to change a data file into a javascript string and this is the code that I am using: sed -i '' -e 's/^/str += "/' -e 's/$/";/' file.xml The first part -e 's/^/str += "/' works as intended, but the second part -e 's/$/";/' adds an additional newline to my file, so that instead of... (3 Replies)
Discussion started by: figaro
3 Replies

10. Solaris

change hostname end ip adresse in solaris 9

hi everybody which files i should change to modify my hostname and my adresse ip in solaris 9 . or how can i do that :confused: it's very important to me thnx (2 Replies)
Discussion started by: lid-j-one
2 Replies
Login or Register to Ask a Question