Parse and delete lines efficiently


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse and delete lines efficiently
# 1  
Old 08-20-2009
Parse and delete lines efficiently

Hi

I have a set of options in the form of key value in a file. Need to find a particular value of 'a' and delete all lines till the next 'a' keyword .

Ex :

a bbb
c ddd
e fff
g hhh

a sss
c ggg
e xxx
f sss

a ddd
d sss
r sss
g hhh

and so forth

Here 'a' keyword is present in each stanza. I need to parse to find a particular value of 'a' (let's say 'sss') and from there, delete all lines till the next 'a' keyword.

Thanks
# 2  
Old 08-20-2009
Always, give the input and the expected output and the condition.
It will be easier if you give the ouput.
# 3  
Old 08-20-2009
Input

a bbb
c ddd
e fff
g hhh

a sss
c ggg
e xxx
f sss

a ddd
d sss
r sss
g hhh

Output

a bbb
c ddd
e fff
g hhh

a ddd
d sss
r sss
g hhh
# 4  
Old 08-20-2009
Try this

Code:
awk  'NR%2==1{print}' RS="\n\n" ORS="\n\n" filename

# 5  
Old 08-20-2009
Ranjith - Can you please explain your command below...

Let's assume I found the line with "a sss" using grep... all I now need to do is start deleting lines till I find the next keyword "a ..." (... can be any value)

Hope that gives better picture.

It's kind of
(while nextkeyword != 'a')
{
delete all lines inbetween
}
# 6  
Old 08-21-2009
Hi,

Sorry misunderstood your requirement. Below script will delete the data from line starting with "a sss" untill the line starting with "a".

Code:
awk '$0~"^a" {flag=1} $0~"^a sss" {flag=0} flag==1'  filename

Regards,

Ranjith
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

2. Shell Programming and Scripting

parse lines

I have file which is having 500 lines. I want to get the first 100 lines then sleep, then again next 100 lines sleep so now till the end of the file. Can someone tell me in perl and bash. also i want to do it in threads. Thanks.. (6 Replies)
Discussion started by: Anjan1
6 Replies

3. 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

4. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

5. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

6. Shell Programming and Scripting

Parse a File ColumnWise & Delete the Rows

Hi , I have a CSV file that has around 50K records in it. I have to delete all the duplicate rows from the file except one, depending upon data in column4. Lets say there are 3 rows for 'column4data' in the file. I have to retain only that line which has the smallest date value in column2.... (3 Replies)
Discussion started by: Sheel
3 Replies

7. UNIX for Dummies Questions & Answers

How get only required lines & delete the rest of the lines in file

Hiiii I have a file which contains huge data as a.dat: PDE 1990 1 9 18 51 28.90 24.7500 95.2800 118.0 6.1 0.0 BURMA event name: 010990D time shift: 7.3000 half duration: 5.0000 latitude: 24.4200 longitude: 94.9500 depth: 129.6000 Mrr: ... (7 Replies)
Discussion started by: reva
7 Replies

8. Shell Programming and Scripting

How to delete lines in a file that have duplicates or derive the lines that aper once

Input: a b b c d d I need: a c I know how to get this (the lines that have duplicates) : b d sort file | uniq -d But i need opossite of this. I have searched the forum and other places as well, but have found solution for everything except this variant of the problem. (3 Replies)
Discussion started by: necroman08
3 Replies

9. Shell Programming and Scripting

How to parse a string efficiently

I am new to the boards and to shell programming and have a requirement to name new files received with a unique sequence number. I need to look at a particular file pattern that exists and then to increment a sequence by 1 and write the new file. Example of file names and sequence # ... (4 Replies)
Discussion started by: sandiego_coder
4 Replies

10. Shell Programming and Scripting

parse files and delete

For the CSV file it should have one entry per line looking like this: pwd/filename,YYYYMMDDhhmmss,OK The date stamp should be from the file properties and OK will be a constant. find . name 'W*' gives me the proper list of files and date +%Y%m%d%H%M%S is the proper date format but I... (0 Replies)
Discussion started by: pimentelgg
0 Replies
Login or Register to Ask a Question