awk gsub not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk gsub not working as expected
# 1  
Old 11-27-2017
awk gsub not working as expected

Hi Experts,
Need your kind help with gsub awk.

Below is my pattern:
Code:
"exec=1_host_cnt=100_dup=4_NameTag=targetSrv_500.csv","'20171122112948"," 100"," 1"," 1"," 4","400","","",
"  aac sample exec ""hostname=XXXXX commandline='timeout 10 openssl  speed -multi 2 ; exit 0'""      ","-1","-1","1"," 234","",""," 70","","400",
"81.124","182","37","158.87","52.0","120.0","120.0","1511.3275","38.87","255"

While running without gsub it is fine
Code:
find . -maxdepth 1 -type f -iname "out*.csv" -exec awk -F "," '{print "No of Errors : " $13}' {} \;

Retruns 13 column value with double quotes which represents no of errors occured which I want to get rid of. So using gsub but it is returning me strange behavior(sometime 1 , sometime 2)
Code:
find . -maxdepth 1 -type f -iname "out*.csv" -exec awk -F "," '{print "No of Errors : "gsub(/^"|"$/, "", $13)}' {} \;

Later I want to extend the implementation to if condition
Code:
if no of errors is ==0. then print no error
else do something

BR,
PD

Last edited by Don Cragun; 11-27-2017 at 05:37 AM.. Reason: Change B tags to CODE tags.
# 2  
Old 11-27-2017
gsub returns the number of substitutions, not the manipulated string.
The latter remains at the given location, here $13
# 3  
Old 11-27-2017
AWK store string to a variable

HI,

Thank you for the insight.

In my case, there must be an way to save the manipulated string to a variable and treat later for comparison to numeric value.
I want to extract $13 column and compare it to numeric value something like
Code:
find . -maxdepth 1 -type f -iname "out*.csv" -exec awk -F "," '{gsub(/^"|"$/, "", $13);print $13; if ($13 == 0) print "Yes"; else print "Failure";}' {} \;

Please suggest since I tried with lot R&D I am not sure about the risk and might be other simple way to do it.

BR,
PD

Last edited by pradyumnajpn10; 11-27-2017 at 06:19 AM..
# 4  
Old 11-27-2017
Quote:
Originally Posted by pradyumnajpn10
Hi Experts,
Need your kind help with gsub awk.

Below is my pattern:
Code:
"exec=1_host_cnt=100_dup=4_NameTag=targetSrv_500.csv","'20171122112948"," 100"," 1"," 1"," 4","400","","",
"  aac sample exec ""hostname=XXXXX commandline='timeout 10 openssl  speed -multi 2 ; exit 0'""      ","-1","-1","1"," 234","",""," 70","","400",
"81.124","182","37","158.87","52.0","120.0","120.0","1511.3275","38.87","255"

While running without gsub it is fine
Code:
find . -maxdepth 1 -type f -iname "out*.csv" -exec awk -F "," '{print "No of Errors : " $13}' {} \;

Retruns 13 column value with double quotes which represents no of errors occured which I want to get rid of. So using gsub but it is returning me strange behavior(sometime 1 , sometime 2)
Code:
find . -maxdepth 1 -type f -iname "out*.csv" -exec awk -F "," '{print "No of Errors : "gsub(/^"|"$/, "", $13)}' {} \;

Later I want to extend the implementation to if condition
Code:
if no of errors is ==0. then print no error
else do something

BR,
PD
In addition to what MadeInGermany has already said, I'm confused by the "pattern" you've shown us above. I would guess that rather than that being a pattern, it is a sample of the data that might be in one of the files whose filename matches the pattern out*.csv. If that is the case, note that the three lines in your sample file contain 10, 11, and 10 fields, respectively, so field #13 on each line in that file is an empty string. So, the output from your script above (without the gsub()) for that output would be:
Code:
No of Errors : 
No of Errors : 
No of Errors :

and the code with the gsub() would produce the output:
Code:
No of Errors : 0
No of Errors : 0
No of Errors : 0

I assume that neither of these are really what you want as the output. Please explain more clearly what you are trying to do and show us the output you hope to get from the files you are processing.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-27-2017
Hi ,

Sorry to confuse.
Yes it is one line/pattern inside the out*.csv file.There may be many out*.csv files.
I am trying to search all the out*.csv files and if the 13 the column is equal to zero then Return success or else Print failure/exit.I am now trying something like below
Code:
find . -maxdepth 1 -type f -iname "out*.csv" -exec awk -F "," '{gsub(/^"|"$/, "", $13);print $13; if ($13 == 0) print "Success"; else print "Failure";}' {} \;

I am not sure about the risk of doing like this. Please suggest if there is any simple way to do it. It should show the File name along with the status.


Ex:
out1.csv: failed
out2.csv: Pass

Second try:
Code:
find . -maxdepth 1 -type f -name "out*.csv" -exec awk -F "," '{printf "File:" "{}: No. of Errors : " ;gsub(/^"|"$/, "", $13);printf $13;if ($13 == 0) print " : Success"
; else print " : Failure";}' {} \;

BR,
PD

Last edited by pradyumnajpn10; 11-27-2017 at 06:57 AM..
# 6  
Old 11-27-2017
I'm not sure what "risk" you expect. As is, the awk script will read only the files and output to screen. I don't think any damage can be done hereby.
There are, howsoever, some improvement opportunitites.
- terminate the find command by a + so only one single awk invocation occurs.
- if you need to preserve $13 for later processing (in the same script, that is), assign it to a variable first, and then operate on / from that.
- use the awk FILENAME variable for your desired output.
- use awk's printf statement to output to the same line.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-27-2017
Thank you Rudic and Don Cragun for your valuable time.. You Rock..Smilie
I will dig more into the + sign you have mentioned and the third valuable statement


Long live Unix.com
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk matching script not working as expected

This is my ubuntu version: $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial $ /bin/awk -V | head -n1 bash: /bin/awk: No such file or directory I have gotten a script that helps me to parse,... (14 Replies)
Discussion started by: delbroooks
14 Replies

2. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

3. Shell Programming and Scripting

awk not working as expected in script

Dear all, I had script which used to work, but recently it is not working as expected. I have command line in my shell script to choose the following format from the output_elog and perform some task afterwards on As you see, I want all numbers in foramt following RED mark except for... (12 Replies)
Discussion started by: emily
12 Replies

4. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

5. UNIX for Dummies Questions & Answers

Gsub regex not working

I have a number of files that I pass through awk/gsub. I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not. Examples: Initial data: /Volumes/Daniel/Public/Drop Box/_Hellsing_Ultimate_OVA_-_10_.mkv gsub & regex: gsub("\]+\]","" ... (4 Replies)
Discussion started by: unknownn
4 Replies

6. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

7. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

8. Shell Programming and Scripting

Var substitution in awk - not working as expected

countA=`awk '/X/''{print substr($0,38,1)}' fName | wc -l` countB=`wc -l fName | awk '{print int($1)}'` echo > temp ratio=`awk -va=$countA -vc=$countB '{printf "%.4f", a/c}' temp` After running script for above I am getting an error as : awk: 0602-533 Cannot find or open file -vc=25. The... (3 Replies)
Discussion started by: videsh77
3 Replies

9. Shell Programming and Scripting

awk not working as expected with BIG files ...

I am facing some strange problem. I know, there is only one record in a file 'test.txt' which starts with 'X' I ensure that with following command, awk /^X/ test.txt | wc -l This gives me output = '1'. Now I take out this record out of the file, as follows : awk /^X/ test.txt >... (1 Reply)
Discussion started by: videsh77
1 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question