Visit Our UNIX and Linux User Community


Adding new line at the end of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding new line at the end of file
# 1  
Old 11-05-2007
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 end of line, if it doesn't exist

Here is example.. for test_file cusor is at the end of line3. I want to bring the cursor down to the next line. Then only, I can process that file. Please help me. Feel free to ask me if you unable to understand my requirements

***test_file****

line1
line2
line3
line4

Last edited by somesh_p; 11-05-2007 at 04:44 PM..
# 2  
Old 11-05-2007
Code:
for f in your_files;do sed '${/^$/!s/$/\
/;}' "$f">"$f"_ && mv "$f"_ "$f"
done

1. There is a new line after s/$/\ !!!
2. If you have GNU sed you can use the -i option
instead of temporary files.

Last edited by radoulov; 11-05-2007 at 06:14 PM.. Reason: Corrected ...
# 3  
Old 11-05-2007
Thank you for your response

It is giving me below error

$ sed '$a' test.webM > ./test.webM_
sed: Function $a cannot be parsed.

I want to introduce a new blank line at the end of file only if it doesn't exist

Thank you
Somesh
# 4  
Old 11-05-2007
Yep,
check the corrected version above.
# 5  
Old 11-05-2007
Yes.. it is working fine for the files that doens't have a blank line at the end of file

But, if the file already contains a blank line at the end, the modified file is becoming zero bytes. It shouldn't modify the file, if it already has blank line at the end

Please suggest some thing

$ ls -ltr test2.webM
-rw-r--r-- 1 spamarth users 15010 Nov 5 16:34 test2.webM
$ sed '${/^$/!s/$//;}' test2.webM > ./test2
$ ls -ltr test2.webM test2
-rw-r--r-- 1 spamarth users 15010 Nov 5 16:34 test2.webM
-rw-r--r-- 1 spamarth users 0 Nov 5 16:35 test2
$
# 6  
Old 11-05-2007
Quote:
Originally Posted by somesh_p
Yes.. it is working fine for the files that doens't have a blank line at the end of file

But, if the file already contains a blank line at the end, the modified file is becoming zero bytes. It shouldn't modify the file, if it already has blank line at the end
[...]
I told you the command is:

Code:
sed '${/^$/!s/$/\
/;}' filename

and NOT:

Code:
sed '${/^$/!s/$//;}'

It works even on my old Solaris Smilie

Code:
$ cat file1
line1
line2
line3
line4
$ cat file2
line1
line2
line3
line4

$ sed '${/^$/!s/$/\
> /;}' file1
line1
line2
line3
line4

$ sed '${/^$/!s/$/\
/;}' file2
line1
line2
line3
line4

$


Previous Thread | Next Thread
Test Your Knowledge in Computers #880
Difficulty: Medium
SCADA systems that tie together decentralized facilities such as power, oil, gas pipelines, water distribution and wastewater collection systems were designed to be open, robust, easily operated and repaired, and highly secure.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on Adding one counter loop at the end of each line in a file

Hello All, I have file a.txt I want to add a counter loop at the end of each line in a file ill explain: i have a site h**p://test.test=Elite#1 i want to add a a counter to the number at the end of the file, that it will be like this urlLink//test.test=Elite#1 urlLink//test.test=Elite#2... (3 Replies)
Discussion started by: nexsus
3 Replies

2. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

3. 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

4. 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

5. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

6. Shell Programming and Scripting

help with sed adding line to end of file

sed '$a\ hello' books hi i am trying to use sed to append hello to the end of the file books, but for some reason i can't get it work. It keeps sayin command garbled. Anyone know what I'm doing wrong. this is in a ksh script as well. (3 Replies)
Discussion started by: bjhum33
3 Replies

7. 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

8. Shell Programming and Scripting

Adding multiple line at the end of the file

I have 2 files which contains the following lines file1.txt line4 line5 line6 file2.txt line1 line2 line3 When i execute a script , I want my file2.txt will looks like this: line1 line2 line3 line4 line5 (2 Replies)
Discussion started by: kaibiganmi
2 Replies

9. Shell Programming and Scripting

Grabing Date from filename and adding to the end of each line in the file.

Hi, I have 24 .dat files something like below. The file name starts with “abc” followed by two digit month and two digit year. Is there a way to grab the month and year from each filename and append it to the end of each line. Once this is done I want to combine all the files into file... (1 Reply)
Discussion started by: rkumar28
1 Replies

10. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies

Featured Tech Videos