Sed one-liner to drop data from beginning of file?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sed one-liner to drop data from beginning of file?
Hello, I'm trying to drop all data from the beginning of a file up to the first occurence of a specific opening xml tag. I need this operation to run as fast as possible since it will be used on huge files (several GB) that, for the most part, don't have any newlines in them.
This is the best I can come up with sofar and it doesn't quite work...
So it doesn't remove the garbage in the beginning, and also removes too many 'foo' tags because of the greedy pattern match.
How can I get sed to match *everything* up to a token, not just line-by-line? Or alternatively, is there some other command I can use that would still run as fast?
Thanks forrestt, but that throws away every foo except one. I need to preserve all the foo tags I can (basically I just have to drop some malformed xml from the beginning of the file, and then keep processing from the first opening tag).
I don't know, if my "solution" ;-) fits your needs - it's a little bit strange. You don't have a ungreedy qualifier without using Perl compatible regex (that's why you should check if can do the job in Perl). I went another way:
Code:
sed -r '1,/<foo /{s/$/|/;s/(<foo )/|\1/;s/[^|]*\|//;/^\|*$/d;s/\|//}' foo.xml
To explain it: I started with a line selection like you:
Code:
jan@jack:~/tmp> sed -r '1,/<foo /{ # start a code block
s/$/|/ # append a | at the end of lines
s/(<foo )/|\1/ # insert a | before the first "<foo "
s/[^|]*\|// # remove everything up to the first | in every line
/^\|*$/d # now delete empty lines (which don't contain a "<foo ")
s/\|// # remove the remaining | at the end of the "<foo " line
}' foo.xml
this solution doesn't work, if a line - after the garbage at the file's head - does not contain a </foo> - lodi wanted to cleanup a html file containing garbage at the beginning of the file up to the first "<foo>" tag. You must extend the script so, that after the first match all lines are printed (and start with "<foo>", not "</foo>") - untested:
Code:
awk ' BEGIN { found = 0; }
/<foo>/{
if (found == 0) {
start = match($0,"<foo>");
print substr($0,RSTART);
found = 1;
next;
}
else
print $0;
}
{ if (found == 1) print $0; } ' file
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.