Sponsored Content
Full Discussion: awk script Equivalent .
Top Forums UNIX for Beginners Questions & Answers awk script Equivalent . Post 303040455 by Eric7giants on Tuesday 29th of October 2019 06:22:30 PM
Old 10-29-2019
awk script Equivalent .

Hello. I wrote some code for an awk command but I want to learn to turn it into an awk script but am stuck. I have a file (data.csv) that has the following data:


Code:
ADD,1,3,5,8,10,11,54
SUB,1,2,3,4
ADD,15,18,21,42,37
ADD,1,1,1,0,0,3,16
ADD,4,1,8,0,4,6,13,16,17,20,8,6,4
SUB,13,8

If the line has ADD, I added. Subtract for lines that start with SUB. I am then to return the sum of all the lines. This is the awk code that I have:

Code:
awk -F',' '{if($1 == "ADD") {for(i=2;i<=NF;i++) t+=$i; print t;} else { t=$2;for(i=3;i<=NF;i++) t-=$i;print t; }t=0}' data.csv

The only issue with the output is that it provides a sum for each line but not the total sum. So how would I turn this into a script?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk equivalent script

Hi All, I need a script that prints a blank line after finding that the next lines first field doesn't match the current lines first field. I really want to do this in awk to improve my own knowledge but the best I can come up with is a bash script. I'd finally like to understand how awk... (10 Replies)
Discussion started by: pondlife
10 Replies

2. Shell Programming and Scripting

shell script equivalent for tcl function

Hello, I need experts help in converting the below tcl function to korn shell function equivalent. proc lsNetMaskToBits {mask} { set dw ; # Top N bits set set dw 0x ; # Make sure it's hexadecimal, else XOR fails. puts "lsNetMaskToBits dw $dw" set dw ; # Complement => low 32-N bits... (1 Reply)
Discussion started by: JackMelson
1 Replies

3. Linux

Need SCP script equivalent to FTP script .

Hi, Currently i'm using the folllowing FTP acript and its working. echo "open $server" > ftp_file echo "user $user $password" >> ftp_file echo "cd $remote_dir" >> ftp_file echo "lcd $local_dir" >> ftp_file echo "put $file">> ftp_file echo "bye" >> ftp_file ftp -nv < ftp_file I've... (1 Reply)
Discussion started by: vickramshetty
1 Replies

4. Shell Programming and Scripting

Bash Script equivalent KSH script?

I always find BASH easier than ksh. At my home, i have written this bash script. I am finding it hard to write its equivalent in ksh, any suggestions? ###################################### #return seconds since `00:00:00 1970-01-01 UTC' (a GNU extension)... (1 Reply)
Discussion started by: boy18nj
1 Replies

5. Windows & DOS: Issues & Discussions

awk to findstr equivalent

Hi, I 'd like to translate this command from awk to findstr on Windows DOS FILE: str1 server1 a str1 server2 a str2 server1 b str2 server2 b Awk command: awk ' $1 ~/str1/ { print $2, $3 } ' file.txt OUTPUT: server1 a server2 a Thanks, (1 Reply)
Discussion started by: phamp008
1 Replies

6. Shell Programming and Scripting

awk equivalent of regex

Hi all, Can someone tell me what's the (g)awk equal of this simple regex to find ip addresses in urls: egrep "^http://{1,3}\.{1,3}\.{1,3}\.{1,3}(:{1,5})?/"Input: http://10.0.0.1/query.exe http://11y10x09w:80/howaboutme http://192.168.100.190:1234/takeme.gpg Output:... (8 Replies)
Discussion started by: r4v3n
8 Replies

7. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

8. Shell Programming and Scripting

awk equivalent code in C for printing NF

Hi all ! whether anyone in forum knows what awk will use while printing number of fields in file(NF) ? for example awk END'{print NF}' file prints number of columns in file if anyone knows equivalent code in C kindly share or explain logic behind it (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

9. Shell Programming and Scripting

Java - Arrays.binarySearch function equivalent in awk

Hi all Does anyone know Java-Arrays.binarySearch function equivalent in awk I tried like this but it's not correct one,it just returns array index if and only when searched value available in array, for some reason if searched value not found then I want to return upper nearest neighbour index.... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

10. Shell Programming and Scripting

sed Equivalent for awk/grep

Any equivalent command using awk or grep? sed -n "/^$(date --date='10 minutes ago' '+%b %_d %H:%M')/,\$p" /abc.log (7 Replies)
Discussion started by: timmywong
7 Replies
ALTER 
TYPE(7) PostgreSQL 9.2.7 Documentation ALTER TYPE(7) NAME
ALTER_TYPE - change the definition of a type SYNOPSIS
ALTER TYPE name action [, ... ] ALTER TYPE name OWNER TO new_owner ALTER TYPE name RENAME ATTRIBUTE attribute_name TO new_attribute_name [ CASCADE | RESTRICT ] ALTER TYPE name RENAME TO new_name ALTER TYPE name SET SCHEMA new_schema ALTER TYPE name ADD VALUE new_enum_value [ { BEFORE | AFTER } existing_enum_value ] where action is one of: ADD ATTRIBUTE attribute_name data_type [ COLLATE collation ] [ CASCADE | RESTRICT ] DROP ATTRIBUTE [ IF EXISTS ] attribute_name [ CASCADE | RESTRICT ] ALTER ATTRIBUTE attribute_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ CASCADE | RESTRICT ] DESCRIPTION
ALTER TYPE changes the definition of an existing type. There are several subforms: ADD ATTRIBUTE This form adds a new attribute to a composite type, using the same syntax as CREATE TYPE (CREATE_TYPE(7)). DROP ATTRIBUTE [ IF EXISTS ] This form drops an attribute from a composite type. If IF EXISTS is specified and the attribute does not exist, no error is thrown. In this case a notice is issued instead. SET DATA TYPE This form changes the type of an attribute of a composite type. OWNER This form changes the owner of the type. RENAME This form changes the name of the type or the name of an individual attribute of a composite type. SET SCHEMA This form moves the type into another schema. ADD VALUE [ BEFORE | AFTER ] This form adds a new value to an enum type. If the new value's place in the enum's ordering is not specified using BEFORE or AFTER, then the new item is placed at the end of the list of values. CASCADE Automatically propagate the operation to typed tables of the type being altered, and their descendants. RESTRICT Refuse the operation if the type being altered is the type of a typed table. This is the default. The ADD ATTRIBUTE, DROP ATTRIBUTE, and ALTER ATTRIBUTE actions can be combined into a list of multiple alterations to apply in parallel. For example, it is possible to add several attributes and/or alter the type of several attributes in a single command. You must own the type to use ALTER TYPE. To change the schema of a type, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the type's schema. (These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the type. However, a superuser can alter ownership of any type anyway.) To add an attribute or alter an attribute type, you must also have USAGE privilege on the data type. PARAMETERS
name The name (possibly schema-qualified) of an existing type to alter. new_name The new name for the type. new_owner The user name of the new owner of the type. new_schema The new schema for the type. attribute_name The name of the attribute to add, alter, or drop. new_attribute_name The new name of the attribute to be renamed. data_type The data type of the attribute to add, or the new type of the attribute to alter. new_enum_value The new value to be added to an enum type's list of values. Like all enum literals, it needs to be quoted. existing_enum_value The existing enum value that the new value should be added immediately before or after in the enum type's sort ordering. Like all enum literals, it needs to be quoted. NOTES
ALTER TYPE ... ADD VALUE (the form that adds a new value to an enum type) cannot be executed inside a transaction block. Comparisons involving an added enum value will sometimes be slower than comparisons involving only original members of the enum type. This will usually only occur if BEFORE or AFTER is used to set the new value's sort position somewhere other than at the end of the list. However, sometimes it will happen even though the new value is added at the end (this occurs if the OID counter "wrapped around" since the original creation of the enum type). The slowdown is usually insignificant; but if it matters, optimal performance can be regained by dropping and recreating the enum type, or by dumping and reloading the database. EXAMPLES
To rename a data type: ALTER TYPE electronic_mail RENAME TO email; To change the owner of the type email to joe: ALTER TYPE email OWNER TO joe; To change the schema of the type email to customers: ALTER TYPE email SET SCHEMA customers; To add a new attribute to a type: ALTER TYPE compfoo ADD ATTRIBUTE f3 int; To add a new value to an enum type in a particular sort position: ALTER TYPE colors ADD VALUE 'orange' AFTER 'red'; COMPATIBILITY
The variants to add and drop attributes are part of the SQL standard; the other variants are PostgreSQL extensions. SEE ALSO
CREATE TYPE (CREATE_TYPE(7)), DROP TYPE (DROP_TYPE(7)) PostgreSQL 9.2.7 2014-02-17 ALTER TYPE(7)
All times are GMT -4. The time now is 10:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy