Print specific entries in a file

Guys,
I need your advice. I am attempting to print 1-n lines and then n-z lines.

If i enter in number it works fine e.g output lines 1 - 10 and 11 - 20, then it works fine.. But it fails when i pass string, as it outputs all the lines and not what i am asking for.

CAMHOSTS=`cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | wc -l`
export CAM_HALF=`expr $CAMHOSTS / 2`
echo "$CAM_HALF"

cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | sed -n '1,/$CAM_HALF/p'

NextLine=`expr $CAM_HALF + 1`

echo "$NextLine"
LAST=`echo "9999"`
cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | sed -n '/$NextLine/,/$LAST/p'


Output of the scripts

+ cat /tmp/NOT_DUPLICATED_HOSTS.txt
+ grep -i cam
+ wc -l
+ CAMHOSTS=261
+ expr 261 / 2
+ CAM_HALF=130
+ export CAM_HALF
+ echo 130
130
+ cat /tmp/NOT_DUPLICATED_HOSTS.txt
+ grep -i cam
+ sed -n '1,/$CAM_HALF/p'
 ipe-genp4n-nfs_13241231 LDN_UX_PRD
 ln7d5267apx_1323444 LDN_UX_DEV


+ expr 130 + 1
+ NextLine=131
+ echo 131
131
+ echo 9999
+ LAST=9999
+ cat /tmp/NOT_DUPLICATED_HOSTS.txt
+ grep -i cam
+ sed -n '/$NextLine/,/$LAST/p'

No output


Environment Variable substitution does not work between single quote characters. Your sed statements are in single quote characters. Try double quote characters.

Also in the sed statments I think that you need a single space character before the p .

I have enetered quotes and that looked to work...

But i have noticed that the oupur does not give a 50%/50% output.

More a 60%/50% ouput

261 Lines

Displays 146 lines first
Then 73 Lines next

cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i cam | sed -n "1,/$CAM_HALF/ p "

How can i ensure i get 50%/50% ratio

[root@x60 test]# a=$[`wc -l text | cut -f1 -d' '`] ; b=$[$a/2] ; echo "Lines: $a ; 1/2: $b" ; echo -n "first part: " ; head -$b text | wc -l ; echo -n "second part: " ; tail -$[$a-$b] text | wc -l
Lines: 33783 ; 1/2: 16891
first part: 16891
second part: 16892

?

Your original code is written as if each record in the input file starts with a field containing the line number. If this is not so, then your sed commands need attention.

USED awk to get around the issue,

cat /tmp/NOT_DUPLICATED_HOSTS.txt | grep -i WAT |awk "NR>=$NextLine&&NR<=$WATHOSTS"