Conversion of rows to columns using awk based om column value

HI,
My Input file data is

dn:adcfgeneral
id:13343
Name:xxxxxx
Password:iutyerwuitywue wpuwt
tuiytruityrutyrwtyrwp
dn:cdferwjyyyy
id:3875
Name:yyyy
Password :hgfdsjkfhdsfkdlshf
dshfkldshfdklsfh
interset:uiuiufj

My output should be

dn:adcfgeneral|id:13343|Name:xxxxxx|Password:iutyerwuitywue wpuwttuiytruityrutyrwtyrwp
dn:cdferwjyyyy|id:3875|Name:yyyy|Password :hgfdsjkfhdsfkdlshfdshfkldshfdklsfh|interset:uiuiufj

The data between dn should converted to rows

I'm not sure why you have a closing COLOR tag in your input file nor why it shouldn't be copied to your output file, but ignoring that, the following seems to do what you want:

awk '
/^dn:/ {printf("%s%s", (NR > 1) ? "\n" : "", $0)
	next
}
{	printf("%s%s", (sub(/:/, "&")) ? "|" : "", $0)
}
END {	print ""
}' file

which, if file contains your sample input, produces the output:

dn:adcfgeneral|id:13343|Name:xxxxxx|Password:iutyerwuitywue wpuwttuiytruityrutyrwtyrwp
dn:cdferwjyyyy|id:3875|Name:yyyy|Password :hgfdsjkfhdsfkdlshfdshfkldshfdklsfh|interset:uiuiufj

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

1 Like

Hello dineshaila,

Welcome to forums, please use code tags for codes/commands/Inputs which you are using into your posts as per forum rules. Coming onto your question, couldn't understand by which logic string has been removed from output shown by you. If this is typo then following may help you in same.

awk '($0 ~ /^dn:/ && A){print A;A="";} {A=A?A OFS $0:$0} END{print A}' OFS="|"   Input_file

Output will be as follows.

dn:adcfgeneral|id:13343|Name:xxxxxx|Password:iutyerwuitywue wpuwt|tuiytruityrutyrwtyrwp
dn:cdferwjyyyy|id:3875|Name:yyyy|Password :hgfdsjkfhdsfkdlshf|dshfkldshfdklsfh|interset:uiuiufj

If above output doesn't meet your requirement or you have any additional information to your requirement, please do show us sample Input_file with expected sample output too. Hope this helps you.

Thanks,
R. Singh

1 Like

Hi Don/Ravinder,

Thanks for the reply.
Could you please expalin how the code works

Are these added comments enough to explain what my script does, or do you still not understand how something works?

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

Hello dineshaila,

Following may help you in same.

awk '($0 ~ /^dn:/ && A){        ######## Checking here if any line is getting started with string "dn:" AND variable A is NOT NULL values in it. Where variable A is the one where I am storing all the lines values in it.
print A;                        ######## print the value of variable A then.
A="";}                          ######## Nullify the variable A's value now.
{A=A?A OFS $0:$0}               ######## Here each line I am appending the value of A with the current line's value in it.
END{print A}'                   ######## END is a awk's default section where we will perform the tasks which want to perform at last, so I am printing variable A's value here too.
OFS="|" Input_file              ######## Setting up output field separator's value to | here as per your requirement. Mentioning the Input_file name too.

Thanks,
R. Singh

Thanks for the clear response