LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   remove certain lines from file based on start of line except beginning and ending (https://www.linuxquestions.org/questions/linux-newbie-8/remove-certain-lines-from-file-based-on-start-of-line-except-beginning-and-ending-4175450811/)

nwalsh88 02-19-2013 11:13 AM

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

colucix 02-19-2013 11:26 AM

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

nwalsh88 02-20-2013 03:02 AM

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.

chrism01 02-20-2013 04:14 AM

Works for me, maybe its a version thing
Code:

sed --version
GNU sed version 4.2.1


colucix 02-20-2013 10:00 AM

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.

nwalsh88 02-20-2013 10:20 AM

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


All times are GMT -5. The time now is 05:14 PM.