sed command to get the total lines

hi,
I am using the following code to get the first field of all the lines in a file using sed command.But its not printing the last line.Why it is so and how can i read the different fields of each line ?

code:
for i in $(sed = filename.txt | sed 'N;s/\n/ /' | cut -d " " -f1)
do
val1=`sed -n "${i},${i}p" filename.txt | cut -d " " -f1`
echo "$val1"
done

Hi,

mychk.txt
check for 1 2
for 2 also 4
form 3 also 5 6

ksr/Scripts> cat mychk.txt | cut -d' ' -f1-3
check for 1
for 2 also
form 3 also
ksr/Scripts> cat mychk.txt
check for 1 2
for 2 also 4
form 3 also 5 6
ksr/Scripts> cat mychk.txt | cut -d' ' -f1-3
check for 1
for 2 also
form 3 also
ksr/Scripts> cat mychk.txt | cut -d' ' -f1,3
check 1
for also
form also
ksr/Scripts> cat mychk.txt | cut -d' ' -f1,3-4
check 1 2
for also 4
form also 5

Whoa, that's incredibly complex. What's wrong with just cut -d " " -f1 filename.txt?

You don't need sed to enumerate the line numbers in the for loop. Just nl filename.txt would get you file numbering (with some option to nl to also number blank lines -- I seem to recall -ba).

But why do you need the line numbers in the first place? And why do you grab the result into a variable and then echo that variable, when simply sed -n ${i}p filename.txt | cut -d " " -f1 without the backticks and variable and echo would print what you want?

I need to retrieve more than 1 field in all the lines.
code:
for i in $(sed = filename.txt | sed 'N;s/\n/ /' | cut -d " " -f1)
do
val1=`sed -n "${i},${i}p" filename.txt | cut -d " " -f1`
val2=`sed -n "${i},${i}p" filename.txt | cut -d " " -f3`
val3=`sed -n "${i},${i}p" filename.txt | cut -d " " -f4`
printf "%s\t%s\t%s\n" "$val1" "$val2" "$val3"
done

How about this instead?

while read first second third forth rest; do
  printf "%s\t%s\t%s\n" "$first1" "$third" "$forth"
done <filename.txt

its working fine.thanks