Need to delete previous lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to delete previous lines
# 1  
Old 07-29-2010
Question Need to delete previous lines

Need to delete the line which is directly above any line which has 3 fields in it.
Code:
one two three
one
two
three
four five six
four
five
six
seven eight nine
seven 
eight
nine
one two three

should output:
Code:
one two three
one
two 
four five six
four
five 
seven eight nine
seven 
eight 
one two three


Last edited by zaxxon; 07-29-2010 at 10:23 AM.. Reason: changed thread subject from "some" to "previous"
# 2  
Old 07-29-2010
Code:
$> awk 'NF==1 {if(a)print a; a=$0} NF==3 {print; a=""}' infile
one two three
one
two
four five six
four
five
seven eight nine
seven
eight
one two three

I changed your subject slightly so others might easier find or identify this thread.
# 3  
Old 07-29-2010
Quote:
Originally Posted by zaxxon
Code:
$> awk 'NF==1 {if(a)print a; a=$0} NF==3 {print; a=""}' infile
one two three
one
two
four five six
four
five
seven eight nine
seven
eight
one two three

I changed your subject slightly so others might easier find or identify this thread.
my mistake, but with the input file some lines have 2 words on it (can be any word), whilst others may have jsut the one on it. I tried replacing the NF==1 with NF==2 but that didn't work properly, as it deleted two lines if the one directly above had 1 field and the one above that had 2, though if the one above only had 2 words, then it worked ok!
# 4  
Old 07-29-2010
Uhm so you are fine now Smilie
# 5  
Old 07-29-2010
Quote:
Originally Posted by zaxxon
Uhm so you are fine now Smilie
no sorry im not! forget the then it worked ok bit! because it hasnt!
# 6  
Old 07-29-2010
If I understood it right (I added 3 exclamation marks to represent 2 fields for easier spotting):
Code:
$> cat infile
one two three
one
two
three
four five six
four
five
six !!!
seven eight nine
seven !!!
eight
nine
one two three
$> awk 'NF<3 {if(a)print a; a=$0} NF==3 {print; a=""}' infile
one two three
one
two
four five six
four
five
seven eight nine
seven !!!
eight
one two three

This User Gave Thanks to zaxxon For This Post:
# 7  
Old 07-29-2010
Code:
#!/bin/bash

declare -a array
i=1
while read -r LINE
do
  set -- $LINE
  if [[ ${#} == 3 ]] ;then
     for((o=1;o<${#array[@]};o++))
     do
        echo "${array[$o]}"
     done
     echo "$LINE"
     i=1
  else
     array[$i]="$LINE"
     ((i++))
  fi
done <"file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete the previous line after pattern match?

Team, I am writing a shell script to perform few health checks of the system, where I need to delete the previous line in the text file after pattern match using sed (or) awk. Could you please help me out on this? For example, <td> <td style=color:green align=center> </td> </tr>... (6 Replies)
Discussion started by: Nagaraj R
6 Replies

2. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

3. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

4. Shell Programming and Scripting

Find and delete file previous to some date

Hello All, I have a directory containing of many .dat file, but with different naming conventions. I want to delete files which are created before and on 10th September 2013. I tried this command to see files which are are the files created before 10th September find path/to/file -type... (6 Replies)
Discussion started by: nnani
6 Replies

5. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

6. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

7. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

8. Shell Programming and Scripting

Need a script to delete previous versions of files

Hi. I need a script (either bash or perl) that can delete previous versions of files. For instance, from our continuous build process I get directories such as build5_dev_1.21 build5_dev_1.22 build5_dev_1.23 build5_dev_1.24 I need a script that I can run every night (using "at"... (6 Replies)
Discussion started by: jbsimon000
6 Replies

9. Shell Programming and Scripting

Moving next 2 lines contents to previous lines

My input file is aaa bbb ccc a1a b1b c1c a2a b2b c2c I want the output file to look like that: aaa,bbb,ccc a1a,b1b,c1c a2a,b2b,c2c How do I achieve this ? (8 Replies)
Discussion started by: Amruta Pitkar
8 Replies

10. Shell Programming and Scripting

Need to delete previous line after successful seearch

i have a file in following pattreen A: Size = 10 B: Size = 0 C: Size = 220 D: Size = 0 i want to Display only Charecters which have which have Size = 0 The Out put Should be B: D: Can Some one Help (7 Replies)
Discussion started by: pbsrinivas
7 Replies
Login or Register to Ask a Question