Sponsored Content
Top Forums Shell Programming and Scripting Help with script to convert rows to columns Post 303017670 by Don Cragun on Sunday 20th of May 2018 06:46:52 AM
Old 05-20-2018
Note that although your printf happens to work with the data you're using, it is dangerous to assume that no characters in data you're printing will ever be interpreted as format string control characters. You might want to consider one of the following as an alternative to your current code:
Code:
BEGIN {	FS = "[=;]"
}
{	for(i = 2; i <= NF; i++)
		printf("%s=%s\n", $1, $i)
}

or:
Code:
BEGIN {	FS = "[=;]"
	OFS = "="
}
{	for(i = 2; i <= NF; i++)
		print $1, $i
}

or:
Code:
BEGIN {	FS = "[=;]"
}
{	for(i = 2; i <= NF; i++)
		print $1 "=" $i
}

These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script required to convert rows to columns

Hi Friends, I have a log file as below siteid = HYD spc = 100 rset = RS_D_M siteid = DEL spc = 200 rset = RS_K_L siteid = DEL2 spc = 210 rset = RS_D_M Now I need a output like column wise as below. siteid SPC rset HYD 100 RS_D_M (2 Replies)
Discussion started by: suresh3566
2 Replies

2. Shell Programming and Scripting

how to convert columns to rows

Hi, I need a shell script for below requirement Input file P1 - 173310 P2 - 173476 P3 - 173230 P4 - 172737 P1 - 173546 P2 - 173765 P3 - 173876 P4 - 172989 Out put file P1 173310 173546 P2 173476 173765 P3 173230 173876 P4 172737 172989 Suresh (6 Replies)
Discussion started by: suresh3566
6 Replies

3. Shell Programming and Scripting

convert rows into columns

Hi guys Could anyone advise me how to convert my rows into columns from a file My file would be similar to this: A11 A12 A13 A14 A15 ... A1n A21 A22 A23 A31 A41 A51 ... Am1 Am2 Am3 Am4 Am5 ... Amn The number of rows is not the same to the number of columns Thanks in advance (2 Replies)
Discussion started by: loperam
2 Replies

4. Shell Programming and Scripting

convert columns into rows

hi, Apologies if this has been covered. I have requirement where i have to convert a single column into multiple column. My data will be like this - 2 3 4 5 6 Output required - 2 3 4 5 6 (1 Reply)
Discussion started by: Nishithinfy
1 Replies

5. Shell Programming and Scripting

Convert columns to rows in perl script

Hi All I want to have a Perl script which convert columns to rows. The Perl should should read the data from input file. Suppose the input file is 7215484 date to date 173.3 A 1.50 2.23 8.45 10.14 2.00 4.50 2.50 31.32 7216154 month to month (3 Replies)
Discussion started by: parthmittal2007
3 Replies

6. Shell Programming and Scripting

Convert few columns to rows

Hi! Does anybody help me in converting following data: INPUT looks like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70. 400. 80. 200. 150. 210. 30. 100. OUTPUT should look like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70.... (5 Replies)
Discussion started by: lovelinux
5 Replies

7. Shell Programming and Scripting

How to Convert rows in to columns?

Hi Gurus, How to convert rows in to columns using linux shell scripting Input is like (sample.txt) ABC DEF GHI JKL MNO PQR STU VWX YZA BCD output should be (sampleoutput.csv) ABC,DEF,GHI,JKL,MNO PQR,STU,VWX,YZA,BCD (2 Replies)
Discussion started by: infasriniit
2 Replies

8. Shell Programming and Scripting

Convert Rows to Columns

Hi Everyone, Could someone shed some lights on how to convert the records in rows form into column basis. 172.29.59.12 IBM,8255-E8B 102691P 8 65536 MB 6100-04-11-1140 172.29.59.15 IBM,8255-E8B 102698P 4 45056 MB 6100-04-11-1140 IP SYS MODEL ... (6 Replies)
Discussion started by: ckwan
6 Replies

9. Shell Programming and Scripting

Shell script to convert rows to columns

Hi I have a file having the values like below ---------------------------- .set A col1=”ABC” col2=34 col3=”DEF” col4=”LMN” col5=25 .set A .set B col1=55 col3=”XYZ” col4=”PQR” col5=66 .set B .set C col2=”NNN” (1 Reply)
Discussion started by: Suneel Mekala
1 Replies

10. Shell Programming and Scripting

Convert rows to columns

hi folks, I have a sample data like what is shown below: 1,ID=1000 1,Org=CedarparkHospital 1,cn=john 1,sn=doe 1,uid=User001 2,uid=User002 2,ID=2000 2,cn=steve 2,sn=jobs 2,Org=Providence I would like to convert it into the below format: 1,1000,CedarparkHospital,john,doe,User001... (11 Replies)
Discussion started by: vskr72
11 Replies
printf(9r)																printf(9r)

