Computing.Net > Forums > Linux > Unix script to join lines

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix script to join lines

Reply to Message Icon

Name: shivaarun
Date: April 11, 2007 at 10:16:03 Pacific
OS: Linux
CPU/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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 12, 2007 at 13:49:51 Pacific
Reply:

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


0

Response Number 2
Name: shivaarun
Date: April 12, 2007 at 14:21:13 Pacific
Reply:

Thanks for update. But the script does not give output in required format.. Pls Help..

Regards
Arun


0

Response Number 3
Name: nails
Date: April 12, 2007 at 14:32:40 Pacific
Reply:

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:


0

Response Number 4
Name: shivaarun
Date: April 12, 2007 at 14:44:52 Pacific
Reply:

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


0

Response Number 5
Name: shivaarun
Date: April 12, 2007 at 14:51:10 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: nails
Date: April 12, 2007 at 16:21:15 Pacific
Reply:

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


0

Response Number 7
Name: shivaarun
Date: April 13, 2007 at 06:34:45 Pacific
Reply:

Thank you Very Much. Your script worked .

Regards
Arun


0

Response Number 8
Name: shivaarun
Date: April 16, 2007 at 14:22:50 Pacific
Reply:

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


0

Response Number 9
Name: ernie
Date: April 16, 2007 at 15:15:28 Pacific
Reply:

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


0

Response Number 10
Name: shivaarun
Date: April 16, 2007 at 15:19:00 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: Unix script to join lines

Win 2000 unable to join Samba PDC www.computing.net/answers/linux/win-2000-unable-to-join-samba-pdc/17101.html

help writing script to read files n www.computing.net/answers/linux/help-writing-script-to-read-files-n/27648.html

UNIX Script help www.computing.net/answers/linux/unix-script-help/7820.html