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.

Trying to use awk to check values and multiple

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
ziggy6's Unix or Linux Image
ziggy6 ziggy6 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 21 March 2018, 2:30 PM EDT
Posts: 32
Thanks: 15
Thanked 0 Times in 0 Posts
Trying to use awk to check values and multiple

I am trying to use AWK to read a file, comma delimited, and check each field to see if it has a suffix of - (dash , minus sign) if so then I want to either move the minus sign the the beginning of the field or take the numeric portion of the field and multiply it by negative 1 to get the field signed properly with the negative sign preceding the number.

Here is a sample of my data



Code:
16361.62 , 4785.75 , 458.00 , 765.00 ,011018, 110.50 , 200.00- , 59, 25 , 443041
.72 , 222032.98 , 46325.27 , 47363.74 , 101643.13 , 776121.82, 11686.72 , 3082.1
2
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , 105.50-, 76461.75
, 14153.18 , 364, 153
68187.16 , 22005.05 , 6767.50 , 1607.00 ,09,01,18, 881.00-, , 46872.51 , 14042.4
4 , 274, 113
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , 105.50-, 76461.75
, 14153.18 , 364, 153


Last edited by RudiC; 4 Weeks Ago at 04:36 PM..
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
RavinderSingh13's Unix or Linux Image
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 11 April 2018, 7:35 AM EDT
Location: Chennai
Posts: 2,715
Thanks: 609
Thanked 1,298 Times in 1,165 Posts
Hello ziggy6,

Not exactly sure about your complete requirement but based on your statements only this solution is. Could you please try following and let me know if this helps.


Code:
awk -F" , " '{for(i=1;i<=NF;i++){if($i~/-/){sub(/-/,"",$i);$i="-"$i}}} 1' OFS=" , "   Input_file

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
ziggy6 (4 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
ziggy6's Unix or Linux Image
ziggy6 ziggy6 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 21 March 2018, 2:30 PM EDT
Posts: 32
Thanks: 15
Thanked 0 Times in 0 Posts
R Singh, Looks like we have a winner. I am not well versed on AWK, only grep and sed. I will study this and see if I can understand it. Thanks for the help!


Code:
16361.62 , 4785.75 , 458.00 , 765.00 ,011018, 110.50 , -200.00 , 59, 25 , 443041
.72 , 222032.98 , 46325.27 , 47363.74 , 101643.13 , 776121.82, 11686.72 , 3082.1
2
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , -105.50, 76461.75
, 14153.18 , 364, 153
68187.16 , 22005.05 , 6767.50 , -1607.00 ,09,01,18, 881.00, , 46872.51 , 14042.4
4 , 274, 113
110896.62 , 24052.37 , 2955.50 , 2032.50 ,10,01,18, 1185.50 , -105.50, 76461.75
, 14153.18 , 364, 153

    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
RavinderSingh13's Unix or Linux Image
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 11 April 2018, 7:35 AM EDT
Location: Chennai
Posts: 2,715
Thanks: 609
Thanked 1,298 Times in 1,165 Posts
Hello ziggy6,

No worries, following is the explanation of code which may help you to understand awk better.


Code:
awk -F" , " '                ##-F means to set field delimiter as " , " space comma space for each line.
{
  for(i=1;i<=NF;i++){        ##Starting a for loop which starts from i=1 to till value of NF whose value will be number of fields and do following.
   if($i~/-/){               ##Checking condition here if value of a field is having -(dash) in it then do following.
     sub(/-/,"",$i);$i="-"$i}##Using sub of awk which will substitute dash with NULL in that specific field AND then adding dash before value of current field.
}}
1                            ##1 will print the current line either edited or non-edited one.
' OFS=" , " Input_file       ##Setting OFS as space comma space here and mentioning Input_file name here too.

Thanks,
R. Singh
Sponsored Links
    #5  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
ziggy6's Unix or Linux Image
ziggy6 ziggy6 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 21 March 2018, 2:30 PM EDT
Posts: 32
Thanks: 15
Thanked 0 Times in 0 Posts
R Singh,
I found a problem with part of the data, 1607.00 became -1607.00 and -881.00 became 881.00
Sponsored Links
    #6  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
delbroooks's Unix or Linux Image
delbroooks delbroooks is offline
Registered User
 
Join Date: Mar 2018
Last Activity: 26 March 2018, 1:26 PM EDT
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
You may invoke the multiple delimiters by using awk -F['/*%...] and so in. Basically use square brackets.
Sponsored Links
    #7  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by ziggy6
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 18 April 2018, 11:57 AM EDT
Location: Houston, Texas, USA
Posts: 1,106
Thanks: 0
Thanked 404 Times in 382 Posts


Code:
awk '{
   c=split($0,a, " *,* *");
   for (i=1;i<=c;i++) if(a[i] ~ /-/) {w=a[i]; sub(/-/,"",a[i]); a[i]="-"a[i]; sub(w, a[i])}
} 1' Input_file > Output_file


Last edited by rdrtx1; 4 Weeks Ago at 02:21 PM.. Reason: direct output to file
The Following User Says Thank You to rdrtx1 For This Useful Post:
ziggy6 (4 Weeks Ago)
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
Check multiple patterns in awk kannan13 Shell Programming and Scripting 3 10-14-2015 05:17 AM
How to get index values for multiple matches in the same line with awk? chilicuil Shell Programming and Scripting 6 07-14-2015 01:39 PM
Array in awk outputs multiple values alan Shell Programming and Scripting 1 11-09-2011 10:00 PM
Use AWK to check for numeric values? (BASH script) TauntaunHerder Shell Programming and Scripting 4 07-17-2011 02:10 PM
matching multiple values in awk VGR Shell Programming and Scripting 0 06-12-2009 11:37 AM



All times are GMT -4. The time now is 05:52 AM.