Removing consecutive lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing consecutive lines in a file
# 8  
Old 10-06-2011
Try this?
Code:
awk '/^Begin/{getline}/^End/{next}1' input_file

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 9  
Old 10-06-2011
I cut and pasted but it doesn't look like there are the right amount of spaces in my post - there should be three spaces after 'Begin' and before the ':'. With 'End', there are five spaces before the ':'.
Let me try pasting again.

Begin : Transaction Id 1234 04-Oct-2011 13:44:22.24 Username smith
End : Transaction Id 1234 04-Oct-2011 13:44:22.25

I modified and ran again: nawk '/^End :/ && A { A=""; next } A { print A ; A="" } /^Begin :/ { A=$0 ; next } 1' 20111005.trl > testnawk &

I think this may have solved it but need to do some testing on a much bigger file.

Is this finding 'End :' putting that in a place holder, then finding if 'Begin :' is the next line above and deleting both or printing everything but those lines ?

Thank you !
# 10  
Old 10-06-2011
Yes it's storing the line in A and printing later, have a look a ahamed101's solution it just calls getline to throw away the being line and process the line following.

Also, to post data as is put it between [code] and [/code] tags.
# 11  
Old 10-06-2011
Looks like it truncated my whitespace yet again ...
Will keep you posted on this - pretty sure it works !
Thank you !

---------- Post updated at 02:15 PM ---------- Previous update was at 02:13 PM ----------

The getline solution took out all of my 'Begin :' lines ...except for one in the middle of the file.

Thank you though !
# 12  
Old 10-06-2011
The getline solution just egrep -ve '^Begin|^End'

If you only want to retain the Begin/End block who contains at least 1 or more lines (and keeping the Begin and End lines of those blocks) then you can go for :

Code:
nawk '{A[NR]=$0}/^Begin/{b=NR}/^End/&&((NR-b)>1){for(i=b-1;++i<=NR;) print A[i]}' infile

you can then play with b or b-1 as well as <NR or <=NR of the "for" loop depending on your need of the Bein/End lines or not

Last edited by ctsgnb; 10-06-2011 at 03:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

2. Shell Programming and Scripting

Removing 3 consecutive lines from script

Hi I need to remove three consecutive lines of code which appear multiple times during a script. Two of the lines also appear in other parts of the scripts and need to stay so I can't strip out the code per se - It needs to be the exact three lines. Hope that makes sense ! Any help much... (5 Replies)
Discussion started by: Grueben
5 Replies

3. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

4. UNIX for Dummies Questions & Answers

Delete 26 consecutive lines in a file

I have a text file that is about 90,000 lines long. How would I delete lines 64-89, 152-177, 240-265, 328-353... etc? The sections I would like to delete are 26 lines long and the number of lines between the sections I would like to delete is 62 lines. Thanks very much in advance. (6 Replies)
Discussion started by: MDeBiasse
6 Replies

5. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

6. Shell Programming and Scripting

Scan a file in realtime and execute certain commands on encountering 5 consecutive identical lines

Mysql log has something like below: I need a bash shell script that will do the following: 1) The script will scan the mysql.log file constantly in real time (something like tail -F mysql.log) 2) If it encounters 5 consecutive identical lines then it would invoke some commands (say... (4 Replies)
Discussion started by: proactiveaditya
4 Replies

7. Shell Programming and Scripting

how to delete two consecutive lines from the file

Hi guys I am deleting a unique line from the file and also need to remove the line above it which is NOT unique and servers as a record separator. Here is an example: # 101 803E 823F 8240 # 102 755f 4F2A 4F2B # 290 747D 0926 0927 # 999 8123 813E ... (5 Replies)
Discussion started by: aoussenko
5 Replies

8. Shell Programming and Scripting

Find time difference between two consecutive lines in same file.

Hello I have a file in following format: IV 08:09:07 NM 08:12:01 IC 08:12:00 MN 08:14:20 NM 08:14:15 I need a script to compare time on each line with previous line and show the inconsecutive line. Ex.: 08:12:00 08:14:15 A better way... (6 Replies)
Discussion started by: vilibit
6 Replies

9. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

10. UNIX for Dummies Questions & Answers

Cutting n consecutive lines from a file...

Hi, I have this problem of separating 10 consecutive lines from a file, say starting from 21 to 30... I have used a filter like this.. head -n 30 myfile | tail -n 10 Is there a simpler way than this? (2 Replies)
Discussion started by: Vishnu
2 Replies
Login or Register to Ask a Question