NAME
printf, uprintf - General: Write formatted text to some output device SYNOPSIS
void printf( char *format, va_dcl var_arglist ); void uprintf( char *format, va_dcl var_arglist ); ARGUMENTS
Specifies a pointer to a string that contains two types of objects. One object is ordinary characters, such as ``hello, world'', which are copied to the output stream. The other object is a conversion specification, such as %d, %o, or %x. Each conversion specification causes the routines described here to convert and print for the next argument in the var_arglist argument. The printf formats %d and %x print 32 bits of data. To obtain 64 bits of data, use %ld and %lx. Specifies the argument list. DESCRIPTION
The printf and uprintf routines are scaled-down versions of the corresponding C library routines. The printf routine prints diagnostic information directly on the console terminal and writes ASCII text to the error logger. Because printf is not interrupt driven, all system activities are suspended when you call it. The uprintf routine prints to the current user's terminal. Interrupt service routines should never call uprintf. It does not perform any space checking, so you should not use this routine to print verbose messages. The uprintf routine does not log messages to the error log- ger. You introduce conversion specifications by using the percent sign (%). Following the %, you can include: Zero or more flags, which modify the meaning of the conversion specification. An optional minus sign (-), which specifies left adjustment of the converted value in the indicated field. An optional digit string, which specifies a field width. If the converted value has fewer characters than the field width, the printf and uprintf routines pad the value with blanks. By default, these routines pad the value on the left. If the conversion string that specifies the value is left justified, these routines pad the value on the right. If the field width begins with a zero, these routines pad the values with zeros instead of blanks. The character h or l, which specifies that a following d, i, o, u, x, or X corre- sponds to an integer or longword integer argument. You can use an uppercase L or a lowercase l. A character that indicates the type of conversion to be applied. A field width or precision can be an asterisk (*) instead of a digit string. If you use an asterisk, you can include an argument that sup- plies the field width or precision. The flag characters and their meanings are as follows: The result of the conversion is left justified within the field. The result of a signed conversion always begins with a sign (+ or -). If the first character of a signed conversion is not a sign, the printf and uprintf routines pad the value on the left with a blank. If the blank and plus sign (+) flags both appear, these routines ignore the blank flag. The result has been converted to a different format. The value is to be converted to an alternative form. For c, d, s, and u conversions, this flag has no effect. For x or X conversions, the printf and uprintf routines pad a nonzero result on the left with 0x or 0X. These routines support the following formats which kernel module writers find particularly useful: Allows decoding of error registers. Prints the character argument. Converts the integer argument to decimal. Converts the integer argument to octal. Converts the integer argument to hexadecimal. Prints the character argument. The printf and uprintf routines print the argument until encountering a null char- acter or until printing the number of characters specified by the precision. If the precision is zero or the precision has not been speci- fied, these routines print the character argument until encountering a null character. Converts the unsigned integer argument to a decimal value. The result must be in the range of 0 through 4294967295, where the upper bound is defined by MAXUINT. Prints a percent sign (%). The routines convert no argument. The following line shows the format of the printf routine with the % b conversion character: printf("reg=%b ", regval, "<base> <arg>*"); In this case, base and arg are defined as: The output base expressed as a control character. For example, 10 gives octal and 20 gives hexadecimal. A sequence of characters. The first character gives the bit number to be inspected (origin 1). The second and subsequent characters (up to a control character, that is, a character <=32) give the name of the register. The following shows a call to printf: printf("reg=%b ", 3, "10 2BITTWO1BITONE "); This example would produce the following output: reg=2<BITTWO,BITONE> The following shows the format of the printf routine with the % r and % R conversion characters: printf("%r R", val, reg_desc); The conversion characters have the following meanings: Allows formatted printing of bit fields. This code produces a string of the format: ""<bit field descriptions>"" Allows formatted printing of bit fields. This code produces a string of the format: ""0x%x<bit field descriptions>"" You use a reg_desc structure to describe the individual bit fields. To describe multiple bit fields within a single word, you can declare multiple reg_desc structures. The reg_desc structure is defined as follows: struct reg_desc { unsigned rd_mask; /* mask to extract field */ int rd_shift; /* shift for extracted */ /* value, - >>, + << */ char *rd_name; /* field name */ char *rd_format; /* format to print field */ struct reg_values *rd_values; /* symbolic names of */ /* values */ }; The members of the reg_desc structure have the following meanings: Specifies an appropriate mask to isolate the bit field within a word and val argument (AND). Specifies a shift amount to be done to the isolated bit field. The shift is done before printing the isolated bit field with the rd_format member and before searching for symbolic value names in the rd_values member. If non-NULL, specifies a bit field name to label any output from rd_format or searching rd_values. If rd_format and rd_values are NULL, rd_name is printed only if the iso- lated bit field is non-NULL. If non-NULL, specifies that the shifted bit field value is printed using this format. If non-NULL, specifies a pointer to a table that matches numeric values with symbolic names. The routine searches the rd_values member and prints the symbolic name if it finds a match. If it does not find a match, it prints ``???''. The following is a sample reg_desc entry: struct reg_desc dsc[] = { /* mask shift name format values */ { VPNMASK, 0, "VA", "0x%x", NULL }, { PIDMASK, PIDSHIFT, "PID", "%d", NULL }, { 0, 0, NULL, NULL, NULL }, }; The printf and uprintf routines also accept a field number, zero filling to length. For example: printf(" %8x ",regval); The maximum field size is 11. NOTES
Some device drivers might have used the tprintf routine to print on a specified terminal or on the system console if no terminal was pro- vided. It is recommended that you discontinue use of tprintf because it will not be supported in future releases of the operating system. RETURN VALUES
None printf(9r)
All times are GMT -4. The time now is 02:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy