Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-21-2018
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..
# 2  
Old 03-21-2018
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)
# 3  
Old 03-21-2018
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
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
# 5  
Old 03-21-2018
R Singh,
I found a problem with part of the data, 1607.00 became -1607.00 and -881.00 became 881.00
# 6  
Old 03-21-2018
You may invoke the multiple delimiters by using awk -F['/*%...] and so in. Basically use square brackets.
# 7  
Old 03-21-2018
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)
Login to Reply

« Previous Thread | Next Thread »
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 increment values in column jiam912 Shell Programming and Scripting 6 06-24-2015 07:49 AM
Reading multiple values from multiple lines and columns and setting them to unique variables. FMMOLA Shell Programming and Scripting 7 05-23-2014 02:42 PM
Read record from the text file contain multiple separated values & assign those values to variables ketanraut Shell Programming and Scripting 7 03-06-2014 12:25 PM
Check null values column Vivekit82 Shell Programming and Scripting 1 09-04-2013 12:54 PM
How to check values stored in variable? ramsavi Red Hat 1 05-10-2013 04:32 AM
How to check for values in array? ramsavi Red Hat 1 05-09-2013 05:33 AM
How do you [e]grep for multiple values within multiple files? callumw UNIX Desktop Questions & Answers 4 03-18-2012 09:21 PM
check for param values raopatwari Shell Programming and Scripting 4 03-18-2011 05:34 PM
Check values in file sandy1028 Shell Programming and Scripting 4 07-21-2010 08:38 PM
how to check the variable values is blank julirani Shell Programming and Scripting 2 12-06-2008 04:24 PM


All times are GMT -4. The time now is 02:43 AM.

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