How to seperate text and adjust format like this?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to seperate text and adjust format like this?
# 1  
Old 05-28-2017
How to seperate text and adjust format like this?

in example.txt file is below
Code:
ADD PDU:SRN=0,PDUID=LOCAL,NAME="PDU_0",PSV=LOW,MOG="PUBLIC",REFERABLE=YES;
ADD PDU:SRN=2,PDUID=LOCAL,NAME="PDU_1",PSV=LOW,MOG="PUBLIC",REFERABLE=YES;
ADD MODULE:MID=84,MT=DSU,SRN1=0,SN1=4,MNAME="DSU84";
ADD MODULE:MID=85,MT=DSU,SRN1=0,SN1=4,MNAME="DSU85";

How to adjust format like below ? please help to give me the idea. Thanks

Code:
ADD PDU:SRN,PDUID,NAME,PSV,MOG,REFERABLE
0,LOCAL,"PDU_0",LOW,"PUBLIC",YES
2,LOCAL,"PDU_1",LOW,"PUBLIC",YES

ADD MODULE:MID,MT,SRN1,SN1,MNAME
84,DSU,0,4,"DSU84"
85,DSU,0,4,"DSU85"

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules).

Last edited by Don Cragun; 05-28-2017 at 01:48 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 05-28-2017
Quote:
Originally Posted by swensens
in example.txt file is below
Code:
ADD PDU:SRN=0,PDUID=LOCAL,NAME="PDU_0",PSV=LOW,MOG="PUBLIC",REFERABLE=YES;
ADD PDU:SRN=2,PDUID=LOCAL,NAME="PDU_1",PSV=LOW,MOG="PUBLIC",REFERABLE=YES;
ADD MODULE:MID=84,MT=DSU,SRN1=0,SN1=4,MNAME="DSU84";
ADD MODULE:MID=85,MT=DSU,SRN1=0,SN1=4,MNAME="DSU85";

How to adjust format like below ? please help to give me the idea. Thanks

Code:
ADD PDU:SRN,PDUID,NAME,PSV,MOG,REFERABLE
0,LOCAL,"PDU_0",LOW,"PUBLIC",YES
2,LOCAL,"PDU_1",LOW,"PUBLIC",YES

ADD MODULE:MID,MT,SRN1,SN1,MNAME
84,DSU,0,4,"DSU84"
85,DSU,0,4,"DSU85"

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules).
What operating system are you using?

What shell are you using?

What have you tried to solve this on your own?

Is this a homework assignment? If not, what is the reason for doing this?

Do all "records" for a given ADD XXX have the same comma separated fields following them in the same order (as in your examples), or can some fields be missing or in a different order on some records?

Are all records for a given ADD XXX pair adjacent (as in your examples), or can record types be separated? If they can be separated, is the output supposed to be grouped by record type or is the order of output records supposed to be the same as the order of the input records?
# 3  
Old 05-28-2017
Quote:
Originally Posted by swensens
in example.txt file is below
Code:
ADD PDU:SRN=0,PDUID=LOCAL,NAME="PDU_0",PSV=LOW,MOG="PUBLIC",REFERABLE=YES;
ADD PDU:SRN=2,PDUID=LOCAL,NAME="PDU_1",PSV=LOW,MOG="PUBLIC",REFERABLE=YES;
ADD MODULE:MID=84,MT=DSU,SRN1=0,SN1=4,MNAME="DSU84";
ADD MODULE:MID=85,MT=DSU,SRN1=0,SN1=4,MNAME="DSU85";

How to adjust format like below ? please help to give me the idea. Thanks

Code:
ADD PDU:SRN,PDUID,NAME,PSV,MOG,REFERABLE
0,LOCAL,"PDU_0",LOW,"PUBLIC",YES
2,LOCAL,"PDU_1",LOW,"PUBLIC",YES

ADD MODULE:MID,MT,SRN1,SN1,MNAME
84,DSU,0,4,"DSU84"
85,DSU,0,4,"DSU85"

Let's give it a go:
Save as process.pl.
Run as perl process.pl example.txt

Code:
my %labels;

while(<>){
 my ($label, $csvs) = split /[:;]/;
 my @pairs = split ',', $csvs;
 my @titles, my @values;
 for(@pairs){
   my ($title, $value) = split '=';
   push @titles, $title;
   push @values, $value;
 }
 $labels{$label}[0] = join ',', @titles;
 push @{$labels{$label}}, join ',', @values;

}
for(keys %labels){
  print "$_:";
  for(@{$labels{$_}}){
    print"$_\n";
  }
  print "\n";
}

Output:
Code:
ADD PDU:SRN,PDUID,NAME,PSV,MOG,REFERABLE
0,LOCAL,"PDU_0",LOW,"PUBLIC",YES
2,LOCAL,"PDU_1",LOW,"PUBLIC",YES

ADD MODULE:MID,MT,SRN1,SN1,MNAME
84,DSU,0,4,"DSU84"
85,DSU,0,4,"DSU85"

This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to adjust text and count based on value in field

The below awk executes as is and produces the current output. It isvery close but what Ican not seem to do is add the -exon..., the ... portion comes from $1 and the _exon is static and will never change. If there is + sign in $4 then the ... is in acending order or sequential. If there is a - in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Programming

Date format change from mm/dd/yyyy to yyyymmdd in comma seperate line in perl

Hi All, I have line ,A,FDRM0002,12/21/2017,,0.961751583,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, it contains date in mm/dd/yyyy format i want to change this to yyyymmdd format using perl. Use code tags, thanks. (8 Replies)
Discussion started by: vishal0746
8 Replies

3. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

4. Solaris

can't adjust time : not owner

Hi experts, I m new to solaris. I am getting following errror in /var/adm/messages file on one of the solaris 10 zones - xntpd: Can't set time of day: Not owner xntpd daemon is online. Any help on this ? (3 Replies)
Discussion started by: sunadmin
3 Replies

5. Shell Programming and Scripting

format text

input file test.csv has following structure. SCHEM1.TAB1;COL1;DATATYPE;NOTNULL-- SCHEMA1.TAB1;COL2;DATATYPE;NOTNULL;WITH DEFAULT;-- .... SCHEMA1.TABn;COL1;DATATYPE;NOTNULL;WITH DEFAULT; i started using awk but text is not tab seperated.its just ; seperated. how to print output... (4 Replies)
Discussion started by: rocking77
4 Replies

6. Shell Programming and Scripting

How to adjust spacing

Is there a way to adjust spacing of a line using k shell? e.g I have a file below $ cat file1 AAA BBB CCC A B C AAAA BB CC I want each word to be adjusted with spaces to have 10 character length like below: AAA BBB CCC A B C AAAA BB CC Any... (4 Replies)
Discussion started by: stevefox
4 Replies

7. UNIX for Advanced & Expert Users

Adjust format file

Hi all... i have a question, and i don`t know what to do ... i have a flat file what is separated by ";" and i need format it... here is an example: this is what i have: AAA ; BBB ; 1 ; 1.1 ; 1.2 ; 1.3 ; 2 ; 2.1 ; 2.2 ; 2.3 ; 3 ; 3.1 ; 3.2 ; 3.3 ; ....... ......... there are a lot of... (4 Replies)
Discussion started by: DebianJ
4 Replies

8. UNIX for Advanced & Expert Users

adjust files

hi... i have a big problem, and i don't know how to solve it. here is the thing: i have 12 files, which are flat files, with several records(lines), and each record has 3 fields, wich are separated by pipe (|), something like this: file 1: 33|12|2000 33|22|3000 66|24|3000 99|48|4000... (7 Replies)
Discussion started by: DebianJ
7 Replies

9. Shell Programming and Scripting

Adjust the db script

Enclosing a script that is used everyday for database shutdown. In here you will find some code that checks oracle version. That part is very unnecessary since we use only 8.1.7 and will never go back.. Can anyone help me by modifying the code, to never use that part and readjust so script does... (1 Reply)
Discussion started by: ST2000
1 Replies

10. Filesystems, Disks and Memory

Cannot adjust division

I have a doubt with an error message, and i want to be sure if this is a normal situation or not. Situation: I was formating and installing a SCSI 36Gb HD with UNIX SCO 5.05, the problem happens when is making the division and filesystem on disk 1, and the message error is "Exit value 139... (1 Reply)
Discussion started by: jav_v
1 Replies
Login or Register to Ask a Question