Hi
i have a file which have a pattern like this
Nov 10 session closed
Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty .
Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk .
Nov 10 afrtetryytr
Nov 10 session closed
Nov 10 Nov 10 03:21:04 EST5EDT 2010
Dec 8 Nov 10 05:03:02 EST5EDT 2010
i have written a code for fatch the line no
Max_LIneNo=`grep -n "Nov 10" purge_exp1.txt | cut -f1 -d: | tail -1
that will give last line no number.
For above example it gives me out put 7
but i need to match only starting 2 words only
so my output should be 6
so where i could make change to get the output 6
please Help me
thanx in advance
Try like this,
Max_LIneNo=`grep -n "^Nov 10" purge_exp1.txt | cut -f1 -d: | tail -1`
OR
awk '/^Nov 10/{i=NR} END {print i}' purge_exp1.txt
1 Like
try:
grep -wn "^Nov 10" purge_exp1.txt | cut -f1 -d: | tail -1
or:
awk '/^Nov 10/{v=NR}END{print v}'
1 Like
hay guys thanx a lot
all script run properly
@ yinyuemi - why you use -wn ? if we use -n it also working.
one more question
: which is fast awk or grep?
grep -w :match the whole form word
like:
echo "1
11
111" |grep 1
1
11
111
echo "1
11
111" |grep -w 1
1
The second question:
it's hard to say which is faster, it's dependent on your files and your requirement. For your question here, awk would be faster, I guess.
Hi guys there is problem again
Max_LIneNo=`grep -wn "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`
then it is not give any output
-------------------------------
Max_LIneNo=`grep -wn "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`
var5=`awk '/^Dec 3/{i=NR} END {print i}' purge_exp1.txt`
but when we write above two command them it will me the line no of Dec 30 instead of DEC 3.
---------- Post updated at 03:42 PM ---------- Previous update was at 03:35 PM ----------
Hi Guys
there is problem again
when i run this command
Max_LIneNo=`grep -wn "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`
it was not give any output to me
but when i run below two command
Max_LIneNo=`grep -n "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`
var5=`awk '/^Dec 3/{i=NR} END {print i}' purge_exp1.txt`
then it will give me line no of Dec 31 line instead of Dec 3
Try this,
awk '$1FS$2 =="Nov 3" {i=NR} END {print i}' inputfile
Hi pravin
Thanx a lot buddy
can you plz explain this portion of command
$1FS$2 =="Nov 3"
it is working
Hi,
$1FS$2=="Nov 3"
$1 is the first field i.e Nov and $2 is second field i.e 3 and FS is field separator.
So, above condition will check $1FS$2 is equal to Nov 3 then assign record number to variable i. Once finished processing of input file , it will print value of i.
one more doubt buddy
if i run this
var5=`awk '$1FS$2 =="Dec 2" {i=NR} END {print i}' input file` then it working fine
but if i run in this way
var="Dec 3"
var5=`awk '$1FS$2 =="$var" {i=NR} END {print i}' input file` then it is not giving any output:(
Use -v option with awk
var5=`awk -v var="Dec 3" '$1FS$2 ==var {i=NR} END {print i}' input file`
there is problem pravin
Hi
my code type is this in var3 i calulate date
var3=$Month1" "$day # var3=Dec 3
Max_LIneNo=`awk -v var="$var3" '$1FS$2 ==$var {i=NR} END {print i}' input File`
but it throw error "it is not correct."
remove $ ... Please check post #11
Max_LIneNo=`awk -v var="$var3" '$1FS$2 ==$var {i=NR} END {print i}' input File`
1 Like
thanx buddy
you helped me alot:)