Sponsored Content
Top Forums Shell Programming and Scripting Update a field using awk and keep the formatting. Post 302910527 by Don Cragun on Friday 25th of July 2014 05:39:29 AM
Old 07-25-2014
Anytime you reset a numbered field (such as $2=x or sub(regex, replacement, $n)) for n not equal to 0), $0 is recalculated. For the general case, you could try something like:
Code:
(	printf "  2 4 6\n"
	printf "1\t3\t7\t\n"
	printf "\t xyz\t 2\t 3 with more fields\n"
) | awk '
{	n = split($0, delims, /[^[:space:]]*/)
	$2+=3
	$3-=1
	for(i = 1; i <= n; i++)
		printf("%s%s", delims[i], $i)
	print ""
}'

which produces the output:
Code:
  2 7 5
1	6	6	
	 xyz	 5	 2 with more fields

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

The loop printing the modified lines and the following print command can be simplified to a single printf command if you know that there are always three fields.

Last edited by Don Cragun; 07-25-2014 at 06:48 AM.. Reason: Add note explaining why problem occurs...
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CSV formatting with prefixing, appending and padding field

Hi I have a very large csv file with some hundreds of thousands of rows of data. The data is in the following format: Up to four alpha numeric characters for the first word. This is either set as 2 characters followed by 2 spaces, or as a single 4character word. This is then followed by an 8... (7 Replies)
Discussion started by: meself
7 Replies

2. UNIX for Dummies Questions & Answers

Conversion problem with date field and formatting.

Hi, My input file contains the record(s) as below with space as FS. 01-01024180 35000 MV010 02/03/09 0306 03060226 03 02-00410330 470000 MV010 02/03/09 0301 03010276 03 1. I need to convert the field 02/03/09 (dd/mm/yy) to yyyymmdd yet retain the Field separator. Using the modified... (2 Replies)
Discussion started by: talk2pawee
2 Replies

3. Shell Programming and Scripting

How to check field formatting of input file?

Hi, I had input file with below data, abcdefghij;20100903040607;1234567891;GLOBAL; Having values of fields with seperated by semi-colon (;) and ended with line feed (\n). Through shell script, how can I check the field formatting? Thanks in advance. (18 Replies)
Discussion started by: Poonamol
18 Replies

4. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

6. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

7. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

8. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

9. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. Shell Programming and Scripting

awk to assign points to variables based on conditions and update specific field

I have been reading old posts and trying to come up with a solution for the below: Use a tab-delimited input file to assign point to variables that are used to update a specific field, Rank. I really couldn't find too much in the way of assigning points to variable, but made an attempt at an awk... (4 Replies)
Discussion started by: cmccabe
4 Replies
CUBRID_FETCH_FIELD(3)							 1						     CUBRID_FETCH_FIELD(3)

cubrid_fetch_field - Get column information from a result and return as an object

SYNOPSIS
object cubrid_fetch_field (resource $result, [int $field_offset]) DESCRIPTION
This function returns an object with certain properties of the specific column. The properties of the object are: o $name -column name o $table -name of the table that the column belongs to o $def -default value of the column o $max_length -maximum length of the column o $not_null -1 if the column cannot be NULL o $primary_key -1 if the column is a primary key o $unique_key -1 if the column is an unique key o $multiple_key -1 if the column is a non-unique key o $numeric -1 if the column is numeric o $blob -1 if the column is a BLOB o $type -the type of the column o $unsigned -1 if the column is unsigned o $zerofill -1 if the column is zero-filled PARAMETERS
o $result -$result comes from a call to cubrid_execute(3) o $field_offset - The numerical field offset. If the field offset is not specified, the next field (that was not yet retrieved by this function) is retrieved. The $field_offset starts at 0. RETURN VALUES
Object with certain properties of the specific column, when process is successful. FALSE on failure. EXAMPLES
Example #1 cubrid_fetch_field(3) example <?php $conn = cubrid_connect("localhost", 33000, "demodb"); $req = cubrid_execute($conn, "SELECT event_code,athlete_code,nation_code,game_date FROM game WHERE host_year=1988 and event_code=20001;"); var_dump(cubrid_fetch_row($req)); cubrid_field_seek($req, 1); $field = cubrid_fetch_field($req); printf(" --- Field Properties --- "); printf("%-30s %s ", "name:", $field->name); printf("%-30s %s ", "table:", $field->table); printf("%-30s "%s" ", "default value:", $field->def); printf("%-30s %d ", "max length:", $field->max_length); printf("%-30s %d ", "not null:", $field->not_null); printf("%-30s %d ", "primary key:", $field->primary_key); printf("%-30s %d ", "unique key:", $field->unique_key); printf("%-30s %d ", "multiple key:", $field->multiple_key); printf("%-30s %d ", "numeric:", $field->numeric); printf("%-30s %d ", "blob:", $field->blob); printf("%-30s %s ", "type:", $field->type); printf("%-30s %d ", "unsigned:", $field->unsigned); printf("%-30s %d ", "zerofill:", $field->zerofill); cubrid_close_request($req); cubrid_disconnect($conn); ?> The above example will output: array(4) { [0]=> string(5) "20001" [1]=> string(5) "16681" [2]=> string(3) "KOR" [3]=> string(9) "1988-9-30" } --- Field Properties --- name: athlete_code table: game default value: "" max length: 0 not null: 1 primary key: 1 unique key: 1 multiple key: 0 numeric: 1 blob: 0 type: integer unsigned: 0 zerofill: 0 PHP Documentation Group CUBRID_FETCH_FIELD(3)
All times are GMT -4. The time now is 11:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy