I have a bash function that goes through a list of files and greps for a certain word and writes it into a file. The problem is I get every instance of the word, I just want to get the first instance. I came across a solution by adding head -1
with the grep, but now, my function just hangs when I call it.
83 function processAppLogs {
84 for i in `find $log_des -name $fname`
85 do
86 p=$i
87 d=${p//applog.txt/unmountTimestampList.txt}
88 grep "UNMOUNTED" $i >> $d
89 grep "PATIENT ID" | head -1 | $i >> $d
90 done
91 }
I'm looking to grep only the first instance of "PATIENT ID" but I think I might have the syntax wrong? Is that the proper way to grep the first instance and write that to a file?
As a side not to @fedorqui's answer.
What you got wrong is the order of commands. The
head
must occur after thegrep
and then should the redirection come. Like