Register
It is currently Sat Jul 26, 2014 1:13 pm

Trying to make a script to remove certain lines of a file.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri May 15, 2009 5:14 pm   

Joined: Fri May 15, 2009 5:12 pm
Posts: 1
Hello everyone, I want to remove the first line and last line of a bunch of html file. This is my script:

#!/bin/bash
F="html/*"
for f in $F
do
cat $f | tail -n +2 >> new.html
done

Currently it removes the first line, how do i remove the last line?

head command needs the number of lines you want to display, which varies from file to file. wc gives the file name in the 2nd column. I have no idea how to use sed. the last line is :
</body></html>


Top
 Profile  
 PostPosted: Fri May 15, 2009 5:49 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
Several ways,

I'll give you a couple ideas,

cat file | wc -l

gives you number of total lines ;)

-------

tac reads the file in reverse

-------

cat yourfile | sed ''$removeline'd' > newfile


Top
 Profile WWW  
 PostPosted: Sat May 16, 2009 4:50 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
here's a one liner using head and tail :)

Code:
tail -n +2 test.txt | head -n$(expr $(cat test.txt | wc -l) - 2)


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sun May 17, 2009 8:21 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
I tried to figure out how to do that with tail, but everything i tried was a huge script lol. Nice, very nice :)

I would probably use the sed version myself though, it's actually one I use when i need to do that very thing.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP