awk Command to add Carriage Return and Line Feed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk Command to add Carriage Return and Line Feed
# 1  
Old 10-15-2018
awk Command to add Carriage Return and Line Feed

Hello,

Can someone please share a Simple AWK command to append Carriage Return & Line Feed to the end of the file, If the Carriage Return & Line Feed does not exist !

Thanks
# 2  
Old 10-15-2018
awk is not a good language for that, being it's not guaranteed to work at all on unterminated lines.

What have you tried?
# 3  
Old 10-15-2018
Hi,

I tried this , I am using cat command which is not efficient


Code:
cat source.txt | sed 's/[^[:print:]\r\t]//g' | perl -lpe 's/\x0d$//' > output.txt

# 4  
Old 10-15-2018
Why not the sed version?


Code:
sed '/\r$/!s/$/\r/' source.txt


or



Code:
sed 's/\r*$/\r/' source.txt

This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-16-2018
With awk, try:
Code:
awk '{sub(/\r?$/,"\r")}1' file


--
Note: With sed, the use of \r with the s command is not standard, it is GNU sed only.
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-16-2018
Unix text files have only a linefeed character to indicate a newline.
Perhaps you mean "add a missing linefeed to the end of the file"?
The consider this shell script
Code:
#!/bin/sh
# append a missing last newline
prtstdin(){
  while IFS= read line; do echo "$line"; done
  [ -z "$line" ]] || echo "$line"
}
if [ $# -eq 0 ]
then
  prtstdin
else
  for arg
  do
    prtstdin  <"$arg"
  done
fi

The script can be used like the "cat" program.
# 7  
Old 10-16-2018
Why not use the unix2dos and dos2unix programs or (more generally) recode? They were explicitly designed for the job you (mis-)use other innocent programs for.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Losing carriage return (X0D) after running awk command

Hi Forum. I'm running the following awk command to extract the suffix value (pos 38) from the "AM00" record and append to the end of the "AM01" record. awk 'substr($0,13,4)=="AM00" {SUFFIX = substr($0,38,2)} substr($0,13,4)=="AM01" {$0 = $0 SUFFIX} 1' before.txt > after.txt Before.txt:... (2 Replies)
Discussion started by: pchang
2 Replies

2. Shell Programming and Scripting

line carriage return characters

Hi, I would like to insert the line carriage retrun characters on each line. (2 Replies)
Discussion started by: koti_rama
2 Replies

3. Shell Programming and Scripting

refine awk command in replacing carriage return

Hi, need your help in below,I have 4 types of file need to be processed so that it will replace carriage return in Remarks column with <:::> Remarks column position may varies in different types of file. sample file: col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10... (8 Replies)
Discussion started by: agathaeleanor
8 Replies

4. Shell Programming and Scripting

Why sed command deletes last line in a file if no carriage return?

Hi I am using sed command to make SCORE=somevalue to SCORE=blank in a file. Please see the attached lastline.txt file. After executing the below command on the file, it removes the last line. cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt Why does sed command remove the... (3 Replies)
Discussion started by: ashok.k
3 Replies

5. Programming

Carriage return or line feed issues

I keep running into the same problem with the following script. Every time it prints the carrage (line feed) char when I test. I believe that the issue is in the group by but I do not see it. The code is as follows. SET FEED OFF SET ECHO OFF SET HEADING OFF SET LINESIZE 1000 SET PAGESIZE... (1 Reply)
Discussion started by: sherrod6970
1 Replies

6. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

7. UNIX for Dummies Questions & Answers

To remove carriage return between the line

Hi, I have a situation where I need to remove the carriage return between the lines. For.eg. The input file: 1,ad,"adc sdfd",edf 2,asd,"def fde",asd The output file should be 1,ad,adc sdfd,edf 2,asd,def fde,asd Thanks Shash (5 Replies)
Discussion started by: shash
5 Replies

8. Shell Programming and Scripting

How to insert carriage return before line feed?

I am doing some edi where translations had to be run on unix. Generally when I run the translations on windows, the output file has both carriage returns and line feed where as when ran on unix will have only line feed. I need to insert carriage return before the line feed. Is there some tool... (2 Replies)
Discussion started by: huey ing
2 Replies

9. Shell Programming and Scripting

Removing Carriage Return and or line feed from a file

Hello I'm trying to write a shell script which can remove a carriage return and/or line feed from a file, so the resulting file all ends up on one line. So, I begin with a file like this text in file!<CR> line two!<CR> line three!<CR> END!<CR> And I want to end up with a file... (1 Reply)
Discussion started by: tbone231
1 Replies

10. Shell Programming and Scripting

carriage return/line feeds

Hello, I have a file that has got carriage returns in it and I want to take them out. Anyone know how I can do this in a ksh? thanks (4 Replies)
Discussion started by: pitstop
4 Replies
Login or Register to Ask a Question