Find line before blank


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find line before blank
# 1  
Old 03-18-2009
Find line before blank

Hello,

I actually have two issues. First, here is the file the way it is now.

someword someword:1
new-word new-word abcd
someword someword:10
new-word new-word abcd
thisis whatIneed:3


someword someword:5
new-word new-word abcd

I need to get the line before the 2 blanks and move it to a different file. I've tried using sed, but keep getting error messages. Also, there may be none, one, or several instances of this within the file.

I should probably also mention that this is part of a larger perl script I'm working on. I have everything else done, the file above is actually generated by the script. I also have everything below it complete, but am completely stuck at this point.

Any help with either would be appreciated.

Last edited by ddrew78; 03-18-2009 at 08:43 PM..
# 2  
Old 03-18-2009
roughly thought of below, hope can help you some

Code:
open $fh,"<","yourfile";
open $out,">>","youroutfile";
undef $/;
$str=<$fh>;
print $out split(/\n^$\n^$\n/s,$str,2)[0];

# 3  
Old 03-18-2009
Here is mine, ugly but should do the trick

(not tested)

Code:
awk 'BEGIN{i=0}
  { content[NR]=$0; if (($0=="")) {b[i]=NR;i++} j++;} 
  END 
 { 
   for (x=0;x<=b[0];x++) 
  {print content[x] > "file1" } for (x=b[1];x<=j; x++) { print content[x] > "file2"}   
}'  /var/tmp/file

# 4  
Old 03-19-2009
Re: Find line before blank

summer cherry,

here is what I ended up with in my script:

#!/usr/bin/perl

open $fh,"<","myfile";
open $out,">>","mynewfile";
undef $/;
$str=<$fh>;
print $out split(/\n^$\n^$\n/s,$str,2)[0];

I got the error message below. Any ideas? I appreciate the help.

syntax error at ansipre2 line 7, near ")["
Execution of ansipre2 aborted due to compilation errors.
# 5  
Old 03-19-2009
Re: Find line before blank

Quote:
Originally Posted by aaaaargh
Here is mine, ugly but should do the trick

(not tested)

Code:
awk 'BEGIN{i=0}
  { content[NR]=$0; if (($0=="")) {b[i]=NR;i++} j++;} 
  END 
 { 
   for (x=0;x<=b[0];x++) 
  {print content[x] > "file1" } for (x=b[1];x<=j; x++) { print content[x] > "file2"}   
}'  /var/tmp/file



Thanks for the reply. Unfortunately I'm new to this and can't figure out how to implement this into my script. Below is the last two lines of the script to get the file I had above.

system "dos2unix ansi3 > ansi7";
system "mv ansi7 ansi3";

Thanks again for any help.
# 6  
Old 03-19-2009
Code:
$ cat sd
sed -n '/^$/!{
h
}
/^$/{
N
/^\n$/ {
x
p
q
}
}' fl
$
$ cat fl
someword someword:1
new-word new-word abcd
someword someword:10
new-word new-word abcd
thisis whatIneed:3


someword someword:5
new-word new-word abcd
$
$ sd
thisis whatIneed:3

Be careful when you cut paste this - there MUST NOT be any trailing spaces.

Last edited by rikxik; 03-19-2009 at 11:49 PM..
# 7  
Old 03-20-2009
Thanks to all who helped me on this. After much pain I decided to go a different route and instead appended the repeating string to it's previous line. Granted, that resulted in a few extra lines of code, but what the heck. Just an FYI, below is the code that ended up giving me the lines I was originally looking for.



open(FILE7, ">file7");
open(MYINPUTFILE, "file3");

while(<MYINPUTFILE>) {
chomp;
my $someword = "";
my $new-word = "";
if (/^someword/) {$someword = $_;while(<MYINPUTFILE>) {chomp;
if (/^new-word/){
print FILE7 "$someword $_";
print FILE7 "\n";}
last;
}
}
}
system "mv file7 file3";
system "dos2unix file3 > file7";
system "mv file7 file3";
system 'cat file3 | cut -d " " -f1-2 >>file0';
system "sort -n file0 >file1";
system "mv file1 file0";
system "sort file0 | uniq -u > file1";
system "mv file1 file0";
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

Combine multiline to one line till a blank line

Hello, I have a file as :- ABC DEF GHI JKL <BlankLine> MNO PQR STU VWX <BlankLine> YZA I need it as below:- ABCDEFGHIJKL; MNOPQRSTUVWX; (3 Replies)
Discussion started by: jassi10781
3 Replies

3. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

4. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

5. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

6. Shell Programming and Scripting

Need sed help: find regex and if the next next line is blank, delete both

I've got a report I need to make easier to read Using sh on HP-UX 11.12. In short, I want to search for a regular expression and when found, examine the next line to see if it's blank. If so, then delete both lines. If not blank, move on to the next regexp. Repeat. So far I've got: ... (7 Replies)
Discussion started by: Scottie1954
7 Replies

7. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

8. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies

9. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

10. UNIX for Dummies Questions & Answers

cant find command that returns blank line

This is my list of sed commands: can anyone tell me where im going wrong. The script works on a file called data which contains 6 student id's and there answers for 6 questions. !/bin/sh sed -e 's/*//g' \ #replace * with nothing -e s/ /X/g' \ #replacing empty space with X -e... (2 Replies)
Discussion started by: jeffersno1
2 Replies
Login or Register to Ask a Question