Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Help with Concatenation

Status
Not open for further replies.

rthreads

Programmer
Apr 22, 2016
4
GB
Hi All,

I need help with Perl Concatenation based on a condition.

In the example file below, the condition is as long as the ID is the same the values of fields need to be concatenated. The expected output will show what I mean.

My Input file is attached.

I want output in the below format

UC "TS_206" /DATA=(ACTION_ORDER=["1","2","3","4"],ACTION_DATE=["26/07/15","26/07/15","26/07/15","26/07/15"],ACTION_COMMENT=["Created","InProgress","Tested","Completed"])
UC "TS_450" /DATA=(ACTION_ORDER=["1","2","3"],ACTION_DATE=["02/08/15","03/08/15","03/08/15"],ACTION_COMMENT=["Created","InProgress","Rejected"])

Please help with the looping syntax.

Many thanks in advance.
 
 http://files.engineering.com/getfile.aspx?folder=dd792c58-6a0c-43d4-b790-66965be5fdd7&file=New_Text_Document_(3).txt
Hi

Code:
[blue]master #[/blue] perl -nae 'sub p{print"UC \"$l\" /DATA=(",join(",",map{"$h[$_]=[".join(",",map{"\"$_\""}@{$s[$_]})."]"}0..2),")\n"if@s;@s=([],[],[])}if($.==1){@h=map{uc s/\B(?=[A-Z])/_/gr}@F[1..3];next}p if@F[0]ne$l;$l=shift@F;push@{$s[$_]},$F[$_]for 0..2;END{p}' New_Text_Document_\(3\).txt
UC "TS_206" /DATA=(ACTION_ORDER=["1","2","3","4"],ACTION_DATE=["26/07/15","26/07/15","26/07/15","26/07/15"],ACTION_COMMENT=["Created","InProgress","Tested","Completed"])
UC "TS_450" /DATA=(ACTION_ORDER=["1","2","3"],ACTION_DATE=["02/08/15","03/08/15","03/08/15"],ACTION_COMMENT=["Created","InProgress","Rejected"])


Feherke.
feherke.ga
 
Hi Feherke.

Many thanks to reply back to my post and to help me with the code.

I get the following error on executing the command

Can't find string terminator "'" anywhere before EOF at -e line 1. (error screen shot attached)

I see [highlight #F57900]END{p}'[/highlight] at the end not sure where the error is generated.

Please help.
 
 http://files.engineering.com/getfile.aspx?folder=bbf8989b-41c4-4b1f-a630-ec253db7c504&file=New_Microsoft_Word_Document.docx
Hi

Your problem is that CMD.exe does not support single quote ( ' ) as string delimiter.

Better put all the part enclosed in single quotes in a file
Code:
[b]sub[/b] p {
    [b]print[/b] [i]"UC \"$l\" /DATA=("[/i], [b]join[/b]([i]","[/i], [b]map[/b]{ [i]"$h[$_]=["[/i] . [b]join[/b]([i]","[/i], [b]map[/b] { [i]"\"$_\""[/i] } @{$s[$_]}) . [i]"]"[/i] } 0..2), [i]")\n"[/i] [b]if[/b] @s;
    @s = ([], [], [])
}
[b]if[/b] ($. == 1) {
    @h = [b]map[/b] { [b]uc[/b] [b]s[/b]/\B(?=[A-Z])/_/gr } @F[1..3];
    [b]next[/b]
}
p [b]if[/b] @F[0] ne $l;
$l = [b]shift[/b] @F;
[b]push[/b] @{$s[$_]}, $F[$_] [b]for[/b] 0..2;
END {
    p
}
...then run it as :
Code:
perl -na your_script_file.pl New_Text_Document_\(3\).txt


Feherke.
feherke.ga
 
Hello Feherke,

As you suggested, when I put the code to a file and executed it works fine.

I would appreciate if you could help me with the problem I have.

Sometimes my input file could contain more than one word for example Heading [highlight #73D216]ActionComment[/highlight] could contain "[highlight #EF2929]Created by Zara"[/highlight].

The current code picks only [highlight #EF2929]Created[/highlight] and ignores (or puts into next column header if exists) the rest of the value ([highlight #EF2929]by Zara[/highlight]).

How do I tell the code to not break on spaces but on tab/tabs as my column values are separated by tabs.

I am attaching the input file with changes to column values so you could try and help me.

Many Thanks again.
 
 http://files.engineering.com/getfile.aspx?folder=d5a21dd1-9f43-472a-a765-937029d60413&file=New_Text_Document_(3)_.txt
Hi

That should be simple to fix :
Code:
perl -na[highlight]lF"\t"[/highlight] your_script_file.pl New_Text_Document_\(3\).txt
And to compensate the [tt]-l[/tt] added in the command line, remove the "\n" from your_script_file.pl.

As I am on Linux and your input file contains Windows line separators, the above alone not works for me, for which I had to add another line in the code :
Code:
[gray]# ...[/gray]
[highlight]$F[3] =~ [b]s[/b]/\s+$//;[/highlight]
[b]if[/b] ($. == 1) {
    [gray]# ...[/gray]
}
[gray]# ...[/gray]
I suppose you will not need this, but adding it will not harm anyway.


Feherke.
feherke.ga
 
Thank you very much Feherke. Your code works fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top