The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding multiple line at the end of the file kaibiganmi Shell Programming and Scripting 2 11-23-2007 12:21 AM
adding a line to a file andy2000 Shell Programming and Scripting 2 03-26-2007 09:44 AM
Adding a character in the beginning of every line in a .dat file Cool Coder Shell Programming and Scripting 2 12-22-2005 06:47 AM
adding text to end of each line in a file cubs0729 Shell Programming and Scripting 2 11-10-2005 01:02 PM
Adding Text To each line of a file cubs0729 Shell Programming and Scripting 4 09-08-2005 05:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-05-2007
somesh_p somesh_p is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 6
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 03:44 PM..
  #2 (permalink)  
Old 11-05-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926

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 05:14 PM.. Reason: Corrected ...
  #3 (permalink)  
Old 11-05-2007
somesh_p somesh_p is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 6
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 (permalink)  
Old 11-05-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Yep,
check the corrected version above.
  #5 (permalink)  
Old 11-05-2007
somesh_p somesh_p is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 6
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 (permalink)  
Old 11-05-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Quote:
Originally Posted by somesh_p View Post
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


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

$

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0