|
|
|
Unix script to join lines
|
Original Message
|
Name: shivaarun
Date: April 11, 2007 at 10:16:03 Pacific
Subject: Unix script to join lines OS: LinuxCPU/Ram: Linux |
Comment: Say I have a file which is of following format Group: Asd Bsd Csd Owner: Date: Group: Aed Asd Csd Owner: Date: I want Group: Asd,Bsd,Csd Owner: Date: Group: Aed,Asd,Csd Owner: Date: How can I do this in Unix. Any suggestion ???? Regards Arun
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: April 12, 2007 at 13:49:51 Pacific
Subject: Unix script to join lines |
Reply: (edit)You can set up a really ugly break point report in a shell script. When the line equals Group: read each line appending to a variable until the word owner appears. Print the variable and continue the loop: #!/bin/bash pr_line="" g_flag=0 fr=1 while read line do if [[ ${line} = "Group:" ]] then g_flag=1 pr_line=${line} continue fi # print when line equals Owner: if [[ ${line} = "Owner:" ]] then printf "%s\n" ${pr_line} g_flag=0 fr=1 fi if [[ g_flag -eq 1 ]] then if [[ fr -eq 1 ]] then pr_line=${pr_line}${line} fr=0 else pr_line=${pr_line},${line} fi continue fi printf "%s\n" ${line} done < data.file
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: shivaarun
Date: April 12, 2007 at 14:21:13 Pacific
Subject: Unix script to join lines |
Reply: (edit)Thanks for update. But the script does not give output in required format.. Pls Help.. Regards Arun
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: nails
Date: April 12, 2007 at 14:32:40 Pacific
Subject: Unix script to join lines |
Reply: (edit)Below, is the output of the bash script. It looks exactly like what you asked for. If isn't, you need to provide more information about the problem: Group:Asd,Bsd,Csd Owner: Date:
Group:Aed,Asd,Csd Owner: Date:
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: shivaarun
Date: April 12, 2007 at 14:44:52 Pacific
Subject: Unix script to join lines |
Reply: (edit)Hi Actually My data file is eactly this : Data file f2.dat : Data for GROUP 'bin' Userlist: bin daemon root scpar test Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:45 Updated by: root Data for GROUP 'console' Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:35 Updated by: root Data for GROUP 'daemon' Userlist: bin daemon root Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:45 Updated by: root So i want the Userlist as Userlist:bin,daemon,root,scpar,test For 2 nd Group (console) no Userlist so dont do anything Userlist:bin,daemon,root. Pls advice...... Regards Arun
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: shivaarun
Date: April 12, 2007 at 14:51:10 Pacific
Subject: Unix script to join lines |
Reply: (edit)Hi I also need all the other data. But i want to Manipulate only UserList so that Userlist is such that UserList: bin,daemon,root instead of UserList: bin daemon root . All other fields i need no changes.... Regards Arun
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: nails
Date: April 12, 2007 at 16:21:15 Pacific
Subject: Unix script to join lines |
Reply: (edit)You can't expect a break point report to work when you change the data break rules: #!/bin/bash pr_line="" g_flag=0 fr=1 while read line do # save when beginning line equals Userlist: case "${line}" in Userlist:*) g_flag=1 pr_line="${line}" continue ;; # print when beginning line equals Owner: Owner:*) if [[ -n ${pr_line} ]] then printf "%s\n" "${pr_line}" fi pr_line="" g_flag=0 fr=1 ;; esac if [[ g_flag -eq 1 ]] then if [[ fr -eq 1 ]] then pr_line=${pr_line}${line} fr=0 else pr_line=${pr_line},${line} fi continue fi printf "%s\n" "${line}" done < f2.dat
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: shivaarun
Date: April 16, 2007 at 14:22:50 Pacific
Subject: Unix script to join lines |
Reply: (edit)Hi all I have a Data in following format which i want to convert to a Flat File. The Input data is user.dat : Data: bin ---------------- Userlist: bin,daemon,root,scpar,test Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:45 Updated by: root Data: console ---------------- Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:35 Updated by: root Data: daemon ---------------- Userlist: bin,daemon,root Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:45 Updated by: root Data: db2asgrp ---------------- Owner: nobody (USER ) Create time: 13-Feb-2007 10:35 Update time: 13-Feb-2007 10:35 Updated by: root The Output file should be : Data~Userlist~Owner~Create Time~Update time~ updated by that is for Data:bin output should be following. I want to do this for each Data : bin~bin,daemon,root,scpar,test~nobody (USER )~13-Feb-2007 10:35~13-Feb-2007 10:45~root If any field is empty then it must print delimiter ~~ . That is if for data: Console There is no UserList so output must be console~~nobody (USER)~13-Feb-2007 10:35~13-Feb-2007 10:35~root Pls advice ... Regards Arun
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: ernie
Date: April 16, 2007 at 15:15:28 Pacific
Subject: Unix script to join lines |
Reply: (edit)Is this home work? It looks like it to me. If this is home work, you should be doing it yourself, and perhaps asking questions to help you understand what you are trying to do. If you do not learn to do the work yourself, how will you do it when you are out in the real world? If this is not home work, please do not be offended, after all, you are asking two similar question now ... Ernie Registered Linux User 247790 ICQ 41060744
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: shivaarun
Date: April 16, 2007 at 15:19:00 Pacific
Subject: Unix script to join lines |
Reply: (edit)Hi Actually from some Users i dont have UsersList. So when i search for each user if there is no UserList it must just print delimiter if u can tell me how i can approach it . It will be of great help for me . Regards Arun
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|