result in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting result in variable
# 1  
Old 05-15-2011
result in variable

Hi all,

I'll try to get a result from a search with "awk" into a variable.
It works for those examples:
Code:
findfirstline=`awk 'BEGIN{ mycount  = 1 } { if(mycount == 1 && /^Video/){ row=NR; print row; mycount = 0; }}' ${i}`
findlastline=`awk '/^Video/ {row=NR} END{print row}' ${i}`

But it does not for this:
Code:
findpicline=`awk 'BEGIN{ counter = 0; for (i = "${findfirstline}"; i <= "${findlastline}"; i++)} { if(NR == i && /^Picture/){ counter++; }} END{print counter; }' ${i}`

And also not for this:
Code:
findpicline=`awk 'BEGIN{ counter = 0; } { while(NR >= "${findfirstline}" && NR <= "${findlastline}" && /^Picture/){ counter++; print counter; }} END{print counter; }' ${i}`

By not working I mean: the counter is "0" instead of "9" which is the number of the matches. What am I doing wrong?


Thanks a lot in advance for help!

tempestas
# 2  
Old 05-15-2011
Code:
#!/bin/ksh
#
# how to pass SHELL vars into AWK...
#
myShellVar='value'

awk -v myAWKvar="${myShellVar}" 'NR==myAWKvar' myFile
#or
awk 'NR==myAWKvar' myAWKvar="${myShellVar}" myFile

# 3  
Old 05-15-2011
Thanks vgersh99!
What a stupid mistake that I made Smilie

But, still, i f I try to implement that in variable it does not work:
Code:
findpicline=`awk 'NR >= awkv1 && NR <= awkv2' awkv1="${findfirstline}" awkv2="${findlastline}" $i | awk 'BEGIN{ counter = 0; } { if(/^Picture/){ counter++; }} END{print counter; }'  ${i}`

It seems as if the whole file is searched instead of only the lines I specified Smilie

Has anyone a suggestion?

Thanks!
# 4  
Old 05-15-2011
Could you post a sample input file and a desired output, please!
I don't quite follow why you need to run awk so many times.....
# 5  
Old 05-15-2011
Well, what I actually wanna do is a little more complex:

I have many files like the example file.

First, I wanna call specific files, i.e. they contain a certain string in their file names. There are at least 5 files of each search term.
For example: "Subj03"

So my solution is:
Code:
#!/bin/sh
### call the function like this: ./tryRead.sh Subj03 
if [ $# -eq 0 ]		# if no args on command line, prompt for input
then
     echo -n "\nPlease enter search string: "
     read NAME
else
     NAME=$1
fi

find . -name "*${NAME}*.log.txt" -type f -maxdepth 1 > ${NAME}.txt
nofiles=`awk ' END { print NR } ' ${NAME}.txt`

for ((j=1; j<=$nofiles; j++)); do
i=`awk 'NR == awkj' awkj="${j}" ${NAME}.txt | awk '{print $0}'`

findfirstline=`awk 'BEGIN{ mycount  = 1 } { if(mycount == 1 && /^Video/){ row=NR; print row; mycount = 0; }}' ${i}`
findlastline=`awk '/^Video/ {zeile=NR} END{print zeile}' ${i}`
#### all those attempts don't work
#findpicline=`awk 'NR >= awkv1 && NR <= awkv2' awkv1="${findfirstline}" awkv2="${findlastline}" $i | awk 'BEGIN{ counter = 0; } { if(/^Picture/){ counter++; }} END{print counter; }'  ${i}`
#findpicline=`awk 'BEGIN{ 'NR >= awkv1 && NR <= awkv2' awkv1="${findfirstline}" awkv2="${findlastline}" $i; counter = 0; } { if(NR >= awkv1 && NR <= awkv2 && /^Picture/){ counter++; }} END{print counter; }'  ${i}`
#findpicline=`awk 'NR >= awkv1 && NR <= awkv2' awkv1="${findfirstline}" awkv2="${findlastline}" $i | awk 'BEGIN{ counter = 0; } { while(NR >=awkv1 && NR <=awkv2 && /^Picture/){ counter++; }} END{print counter; }'  ${i}`
#findpicline=`awk 'BEGIN{ counter = 0; NR >= awkv1 && NR <= awkv2 awkv1="${findfirstline}" awkv2="${findlastline}" $i; } { while(NR >= awkv1 && NR <= awkv2 && /^Picture/){ counter++; }} END{print counter; }'  ${i}`
#awk 'NR >= awkv1 && NR <= awkv2' awkv1="${findfirstline}" awkv2="${findlastline}" $i
#findpicline=`awk 'BEGIN{ counter = 0; for (i = "${findfirstline}"; i <= "${findlastline}"; i++)} { if(NR == i && /^Picture/){ counter++; }} END{print counter; }' ${i}`
#findpicline=`awk 'BEGIN{ counter = 0; } { while(NR >= "${awkfindfirstline}" && NR <= "${awkfindlastline}" && /^Picture/){ counter++; }} END{print counter; }' ${i}`

findpicline=`awk 'NR >= awkv1 && NR <= awkv2' awkv1="${findfirstline}" awkv2="${findlastline}" $i | awk 'BEGIN{ counter = 0; } { if(/^Picture/){ counter++; }} END{print counter; }'  ${i}`

nolines=`expr ${findlastline} - ${findfirstline} - ${findpicline}`
echo $findfirstline "and" $findlastline "and" $nolines "and" $findpicline #"und" $datende 
if [ $nolines -eq 80 ]
	then
findline=`awk '/^video summary/ {z=NR} END{print z}' ${i}`
findline=`expr ${findline} + 2`
echo StimDisp GR RT > ${i%.*}_list.txt
awk '/^Video/ {print $2, $4, $5}' $i >> ${i%.*}_list.txt 
holdon=`awk '/^Video/ {print $2, $4, $5}' ${i}`
datende=`awk 'END { print NR }' ${i}`
echo StartT EndT ${i%.*}_tmp.txt
awk -F: 'NR >= v1 && NR <= v2' v1="${findline}" v2="${datende}" $i | awk '{print $9, $10}' >> ${i%.*}_tmp.txt 
paste $ECHO "${i%.*}_list.txt" $ECHO "${i%.*}_tmp.txt" > ${i%.*}_SOI.txt
rm $ECHO "${i%.*}_list.txt"
rm $ECHO "${i%.*}_tmp.txt"
  else echo "The number of stimuli is" $nolines " but should be 80. Check the files manually!"
fi

done

I basically wanna check that I have really only 80 lines starting with "Video".
Sorry for the very long and not elegant code, but I am just a beginner.

Thanks in advance for help!

Last edited by tempestas; 05-15-2011 at 01:46 PM.. Reason: was too fast pressing the "submit" button and forgot to attach an example for the result
# 6  
Old 05-15-2011
Trying to compute the number of records that start with "Video" based on first and last observed won't work here -- you have records mixed in with the Video records such that last - first will be greater than 80.

If all you need is a count of records that begin with "Video" this might be better:
Code:
count=$(grep -c "^Video" text.txt)

If you must use awk:
Code:
count=$( awk '$1 == "Video" { count++; } END{ printf( "%d\n", count ); }'  test.txt)

Both of these assume Kshell or bash where back-quotes are deprecated in favour of $(cmd).
# 7  
Old 05-15-2011
Something along these lines...
nawk -f tempe.awk TestSubj03LogFile.log.txt

tempe.awk:
Code:
/^Video/ { v[$2]=($4 OFS $5);start[$2]=$7;next}
/^filename/{f=1;next}
f {
  n=split($1, a, "[\\\\.]")
  movie=a[n-1]
  print movie, v[movie], start[movie],start[movie]+$NF
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

2. UNIX for Dummies Questions & Answers

I save the result in a variable

I have friends that this command worked perfectly, but I would like to save the result in a variable, which I have not achieved var=prueba.txt echo $var | cut -d "." -f 1 prueba I need to do this but does not work me salida=echo $var | cut -d "." -f 1 echo "result:$salida" ... (8 Replies)
Discussion started by: tricampeon81
8 Replies

3. Shell Programming and Scripting

Result of 'cut' into variable

Hello, I would like to have a result of a cut-command in a variable. The file "t.dat" has the following content: Company 001.239879123.OB1X.672W12.STS UNOLD.001.02 My bash-script: Header="" Header=$(cut -c1-160 t.dat | head -1) echo $Header ... (9 Replies)
Discussion started by: API
9 Replies

4. Shell Programming and Scripting

How to get result of sqlcmd output in variable?

Hi All, I am looking for a solution. I am executing below command in shell script: SQL= "select count(*) from dual" sqlcmd --login /oracle/baccess.xml --sql "$SQL". I can see the result while executing above shell script. but I want to store the value of query output irrespective the... (2 Replies)
Discussion started by: lokendrasb
2 Replies

5. Shell Programming and Scripting

How to combine two variable result?

Hi, i have two variables i.e lck_ckm_customer=ckm_customer and(present in some script A) table=ckm_customer(present in script B) in script B i am executing this part if ; then --- ---- ---- fi Now, while comparing in my log file i am getting this result if but i... (2 Replies)
Discussion started by: gnnsprapa
2 Replies

6. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

7. Shell Programming and Scripting

Taking sed result in a variable

Hi All, How can i take the following code having three seds into a variable : echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/+/g' | sed 's/\#/'$job_date'/g' I want to get the result stored in a script variable i tried var2=`echo "$DateFileFormat" | sed 's/\./\\\\./g' |... (4 Replies)
Discussion started by: abhinav192
4 Replies

8. Shell Programming and Scripting

Assign result to variable

Hi friends, firstly, i can run following expression and i took 100 value. sqlplus -s username/password@TTTEST @umt.sql umt.sql exists "select t.deger from parametre t where t.id=30". result of this query =100 i need to assign this value(100) to variable(for example x... (2 Replies)
Discussion started by: temhem
2 Replies

9. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies

10. UNIX and Linux Applications

How to get a result of a cmd into variable?

Hi , I am trying following . I need to get a result of an autosys cmd into a unix variable. The autosys cmd is autostatus -G jpm_day_today Please help me in storing the value returned by this cmd into a unix variable. Appreciate your time to read this post. (1 Reply)
Discussion started by: manchau
1 Replies
Login or Register to Ask a Question