Sponsored Content
Top Forums UNIX for Advanced & Expert Users Conversion of rows to columns using awk based om column value Post 302972075 by Don Cragun on Friday 29th of April 2016 03:42:51 PM
Old 04-29-2016
Are these added comments enough to explain what my script does, or do you still not understand how something works?
Code:
awk '
/^dn:/ {# For input lines starting with "dn:":
	# if this is not the 1st input line, add a newline to previous output;
	# then, in either case, add the current input line (without the
	# terminating newline character) as the start of a new output line.
	printf("%s%s", (NR > 1) ? "\n" : "", $0)
	# Skip to next input line.
	next
}
{	# For other input lines:
	# If the line contains a colon, add "|" as a field separator and add the
	# current input line (withut the terminating newline character) to the
	# current output line; otherwise, just add the current input line
	# (without the terminating newline character) to the current output
	# line.
	printf("%s%s", (sub(/:/, "&")) ? "|" : "", $0)
}
END {	# When we hit EOF on the input file, add a trailing newline to the last
	# output line.
	print ""
}' file

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies

2. Shell Programming and Scripting

Arrange output based on rows into columns

Hi All, I would like to ask help on how can i achieve below output. Inputfile: Oct11,apa1-daily,01:25:01 Oct11,apa2-daily,01:45:23 Oct12,apa1-daily,02:30:11 Oct12,apa2-daily,01:55:01 Oct13,apa1-off,01:43:34 Oct13,apa2-off,01:22:04 Desired output: Clients ... (3 Replies)
Discussion started by: mars101
3 Replies

3. UNIX for Dummies Questions & Answers

conversion and transpositions of columns and rows

Hi, I am wondering how to do this more efficiently than I am currently doing it: I have these 3 columns 3315 E14 1 397 E14 2 321 E14 3 27 E14 4 7 E14 5 2 E14 6 But I like them to be displayed like this: 1 2 ... (5 Replies)
Discussion started by: danieladna
5 Replies

4. Shell Programming and Scripting

Skipping rows based on columns

Hi, suppose I have the following file and certain rows have missing columns, how do i skip these rows and create an output file which has all the columns in it E/N Ko_exp %err Ko_calc %err diff diff- diff+ 0.95 ======== ======= ==== ======= ==== ===== ===== =====... (12 Replies)
Discussion started by: ramky79
12 Replies

5. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

6. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

7. UNIX for Dummies Questions & Answers

Columns to rows conversion in unix

Hi All, My requirement is to convert the rows to columns, please help me how to do in one command. Ex: source file is having data like ABC,XYZ,123,987,KKK,XXX,666 Need output like ABC XYZ 987 KKK XXX 666 Regards, Pavan. (3 Replies)
Discussion started by: madsongtel
3 Replies

8. Shell Programming and Scripting

Convert rows to columns based on condition

I have a file some thing like this: GN Name=YWHAB; RC TISSUE=Keratinocyte; RC TISSUE=Thymus; CC -!- FUNCTION: Adapter protein implicated in the regulation of a large CC spectrum of both general and specialized signaling pathways GN Name=YWHAE; RC TISSUE=Liver; RC ... (13 Replies)
Discussion started by: raj_k
13 Replies

9. Solaris

Rows to column conversion in Solaris

i am using the command pkginfo -l | /usr/xpg4/bin/grep -e 'VENDOR' -e 'NAME' -e 'VERSION' >> /tmp/test1.txt in order to get name,vendor and version of the applications installed on Solaris machine.The output stored in test1.txt in as shown below: NAME: Solaris Zones (Usr) VERSION: ... (4 Replies)
Discussion started by: omkar.jadhav
4 Replies

10. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies
SOELIM(1)						      General Commands Manual							 SOELIM(1)

NAME
soelim - interpret .so requests in groff input SYNOPSIS
soelim [-Crtv] [-I dir] [files ...] It is possible to have whitespace between the -I command line option and its parameter. DESCRIPTION
soelim reads files and replaces lines of the form .so file by the contents of file. It is useful if files included with .so need to be preprocessed. Normally, soelim should be invoked with the -s option of groff. To embed `' in the file name, write `\' or `e'. To embed a space, write ` '. Any other escape sequence in file makes soelim ignore the whole line. Note that there must be no whitespace between the leading dot and the two characters `s' and `o'. Otherwise, only groff interprets the .so request (and soelim ignores it). OPTIONS
-C Recognize .so even when followed by a character other than space or newline. -Idir This option may be used to add a directory to the search path for files (both those on the command line and those named in .so requests). The search path is initialized with the current directory. This option may be specified more than once; the directories are then searched in the order specified (but before the current directory). If you want to make the current directory be read before other directories, add -I. at the appropriate place. No directory search is performed for files with an absolute file name. -r Do not add .lf requests (for general use, with non-groff files). -t Don't emit .lf requests but TeX comment lines (starting with `%') giving the current file and line number. -v Print the version number. USAGE
The normal processing sequence of groff is this: input sourced file file | | v v preprocessor -> troff -> postprocessor | v output file That is, files sourced with .so are normally read only by troff (the actual formatter). soelim is not required for troff to source files. If a file to be sourced should also be preprocessed, it must already be read before the input file passes through the preprocessor. This is handled by soelim: input file | v soelim -> preprocessor -> troff -> postprocessor ^ | | v sourced output file file SEE ALSO
groff(1) Groff Version 1.21 31 December 2010 SOELIM(1)
All times are GMT -4. The time now is 01:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy