The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
sed and blank lines alrinno Shell Programming and Scripting 6 07-17-2008 02:42 PM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 02:21 AM
Eliminate blank lines... shashi_kiran_v UNIX for Dummies Questions & Answers 4 09-06-2005 01:39 AM
delete blank lines or lines with spaces only vascobrito UNIX for Dummies Questions & Answers 3 01-13-2004 07:36 AM
Blank Lines - End of file saabir Shell Programming and Scripting 4 07-15-2003 12:55 PM

Reply
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 07-11-2009
jepeto jepeto is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 4
Question Add blank lines

Hi!

I am trying to add blank lines at the end of a text file just before the last line. (This is a part of a script)

For example this is my text file... lines with dashes are included

Quote:
--------------------

this is an example of
my lack of
knowledge to use the
sed editor

--------------------
The problem is that i dont know before hand the number of blank lines to be added. The number of blank lines are determined by a variable and they are not always the same

ex
> echo $a
> 4

So the sed command....

Code:
sed '/-----------/s/^/\n\n\n\n&/g' myfile.txt
....does not do the job
What i need is to use my variable output in a command to add the blank lines

Any help will be appreciated
  #2 (permalink)  
Old 07-11-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
An approach with awk:

Code:
number=4

awk -v var=$number -v n=$(wc -l < myfile.txt) 'NR==n{for(i=1;i<=var;i++){print ""}}1' myfile.txt
  #3 (permalink)  
Old 07-11-2009
jepeto jepeto is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 4
Thank you for the reply Franklin52
I dont know why but that didnt work for me.
The output i get is exactly what i have in my text file
It does not add any blank lines to it

---------- Post updated at 05:17 PM ---------- Previous update was at 05:00 PM ----------

It works fine now
I just had to add awk '{ print $1 }' to your command

Code:
awk -v var=$number -v n=$(wc -l < myfile.txt | awk '{ print $1 }') 'NR==n{for(i=1;i<=var;i++){print ""}}1' myfile.txt
Thanks for the help Franklin52!!!!
  #4 (permalink)  
Old 07-11-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,057
I tried it in sed and gave up after three attempts, so as a complete alternative!...

Code:
head -$(echo $(($(cat myfile.txt| wc -l)-1))) myfile.txt; echo; tail -1 myfile.txt
awk rules!
  #5 (permalink)  
Old 07-11-2009
nj78 nj78 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 105
Yeah, awk lets put in loop. This is one of those times I like the x operator in Perl. Thanks.
Reply

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:39 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