Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Issue in awk parsing under while loop

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 2 Weeks Ago
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 June 2017, 6:17 PM EDT
Posts: 1,633
Thanks: 46
Thanked 622 Times in 583 Posts
If sPath contains three elements that would suggest an iteration.

Code:
for p in ${sPath[@]}; do
   # do something with $p
done

If $sPath/*12581.log could expand to multiple entries, that suggest another iteration.


Code:
for p in ${sPath[@]}; do
   for f in "$p"/*12581.log; do
       if [[ -s $f ]]; then
           # do something with $f a file that exist
       fi
   done
done

Now, consider this:

Code:
sPath=$(awk -F: 'sub("^dict=", "") {for(i=1;i<NF;i++){print $i}}' file.txt)
find $sPath -name '*12581.log' -type f

Sponsored Links
    #9  
Old Unix and Linux 2 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 3:14 AM EDT
Location: Aachen, Germany
Posts: 10,959
Thanks: 280
Thanked 3,368 Times in 3,101 Posts
How about (ignoring the unproblematic "find: ‘dict’: No such file or directory" error message)

Code:
find $(tr '=:' ' ' < file.txt) -iname "*12581.log"


For your error in post#5:
Quote:
Originally Posted by Don Cragun View Post
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 20 June 2017, 3:44 AM EDT
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Hi,

Can any one tell me whats wrong in my original posted code, since in my actual implementation i can not do awk exact match. Since the data will be having multiple lines of same match pattern. So i am using xmllint to get the desired tag values. Hence i am unable to use this code.

Please refer my initial post wherein i have mentioned only one line in the input. But actually it will be having multiple similar lines with different values under different xml tags.


Code:
cat > file.txt
<xml>
<a>
dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq:
</a>
<b>
dict=/dictr/abcd/d1/wqa:/dictr/abcd/d2/wqb:/dictr/abcd/d3/wqc:
</b>
</xml>

---------- Post updated at 10:01 PM ---------- Previous update was at 01:24 PM ----------

Some how i managed to attain the desired results...
    #11  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Quote:
Originally Posted by ananan View Post
Hi

I am trying to parse a grep output using awk. It works fine individually and not working under the loop with variable name assigned.


Code:
cat > file.txt
dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq:

sample tried code

Code:
Nos=`grep -w "dict" file.txt | awk -F"=" '{print $2}' | awk -F":" '{print NF-1}'`
echo "$Nos"
while [ "$Nos" -ge 1 ]
do
sPath=`grep -w "dict" file.txt | awk -F"=" '{ print $2}' | awk -F":" '{ print $(echo "$Nos") }'`
echo "$sPath" 
if [[ -s "$sPath"/*12581.log ]]; then
    FileName=`ls  "$sPath"/*12581.log |head -1`
fi
  Nos=`expr $Nos- 1`
done
#echo "$FileName"

sample output

Code:
/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq:
/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq:
/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq:

Quote:
Originally Posted by ananan View Post
Hi,

Can any one tell me whats wrong in my original posted code, since in my actual implementation i can not do awk exact match. Since the data will be having multiple lines of same match pattern. So i am using xmllint to get the desired tag values. Hence i am unable to use this code.

Please refer my initial post wherein i have mentioned only one line in the input. But actually it will be having multiple similar lines with different values under different xml tags.


Code:
cat > file.txt
<xml>
<a>
dict=/dictr/abcd/d1/wq:/dictr/abcd/d2/wq:/dictr/abcd/d3/wq:
</a>
<b>
dict=/dictr/abcd/d1/wqa:/dictr/abcd/d2/wqb:/dictr/abcd/d3/wqc:
</b>
</xml>

The following comments explain part of what I believe is wrong with your script in post #1.

Presumably, the first part of your code:

Code:
Nos=`grep -w "dict" file.txt | awk -F"=" '{print $2}' | awk -F":" '{print NF-1}'`
echo "$Nos"

when given the above contents in file.txt will set Nos to:

Code:
/dictr/abcd/d3/wq
/dictr/abcd/d3/wqc

and print those results. You then start a while loop with an invalid test condition:

Code:
while [ "$Nos" -ge 1 ]

the test -ge operator compares two numeric values, but a variable containing two pathnames separated by a <newline> character is not a numeric value.

Then we move on to the last stage of the pipeline in the command substitution in the variable assignment:

Code:
awk -F":" '{ print $(echo "$Nos") }'

I have no idea what the intent of this code is, but what it does is a request to print the field whose field number is formed by concatenating the value of the awk variable named echo (which has not been defined in this script and, therefore, has an empty string value) and the literal string $Nos (shell variables are not expanded inside single-quoted strings). Since that concatenation does not produce a numeric value, trying to print the contents of the field number identified by a non-numeric value gives you a 2nd syntax error. And, if the script hasn't aborted yet from the two syntax errors so far, the shell variable sPath will be set to an empty string. That then leads to an if statement that tests whether or not there is a file matching the filename matching pattern /*12581.log and if there is exactly one file matching that pattern and has a size greater than zero. If more than one file is matched by that pattern, you have a 3rd syntax error (since that test only works with one operand to the test -s operator).

And, finally, after that if statement, you have another command substitution:

Code:
  Nos=`expr $Nos- 1

that is used to reset the value of the Nos shell variable to its current (non-numeric) value to one less than its current value (which is another syntax error) which, therefore, sets Nos to an empty string.

You also have unneeded uses of awk, cat, expr, and grep (some of which have already been commented on by others in this thread) but if given proper arguments, they might not do anything but waste system resources and slow down your script.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing log file and print latest number in loop Ariean Shell Programming and Scripting 1 05-16-2014 05:55 AM
Parsing issue raghunsi Shell Programming and Scripting 2 09-29-2010 12:42 AM
Issue with parsing config variables madhukalyan UNIX for Dummies Questions & Answers 1 01-08-2010 03:06 AM
parsing issue with edi file fwellers Shell Programming and Scripting 5 12-29-2009 03:09 PM
parsing logfiles (performance issue) subin_bala Shell Programming and Scripting 4 05-06-2008 01:35 AM



All times are GMT -4. The time now is 06:36 AM.