Trying to use awk to check values and multiple


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to use awk to check values and multiple
# 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 05: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
This User Gave Thanks to RavinderSingh13 For This Post:
# 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 03:21 PM.. Reason: direct output to file
This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Merge values from multiple directories into one file in awk or bash

I am trying to merge or combine all $1 values in validation.txt from multiple directories into one new file and output it here tab-delimited:/home/cmccabe/Desktop/20x/total/total.txt. Each $2 value and the header would then be a new field in total.txt. I am not sure how to go about this as cat is... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Beginners Questions & Answers

awk GSUB read field values from multiple text files

My program run without error. The problem I am having. The program isn't outputting field values with the column headers to file.txt. Each of the column headers in file.txt has no data. MEMSIZE SECOND SASFoundation Filename The output results in file.txt should show: ... (1 Reply)
Discussion started by: dellanicholson
1 Replies

4. Shell Programming and Scripting

Check multiple patterns in awk

I need to check if 2 values exists in the file and if they are equal print 0. output.txt: ------------ 1 2 3 4 5 6 Inputs: a=1 b=2 My pattern matching code works but I am trying to set a counter if both the pattern matches which does not work.If the count > 0,then I want to... (3 Replies)
Discussion started by: kannan13
3 Replies

5. Shell Programming and Scripting

Check file from multiple files is empty using awk

I am passing multiple files in awk & since one of the file is empty(say file3) so the same gets skipped & logic goes for toss. Need suggestion/help in checking and putting additional checks for the same awk -F, 'FNR==1 {++filecounter} filecounter==1 {KRL=$2;next} filecounter==2... (8 Replies)
Discussion started by: siramitsharma
8 Replies

6. Shell Programming and Scripting

How to get index values for multiple matches in the same line with awk?

Hi, I know that echo "bob alice robert alice" | awk '{print index($0,"alice")}' 5Will output the index of the first alice match, is there any way to get the index of all matches?, eg: echo "bob alice robert alice" | awk 'unknown magic' 5:18Thanks for your time. (6 Replies)
Discussion started by: chilicuil
6 Replies

7. Shell Programming and Scripting

Get multiple values from an xml file using one of the following commands or together awk/perl/script

Hello, I have a requirement to extract the value from multiple xml node and print out the values to new file to compare. Would be done using either awk/perl or some unix script. For example sample input file: ..... ..... <factories xmi:type="resources.jdbc:DataSource"... (2 Replies)
Discussion started by: slbmind
2 Replies

8. Shell Programming and Scripting

Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner. I use this code on ASCII files I need to report against. awk 'BEGIN { tokens = 0 tokens = 0 tokens = 0 } { for (token in tokens) { if ($1 == token){print $0; tokens++;}}} END {for (token in tokens){ if( tokens ==... (1 Reply)
Discussion started by: alan
1 Replies

9. Shell Programming and Scripting

Use AWK to check for numeric values? (BASH script)

Hi, I'm quite new to scripting, but know a few AWK statements. I have the following line in my script: hostname=`echo $file | awk 'BEGIN{FS=OFS="."}{$NF=""; NF--; print}'` I use this in my script to rename files, which are similar to this: name.mvkf.mkvfm.mkfvm.1 To the... (4 Replies)
Discussion started by: TauntaunHerder
4 Replies

10. Shell Programming and Scripting

matching multiple values in awk

How will you change the 5th column in the data file with the value in the second column in the error_correction.txt file. You have to match an extra variable, column 3 of the error_correction file with column 6 of the data.txt file. data.txt: vgr,bugatti veron,,3.5,Maybe,6,.......,ax2,....... (0 Replies)
Discussion started by: VGR
0 Replies
Login or Register to Ask a Question