Hi there,

Here is the problem:
I have a Text file with several hundreds of lines and I want to store line 100 to 200 in another text file. I wrote the following piece of code but result is all of the lines not just the ones that I need.

#!/bin/bash

  echo enter file name
  read fname

   exec<$fname

   a=0
   while read line
   do
    if test "$line" -gt 100 && "$line" -lt 200
        then
        a=$line
        echo "$a"
        break
    fi
   done <$fname

any suggestion is very much appreciated.

First of all, I don't believe you. The code you gave may not possibly produce more than one line of output (because of break at line 15).

That said
-- line 11 means
(a) test "$line" -gt 100
(b) if (a) returns success (exit code 0), execute the command "$line" -lt 200

-- $line is a contents of the line you've just read, not a sequence number. Taking your word that "result is all of the lines", I suspect that your file consists of numbers, one per line, all of them less than 100 (this is the only scenario in which your script may run with no errors).

Of course the right way to achieve your goal is a sed script:

sed -n -e '100,200p'

If you insist on a pure bash with no external utilities, then

# read and ignore first 100 lines:
for ((i=0; i<100; i++)); do read line; done

# read and echo next 100 lines:
for ((i=0; i<100; i++)); do read line; echo $line; done
# That's it

Thanks for your quick reply. I am not very experienced in shell programming. I just thought I would give it a try as I have been doing this manually which was a pain! And yes you are completely right! I assumed by using $line, I am counting the number of lines!

The sed command seems easy to work with, I googled it but I still do not understand the p at the end (-->200p) and as I said I would like to store this 100 lines in a file lets say file named fname1 and the next 100 to the fname2. For printing in to a file I used -w NameoftheFile. Which did not work.

Thanks for your quick reply. I am not very experienced in shell programming. I just thought I would give it a try as I have been doing this manually which was a pain! And yes you are completely right! I assumed by using $line, I am counting the number of lines!

The sed command seems easy to work with, I googled it but I still do not understand the p at the end (-->200p) and as I said I would like to store this 100 lines in a file lets say file named fname1 and the next 100 to the fname2. For printing in to a file I used -w NameoftheFile. Which did not work.

I would like to correct myself. I used the following lines

sed -n -e '100,107p' > newfile3.txt
   sed -n -e '108,127p' > newfile4.txt

after running the script newfile3.txt had what I expect to see but newfile4.txt was empty. I assumed that might have something to do with p.
Could you plz help me with this?

Hi SakuraPink!

Sounds like you're making progress!

The 'p' at the end is for 'print'. Take a look at the sed man page for more info about that.

If you're using these sed lines in a similar way to your original script, that would explain why the second file is blank. Sed isn't going to process lines 100 - 107 and stop there. It's going to continue to the end of your input, leaving nothing for your second sed command to process.

A better way might be to have sed look at the file directly each time:

#!/bin/bash

# Prompt for filename
read -p "Enter file name: " fname

# Print lines 100 - 107 into newfile3.txt
sed -n -e '100,107p' $fname > newfile3.txt

# Print lines 108 - 107 into newfile4.txt
sed -n -e '108,127p' $fname > newfile4.txt

I hope this helps!

The sed command seems easy to work with, I googled it but I still do not understand the p at the end (-->200p)

By default sed prints every line it works with. The -n option suppresses this (usually unwanted) behaviour. Now that you've get rid of unwanted lines, you still need to print those you need. That's what a p command is doing.

Thanks a lot Gromit. It helped. ::)

nezachem Thanks for your explanation. I also looked at some tutorials and now I understand it to some extent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.