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

👤 Login to reply

    #1  
Old 03-21-2018
ziggy6 ziggy6 is offline
Registered User
 
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; 03-21-2018 at 04:36 PM..
Sponsored Links
    #2  
Old 03-21-2018
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
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 (03-21-2018)
Sponsored Links
    #3  
Old 03-21-2018
ziggy6 ziggy6 is offline
Registered User
 
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 03-21-2018
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
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 03-21-2018
ziggy6 ziggy6 is offline
Registered User
 
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 03-21-2018
delbroooks delbroooks is offline
Registered User
 
You may invoke the multiple delimiters by using awk -F['/*%...] and so in. Basically use square brackets.
Sponsored Links
    #7  
Old 03-21-2018
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
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; 03-21-2018 at 02:21 PM.. Reason: direct output to file
The Following User Says Thank You to rdrtx1 For This Useful Post:
ziggy6 (03-21-2018)
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 07:52 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?