Adding end of line character in the last record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding end of line character in the last record
# 1  
Old 03-15-2013
Adding end of line character in the last record

Need to add end of line character to last record in a fixed width file.

When i take the record length of each line, for all the records it gives example like 200 except the last line as 199.

Due to this my other script fails.

Please help me on this.
# 2  
Old 03-15-2013
I think this will add newline to end of last line:
Code:
sed "$ s/$/\n/" infile > outfile

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 03-15-2013
Do you mean the last line has no trailing newline? In that case you may or may not be able to use sed some can, but some cannot handle that. Try
Code:
printf "\n" >> file

--
@hanson44: most seds cannot handle \n in the replacement part of the substitute command (and they will write a literal "n")
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-15-2013
sed does read the last line, even if no EOL, at least with GNU sed.
I guess I'm used to GNU sed or other modern version of sed.
But you have a good point. And your solution is 100% better.
Code:
$ echo -n x > temp.x
$ cat temp.x
x$sed "$ s/$/\n/" temp.x
x
$

# 5  
Old 03-15-2013
Adding end of line character in the last record

Thanks for both. Its is workinf fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding semicolon at the end of each line

Hi, I have a script which I need to change. I want to add a semicolon at the end of each line where the line starts with "grant" for e.g. create table(.... ); grant select on TABL1 to USER1 grant select on TABL1 to USER2should become create table(.... ); grant select on TABL1 to... (3 Replies)
Discussion started by: pparthiv
3 Replies

2. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

3. Shell Programming and Scripting

Need a ksh script for adding the space at the end of record in a flat file

Hi, I need a ksh script for the below requirement: i have a Delimited flat file with 200 records delimiter is '|~|' i need a script to insert space at the end if the record is ending with delimiter '|~|' if it didnt end with delimiter it should not append space. Example: ram|~|2|~| ... (16 Replies)
Discussion started by: srikanth_sagi
16 Replies

4. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

5. Shell Programming and Scripting

Adding lines at end of a line

This is what I want to do. I want to write a script that reads each line (of the highlighted file below) and add a specific number of blank lines (sometime 2, 3 or 5 lines) at the end of each line while copying that line. For example, here is the input. The sky is blue. I like to eat. I like... (19 Replies)
Discussion started by: Ernst
19 Replies

6. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

7. Shell Programming and Scripting

adding semicolumn at end of line

Hi , I need to add semicolumn at the end of each line in a file. can any one help me in this? Thanks in advance (2 Replies)
Discussion started by: scorpio
2 Replies

8. Shell Programming and Scripting

Adding new line at the end of file

Hi I have few files. For some files the cursor is at the end of last line. For other files, cursor is at the new line at the end. I want to bring the cursor down to next line for the files that are having cursor at the end of last line In otherwords, I want to introduce a blank line at the... (5 Replies)
Discussion started by: somesh_p
5 Replies

9. Shell Programming and Scripting

Finding a character in first line of a record

HI, I am pretty new to Unix scripting. I will need help in Finding a character in first line of a file or a set of files. The scenario is as follows: Lets consider a set of files which is having a character "ID"(without quotes) in the first line of each file.I need to find this character... (14 Replies)
Discussion started by: bsandeep_80
14 Replies

10. UNIX for Dummies Questions & Answers

adding a column at the end of the record

hello., i have a .txt file. inside the .txt file i have., /home/ss/cca.costco.transaction_date /home/sk/cca.costco.transaction_date /home/st/cca.costco.transaction_date /home/sv/cca.costco.transaction_date cca.costco.transaction_date is the file name. inside the file there are some... (2 Replies)
Discussion started by: pavan_test
2 Replies
Login or Register to Ask a Question