Sed delete certain number of empty lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed delete certain number of empty lines
# 1  
Old 07-25-2011
Sed delete certain number of empty lines

Hi

What I'm trying to do is delete every blank line upto a certain number, so for instance I only want to delete the first 4 empty lines within a file, I know how to delete every line with:

Code:
sed '/^$/d'

But I can't figure out how to limit it to only the first 4 occourances

I though it would be:
Code:
sed '4,/^$/d'


But that doesn't seem to do it
# 2  
Old 07-25-2011
I don't think 'sed' can do it, but here is one possible solution:
Code:
#!/bin/ksh
typeset -i mCnt=0
for mLine in $(egrep -n '^$' b); do
  mTmp=$(echo ${mLine} | sed 's/:/d/')
  mOLine=${mOLine}${mTmp}';'
  mCnt=${mCnt}+1
  if [[ ${mCnt} -eq 4 ]]; then
    break
  fi
done
sed "${mOLine}" Input_File

# 3  
Old 07-25-2011
You can loop 4 times over sed
Code:
for i in 1 2 3 4
do
sed '/^$/d'
done

# 4  
Old 07-25-2011
Quote:
Originally Posted by h@foorsa.biz
You can loop 4 times over sed
Code:
for i in 1 2 3 4
do
sed '/^$/d'
done

Hi


that didn't work, it just goes in a loop 4 times removing all empty lines from the file.

I guess an example input -> output would be:

Input
Code:
1

2

3

4

5

6

Output:
Code:
1
2
3
4

5

6

# 5  
Old 07-26-2011
Hi,

Test this pure 'sed' solution:
Code:
$ cat infile
Sat Jul 23 16:10:03 EDT 2011



Jul 22 2011
Jul 23 2011

Jul 24 2011

Jul 25 2011

Jul 26 2011

Jul 27 2011
$ cat script.sed
/./ { p ; b }
x
s/\(\n\)/&/4 
t a
x
H
b
: a
x
p
$ sed -nf script.sed infile
Sat Jul 23 16:10:03 EDT 2011
Jul 22 2011
Jul 23 2011
Jul 24 2011

Jul 25 2011

Jul 26 2011

Jul 27 2011

Regards,
Birei
# 6  
Old 07-26-2011
Quote:
Originally Posted by duonut
so for instance I only want to delete the first 4 empty lines within a file
Code:
awk '!NF && c<4{c++;next}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

2. Shell Programming and Scripting

delete lines with empty second column

hi, I'd like to ask you if you can help me with such surely easy thing - I have some data and need to plot them. I have txt of data in two columns, tab separated, but some second columns are empty. like 1```` 2 1.2`` 3```` 2 4.44` 5````` 6.1```1 how can I erase all the lines... (2 Replies)
Discussion started by: bsco
2 Replies

3. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

4. Shell Programming and Scripting

How to delete all leading empty lines in a file?

How to delete all leading empty lines in a file? $>cat input.txt test Test $> output file should be test Test $> (14 Replies)
Discussion started by: johnveslin
14 Replies

5. Shell Programming and Scripting

What's the command to remove empty lines with sed?

3 10 20 10 100 100 10000 Output: 3 10 20 10 100 100 10000 ---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ---------- sed '/^$/d' file doesn't work. (8 Replies)
Discussion started by: cola
8 Replies

6. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

7. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

8. Shell Programming and Scripting

delete first number of even-numbered lines

Hello ! I am trying to delete a number from the even-numbered lines of a pipeline after having extracted and sorted the desired data from an original text file using sed... sed -r 's/\(*\)*\((*)\),*,*,(*)*: * \w*\=(*).*/\3,\1/g' | sort -n then the data looks like : Now, I am... (3 Replies)
Discussion started by: ShellBeginner
3 Replies

9. UNIX for Advanced & Expert Users

How to delete empty lines

abc# abc#this is a test abc#this is a test to delete abc# xyz# xyz#this is a test two xyz# In the above example '#' is common. How to do delete the emply lines. In specific to observe the output as: abc#this is a test abc#this is a test to delete xyz#this is a test two . . . . (5 Replies)
Discussion started by: Aejaz
5 Replies

10. Shell Programming and Scripting

delete multiple empty lines

Hi, I need to delete the lines with empty name. What is the best way to do it? Thanks a lot for your help! EMID MMDDYY HOURS JOB EMNAME 0241 051605 11.40 52062 someone 0520 051605 10.63 52062 0520 051605 10.66 52062 0520 051605 10.65 52062 (3 Replies)
Discussion started by: whatisthis
3 Replies
Login or Register to Ask a Question