LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-19-2013, 12:13 PM   #1
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Rep: Reputation: Disabled
remove certain lines from file based on start of line except beginning and ending


Hi, I have multiple files which all have multiple entries like below..

Quote:
00..................
06..................
06..................
06..................
06..................
70..................
80..................
00..................
06..................
06..................
06..................
06..................
70..................
80..................
00..................
06..................
06..................
06..................
06..................
70..................
80..................
I want to have a script that will delete all the 00 records except for the first one and delete all the 80 records except for the last one.

Any help will be greatly appreciated.

Thanks
 
Old 02-19-2013, 12:26 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Maybe using something like:
Code:
sed '1!{/^00/d};$!{/^80/d}' file
It means don't delete a line starting with 00 if it is the first line in the file (but delete from the rest of the file) and don't delete a line starting with 80 if it is the last line in the file (but delete from the rest).
 
Old 02-20-2013, 04:02 AM   #3
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for your reply.

When i run the above command this is the result i get:

Quote:
sed: 0602-404 Function 1!{/^00/d};$!{/^80/d} cannot be parsed.
 
Old 02-20-2013, 05:14 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Works for me, maybe its a version thing
Code:
 sed --version
GNU sed version 4.2.1
 
Old 02-20-2013, 11:00 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Indeed, googlin' around it seems an error from UNIX sed. I don't know how to modify it to let it work in not-GNU sed, but until someone more experienced steps in, here is an awk solution:
Code:
awk '/^00/ && !_[00]++; /^80/{rec = $0}; !/^00/ && !/^80/; END { print rec }' file
The difference between the awk and the sed solution is that the former works even if the first 00 line is not the first line of the file and the last 80 line is not the last one, whereas the latter makes these assumptions.
 
1 members found this post helpful.
Old 02-20-2013, 11:20 AM   #6
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for your help colucix

I eventually got it working with the help of a colleague.

the script we used was:

Quote:
awk '
BEGIN { zz = ""; ee = "" }
$0 ~ /^00/ && zz == "" { zz = $0 ; print ; next }
$0 ~ /^80/ { ee = $0 ; next }
$0 !~ /^00/ { print }
END { print ee }
' file

exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove lines based upon the previous line Adzrules Linux - Newbie 2 11-16-2012 07:18 AM
Remove many lines based on content of other file anderssk Programming 13 06-23-2011 11:34 AM
[SOLVED] How to remove line breaks from lines ending with a comma jasonws Linux - General 1 11-10-2010 01:03 PM
Remove chars from beginning of line using vi Johnomal Linux - General 4 08-31-2009 02:55 AM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration