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
MAXDB_FETCH_FIELD_DIRECT(3)						 1					       MAXDB_FETCH_FIELD_DIRECT(3)

maxdb_fetch_field_direct - Fetch meta-data for a single field

       Procedural style

SYNOPSIS
mixed maxdb_fetch_field_direct (resource $result, int $fieldnr) DESCRIPTION
Object oriented style mixed maxdb_result::fetch_field_direct (int $fieldnr) maxdb_fetch_field_direct(3) returns an resource which contains field definition information from specified resultset. The value of fieldnr must be in the range from 0 to number of fields - 1. RETURN VALUES
Returns an resource which contains field definition information or FALSE if no field information for specified fieldnr is available. Object attributes +-----------+---------------------------------------------------+ |Attribute | | | | | | | Description | | | | +-----------+---------------------------------------------------+ | name | | | | | | | The name of the column | | | | |max_length | | | | | | | The maximum width of the field for the result | | | set. | | | | | type | | | | | | | The data type used for this field | | | | | decimals | | | | | | | The number of decimals used (for integer fields) | | | | +-----------+---------------------------------------------------+ EXAMPLES
Example #1 Object oriented style <?php $maxdb = new maxdb("localhost", "MONA", "RED", "DEMODB"); /* check connection */ if (maxdb_connect_errno()) { printf("Connect failed: %s ", maxdb_connect_error()); exit(); } $query = "SELECT name, cno from hotel.customer ORDER BY name"; if ($result = $maxdb->query($query)) { /* Get field information for column 'SurfaceArea' */ $finfo = $result->fetch_field_direct(1); printf("Name: %s ", $finfo->name); printf("Table: %s ", $finfo->table); printf("max. Len: %d ", $finfo->max_length); printf("Flags: %d ", $finfo->flags); printf("Type: %d ", $finfo->type); $result->close(); } /* close connection */ $maxdb->close(); ?> Example #2 Procedural style <?php $link = maxdb_connect("localhost", "MONA", "RED", "DEMODB"); /* check connection */ if (maxdb_connect_errno()) { printf("Connect failed: %s ", maxdb_connect_error()); exit(); } $query = "SELECT name, cno from hotel.customer ORDER BY name"; if ($result = maxdb_query($link, $query)) { /* Get field information for column 'cno' */ $finfo = maxdb_fetch_field_direct($result, 1); printf("Name: %s ", $finfo->name); printf("Table: %s ", $finfo->table); printf("max. Len: %d ", $finfo->max_length); printf("Flags: %d ", $finfo->flags); printf("Type: %d ", $finfo->type); maxdb_free_result($result); } /* close connection */ maxdb_close($link); ?> The above example will output something similar to: Name: CNO Table: max. Len: 4 Flags: -1 Type: 0 SEE ALSO
maxdb_num_fields(3), maxdb_fetch_field(3), maxdb_fetch_fields(3). PHP Documentation Group MAXDB_FETCH_FIELD_DIRECT(3)
All times are GMT -4. The time now is 02:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy