LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-07-2008, 09:02 AM   #1
samyboy
LQ Newbie
 
Registered: Nov 2006
Posts: 12

Rep: Reputation: 0
sed: print section of file from string to end of file


Hi,

I've got a file with garbage (empty lines, weird chars) before the real content of my file.

In this case, I am deleting everything before the '<?xml' string.

My way is too much complicated because it uses three commands and a temp file ...

sed -n '/<?xml/,$p' file.xml > file.tmp
sed 's/^.*<\?xml/<?Xml/1' file.tmp > file.xml
rm -f file.tmp


Any knowledge to change it into one command ?

Thanks.
Sam
 
Old 02-07-2008, 09:59 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Hi,

Is this what you want/need:

sed -i -n '/<?xml/,$p' infile

This will edit the infile directly (the -i option) and print everything from the first <?xml to the last line (/<?xml/,$p). The -n will only print that what is wanted.

If you do want a backup of the original file you can extend the -i option. -ibak will generate a <file>.bak file and a -iorg will create a <file>.org file.

BTW: The -i option is only present from sed version 4.0 and higher.

Hope this helps.
 
Old 02-08-2008, 08:19 AM   #3
samyboy
LQ Newbie
 
Registered: Nov 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
sed -i -n '/<?xml/,$p' infile
Hi,

thanks for your answer. However, the line you mentionned is the same as my first line.

I still need to merge it with this one below which "cleans" the garbage before the "<?xml" string (at the same line):

sed 's/^.*<\?xml/<?Xml/1' file.tmp
 
Old 02-08-2008, 08:40 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Hi,

Ok. Well in that case your initial set of commands aren't that complicated. I would personally re-write it to the following:

$ sed -n '/<?xml/,$p' infile | sed 's/^.*<\?xml/<?Xml/' > infile.tmp
$ mv infile.tmp infile


Too my knowledge there's no way to make a more compound sed (or other prog like awk) statement. But I could be wrong.....

Oh, you could make this a 'one-liner':
$ sed -n '/<?xml/,$p' infile | sed 's/^.*<\?xml/<?Xml/' > infile.tmp && mv infile.tmp infile

I used '&&' between the commands instead of a ';' to make sure that the move part is only done if the previous command succeeds.
 
Old 02-26-2008, 07:23 AM   #5
samyboy
LQ Newbie
 
Registered: Nov 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Wink

Nice thanks!
 
  


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
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 07:17 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM


All times are GMT -5. The time now is 06:23 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration