Visit Our UNIX and Linux User Community


Deleting lines in a flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting lines in a flat file
# 1  
Old 08-03-2009
Deleting lines in a flat file

Hi Friends
I have a flat file which has sentence like "Notice generated". It can be anywhere in the flat file.
What I want to do is, I want to delete all the lines which are above sentence "Notice generated".
How can I do it.
Kindly advice.
Anushree
# 2  
Old 08-03-2009
Try this:

Code:
sed '/Notice generated/d' file

# 3  
Old 08-03-2009
No the simple sed (nor the ubiquitous grep -v 'Notice generated' > newfile) would NOT do what anushree.a is asking for. Those commands would only get rid of the lines WITH those text tags.

His real question is "How to delete the lines above the text tag?", and does specify how many lines above his text search term.

I'm sure perl could do it, I'll play around and post my results.
# 4  
Old 08-03-2009
hoping that 'Notice generated' comes once in the file and u want delete all the lines above it...

Try this...
Code:
sed '1,/Notice generated/d' youfile

# 5  
Old 08-03-2009
A different approach (but conventional one) Might the famous sed one
liner -- Print 1 line of context before and after regexp, with
line number indicating where the regexp occurred this is
to (similar to "grep -A1 -B1").
Code:
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

Where '1' could be some other number.

Last edited by Yogesh Sawant; 08-04-2009 at 02:28 AM.. Reason: added code tags
# 6  
Old 08-04-2009
Hey Malcomex999 and friends,
sed.... /d' filename worked perfectly.

Thank you very much for the guidance.
Take care and god bless you all.
Anushree

Previous Thread | Next Thread
Test Your Knowledge in Computers #915
Difficulty: Easy
In Javascript, 'UNIX.com'.slice(); returns UNIX.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

deleting lines from file

We have a server that logs transactions to a file. I want to write a script that will delete the first 50 lines of the file daily without renameing the file or moving the file. (8 Replies)
Discussion started by: daveisme
8 Replies

2. UNIX for Advanced & Expert Users

Deleting lines from a file

How I can delete 100 lines anywhere in a file without opening a file and without renaming the file. (11 Replies)
Discussion started by: Nirgude07
11 Replies

3. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies

4. UNIX for Dummies Questions & Answers

Deleting whole lines from a file

I have a file with 65 sets of 35 coordinates, and would like to isolate these coordinates so that I can easily copy the coordinates to another file. The problem is, I've got a 9 line header before each set of coordinates (so each set is 44 lines long). There are a zillion threads out there about... (3 Replies)
Discussion started by: red baron
3 Replies

5. Shell Programming and Scripting

remove specific lines from flat file using perl

Hi, Here is wat im looking for.. i have a flat file which looks like this.. 00 * * * * .. .. * * text text text COL1 COL2 ----- ----- 1 a (12 Replies)
Discussion started by: meghana
12 Replies

6. Shell Programming and Scripting

Deleting lines in a file

How do I delete all the lines after the line containing text ***DISCLOSURES*** . I want to delete this line too. Thank you (2 Replies)
Discussion started by: reachsamir
2 Replies

7. Shell Programming and Scripting

Deleting last 2 lines from the file.

Hi I have a file & always I need to remove or delete last 2 lines from that file. So in a file if I have 10 lines then it should return me first 8 lines. Can someone help me? (4 Replies)
Discussion started by: videsh77
4 Replies

8. Shell Programming and Scripting

merge multiple lines from flat file

Hi, I have a tab delimited flat file like this: 189 Guide de lutilisateur sur lappel conférence à trois au moyen d'adaptateurs téléphoniques <TABLE><TBODY><TR><TD><DIV class=subheader>La fonction Appel conférence à trois </DIV></TD> \ <TD><?php print $navTree;?> vous permet de tenir un appel... (4 Replies)
Discussion started by: hnhegde
4 Replies

9. Shell Programming and Scripting

How to read lines from a flat file

i have some commands written line by line in one flat file i have to read each linefrom this file(file name passed as parameter) and then i have to execute this How can i do it? (2 Replies)
Discussion started by: bihani4u
2 Replies

10. Shell Programming and Scripting

deleting lines in a log file

Is there an easy way to delete the first so many lines in a log file? like I have a log file that has 10000 lines, i want to just get rid of the first 9000. (2 Replies)
Discussion started by: BG_JrAdmin
2 Replies

Featured Tech Videos