Register
It is currently Tue Oct 21, 2014 2:43 am

Remove first 21 lines from a file - 'while's and 'if's


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon May 11, 2009 8:30 am   

Joined: Fri Sep 26, 2008 7:58 am
Posts: 10
Hi all

I need to remove the first 21 lines from a text file, how would I go about this.

It doesn't need to be done in place, so what I tried at first was a while loop to read the first 21 lines, ignore them, then output the rest to another file, but it just seems to throw it into an infinite loop.

Anyone have an idea how I can do this?

I've tried this (keep in mind that my bash experience is limited so there are probably a million things wrong with this, feel free to correct it, but if you could please explain what I have messed up that would help me not to repeat these mistakes in the future):

Code:
line = 1
while read line do
      
      if [line <= 21]
         then echo $ line && $line++
      fi
      else
      {
         echo line >> bar.txt
         line++
      }
done < foo.txt


Also I don't know if the "while read line" causes an increment of line value with every iteration (i.e. do I need something like line++ or is it sorted out for me)

I'm sure there is a simple solution for this, I just lack the bash experience to figure it out.

Also anyone have any recommendations for reading up on bash script, something that starts with syntax for while, for, if etc. and works up to more complex stuff?


Top
 Profile  
 PostPosted: Mon May 11, 2009 3:18 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
I'd probably do it with sed! (I <3 SED)

Code:
sed '1,21d' foo.txt > bar.txt


"While read" isn't going to increment the line numbers. Every iteration of the loop basically just sets "$line" to whatever the contents of the next line is. To do it like that, you'd probably have to introduce a count. It looks like you were trying to increment the line numbers with "line++" but the contents of $line isn't what you want to increment. Actually, there are a lot of syntax errors in here. Here's an example that would work based on your code:

Code:
#!/bin/bash

# Let's set the "count" to "1"
count=1

# Now let's loop through the file.  The "read"
# command will set the "$line" variable to the contents
# of the current line
while read line; do

  # If the count is less than or equal
  # to "21"...
  if [ $count -le 21 ]; then

    # then we echo the line that we're excluding
    # and do nothing else with it...
    echo "removing line #$count: $line"

  # Or else (if greater than 21)...
  else

    # We echo the line into our new file (bar.txt)
    echo $line >> bar.txt

  # end of if statement!
  fi

  # Iterate our "count" up one
  count=$(($count+1))

done < foo.txt


I prefer the one-line option myself ;)

Hope this helps!
-J


Top
 Profile YIM  
 PostPosted: Mon May 11, 2009 3:56 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
If you have a proper version of tail this is also possible.

Code:
tail -n+22 file.txt > new_file.txt


Note that tail using the + sign adds 1 extra to the line count.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed May 13, 2009 1:10 am   

Joined: Fri Sep 26, 2008 7:58 am
Posts: 10
Thanks a lot for the replies everyone. As you can see I am pretty unexperienced when it comes to bash, I didn't even realise that you could use sed with specific line numbers/ranges as options, time to check those man pages a little more thoroughly!

I also thought 'tail' only worked on the end of files, again I need to check these man pages more thoroughly. (I think I used the -f option with it and that is pretty much the extent of my 'tail' use)

I'll probably go with the sed option as it will be simpler to read, but I will read over the other options provided in hopes of improving my bash abilities.

Thanks again :)

EDIT:

I do have one question though.
Why, in that 'read' example provided was a hash symbol used in front of the dollar sign? Is that something like a backslash in java Strings or is it just your way of writing the word "number"?

Code:
   
        # then we echo the line that we're excluding
        # and do nothing else with it...
        echo "removing line #$count: $line"



Top
 Profile  
 PostPosted: Thu May 14, 2009 12:42 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well the -n+<num> syntax is pretty much linux tail specific. Haven't found many unix systems that can handle it.

What it does is just telling tail that it should tail all lines starting from 'your-line' - 1.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon May 18, 2009 2:59 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Sorry, just saw your reply :)

Quote:
Why, in that 'read' example provided was a hash symbol used in front of the dollar sign? Is that something like a backslash in java Strings or is it just your way of writing the word "number"?


Just my way of writing the word "number" :)

The output will increment based on the line number, so it should look like:

removing line #1: Hi, I'm line number 1!
removing line #2: Hi, I'm line number 2!
etc...

Thanks!
-J


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 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