Max89
November 24, 2009, 11:56am
1
Is there a way to grep only one line from a file listing name of files?
If I use head -1 it's ok(the first line only)
If I use head -2 it's not good because I have already checked the first line.
I'd like something like this:
while test $i -le $max
do
grep "$3" `head -$i temp.txt` > temp2.txt
cat temp2.txt
i=`expr $i + 1`
done
but the head command doesn't give only the i line as I would like.
Thank you in advance.
Neo
November 24, 2009, 11:58am
2
Have you tried the -m flag?
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines
are output, grep ensures that the standard input is positioned to just after the last matching line before exiting,
regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep
stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep
does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting
NUM non-matching lines.
Max89
November 24, 2009, 12:20pm
4
Thank you all,but I don't understand 'NR=='
Should I put there $3(the argument I want to compare every line)?
awk '$3=='$i temp.txt
awk '$3'$i temp.txt
Both doesn't work.
You would just be using awk rather than head.
grep "$3" `awk 'NR=='$i temp.txt ` > temp2.txt
Though you could have used head anyhow:
`head -$i temp.txt|tail -1`
If you are simply looping over a file, why not:
i=0
for fname in `cat temp.txt`
do
if test $i -gt $max; then
break
fi
grep "$3" $fname > temp2.txt
cat temp2.txt
i=`expr $i + 1`
done