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
    #1  
Old Unix and Linux 06-12-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
Issue in awk parsing under while loop

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:

Sponsored Links
    #2  
Old Unix and Linux 06-13-2017
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 14 September 2017, 11:33 AM EDT
Posts: 1,641
Thanks: 46
Thanked 625 Times in 586 Posts
I know you said "[you're] trying to parse a grep output using awk", but what are you, ultimately, trying to accomplish. What's the end goal?

What's the result you expect out of this?

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

Sponsored Links
    #3  
Old Unix and Linux 06-13-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
Hi

I want

Code:
echo "$sPath"

should produce the output


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

    #4  
Old Unix and Linux 06-13-2017
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 14 September 2017, 11:33 AM EDT
Posts: 1,641
Thanks: 46
Thanked 625 Times in 586 Posts
Quote:
Originally Posted by ananan View Post
Hi

I want

Code:
echo "$sPath"

should produce the output


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

OK, here the thing, AWK doesn't need the help of GREP, nor do you need to invoke AWK twice to achieve that result.

Consider the following:

Code:
awk -F: 'sub("^dict=", "") {for(i=1;i<NF;i++){print $i}}' file.txt
/dictr/abcd/d1/wq
/dictr/abcd/d2/wq
/dictr/abcd/d3/wq

Sponsored Links
    #5  
Old Unix and Linux 06-13-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
Hi

What about assigning the output to a variable if certain file exists.


Code:
bash-3.00$ awk -F: 'sub("^dict=", "") {for(j=1;j<NF;j++) system("test -f " "$j"/*12581.log)==0 { print $j }}' file.txt
awk: syntax error near line 1
awk: bailing out near line 1
bash-3.00$ 

need to assign the output of awk to a variable for further processing. the following part of the previous code needs to be merged to assign.

if [[ -s "$sPath"/*12581.log ]]; then
    FileName=`ls  "$sPath"/*12581.log |head -1`
fi

Sponsored Links
    #6  
Old Unix and Linux 06-13-2017
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 14 September 2017, 11:33 AM EDT
Posts: 1,641
Thanks: 46
Thanked 625 Times in 586 Posts
Assigning to a variable:

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

What do you expect to be the result of this? What would it yield?

Code:
"$sPath"/*12581.log

Sponsored Links
    #7  
Old Unix and Linux 06-13-2017
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
i will be having files ending with *12581.log in only one of the returned path.
So i want to use that path for further processing.

simple terms awk will give three paths and definitely in one path *12581.log will be present.
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 11:05 PM.