how to delete two consecutive lines from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to delete two consecutive lines from the file
# 1  
Old 04-12-2011
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:
Code:
#
  101   803E     823F     8240
#
  102   755f     4F2A     4F2B
#
  290   747D     0926     0927
#
  999   8123     813E     8155
#
  206   807C     803F     8237

Deleting a unique line is not a problem, but I cannot figure out how to remove the line above it (starts with # ...)
May be someone already handle similar problem.
Thanks a lot for advice

Last edited by Franklin52; 04-12-2011 at 03:10 PM.. Reason: Please use code tags
# 2  
Old 04-12-2011
For this, sed is a nice tool:
Code:
sed '
  :loop
  $b
  N
  /\nYOUR_PATTERN/d
  P
  s/.*\n//
  b loop
 ' infile >outfile

If at end of file, branch to <no_tag>, which is to end of script, print and exit (some sed are funny with N at EOF). Get a second line into the buffer. If YOUR_PATTERN is the second line, delete both. Otherwise, spit out the first line, delete it and go back to 'loop' to get another line.

In vi, this is an ex ':' global command:
Code:
:g/YOUR_PATTERN/.-1,.d

# 3  
Old 04-12-2011
DGPickett,
thanks for advice, but for some reason it down not remove the requested lines Smilie

This is before the change:
Code:
$ grep " 2001   8232     8232     8232" firmexbytes.dta.new 
 2001   8232     8232     8232
$

Here is the script:
Code:
$ sed '
> :loop
> $b
> N
> /\n"2001   8232     8232     8232"/d
> P
> s/.*\n//
> b loop
> ' firmexbytes.dta.new > firmexbytes.dta.del

This is after the script was run:
Code:
$ grep " 2001   8232     8232     8232" firmexbytes.dta.del
 2001   8232     8232     8232
$


Last edited by Franklin52; 04-12-2011 at 03:03 PM.. Reason: Please use code tags
# 4  
Old 04-12-2011
You missed some quoting,m and now are looking for it and the double quotes (literally because you are in single quotes). I do sed scripts inline in single quotes, but temporrily close the single quoting and go to double to access a shell provide env var, then close the double and restart the single quoting. If you put in only literals, the outer single quotes are sufficient.

Second, the pattern must have any leading white space characters after \n.

Last edited by DGPickett; 04-12-2011 at 03:26 PM..
# 5  
Old 04-12-2011
You can also try:
Code:
tac file|awk '/PATTERN/{getline;next}' |tac

# 6  
Old 04-12-2011
I wonder what the overhead is for tac? VM, seek, mmap()? Since it works on pipes, it is probably VM intensive! Solaris has "tail -r".
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

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

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

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

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

6. Shell Programming and Scripting

Removing consecutive lines in a file

We have very large transaction logs that have transactions which start with a line that starts with 'Begin :' and ends with a line that starts with 'End :'. For most transactions there is valid data between those two lines. I am trying to get rid of lines that look like this: Begin :... (11 Replies)
Discussion started by: deneuve01
11 Replies

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

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

9. Shell Programming and Scripting

search a word and delete consecutive lines below it

Hi all coders, I need a help to process some data. I have this file, 3 09/21/08 03:32:07 started undef mino Oracle nmx004.wwdc.numonyx.co m Message Text : The Oracle session with the PID 1103 has a CPU time consuming of 999.00... (3 Replies)
Discussion started by: vikas027
3 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