Data Manipulation
up vote
-2
down vote
favorite
I have a data as mentioned below.
host_name Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Required Output is below.
host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager
shell-script awk scripting perl gawk
add a comment |
up vote
-2
down vote
favorite
I have a data as mentioned below.
host_name Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Required Output is below.
host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager
shell-script awk scripting perl gawk
1
This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek♦
Jun 8 '16 at 14:39
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have a data as mentioned below.
host_name Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Required Output is below.
host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager
shell-script awk scripting perl gawk
I have a data as mentioned below.
host_name Server1.domain.com
contacts DL - Desktop
contact_groups ravi, raj, rahim
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Required Output is below.
host_name Server1.domain.com, contacts ravi,raj,rahim, Contact_group DL-Desktop
host_name Server2.domain.com contact_groups DL - Server
host_name Server3.domain.com
host_name Server4.domain.com contacts services,helpdesk,manager
shell-script awk scripting perl gawk
shell-script awk scripting perl gawk
edited Nov 25 at 23:40
Rui F Ribeiro
38.3k1477127
38.3k1477127
asked Jun 8 '16 at 6:35
Ravi Ankam
86
86
1
This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek♦
Jun 8 '16 at 14:39
add a comment |
1
This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek♦
Jun 8 '16 at 14:39
1
1
This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek♦
Jun 8 '16 at 14:39
This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek♦
Jun 8 '16 at 14:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1
export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'
Content of file1
host_name Server1.domain.com
contacts ravi, raj, rahim
contact_groups DL - Desktop
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Output of the above command
host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
add a comment |
up vote
1
down vote
$ sed -e '2,$ s/^host_name/n&/' ravi.txt |
perl -n -e 'if (! m/^$/) {
chomp;
$line .= $_ . ", "
};
if (m/^$/ || eof) {
$line =~ s/ +/ /g; # merge multiple spaces into one space
$line =~ s/, $//; # strip the trailing comma
print $line,"n" ;
$line=""
}'
host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
First use sed
to convert the input to paragraphs (separated by a newline). Then perl
to join the lines in each paragraph together and print them out.
This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1
export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'
Content of file1
host_name Server1.domain.com
contacts ravi, raj, rahim
contact_groups DL - Desktop
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Output of the above command
host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
add a comment |
up vote
1
down vote
accepted
I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1
export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'
Content of file1
host_name Server1.domain.com
contacts ravi, raj, rahim
contact_groups DL - Desktop
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Output of the above command
host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1
export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'
Content of file1
host_name Server1.domain.com
contacts ravi, raj, rahim
contact_groups DL - Desktop
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Output of the above command
host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1
export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}n"; fi; export output="${output}, $line"; done < file1 && echo -e $output | sed 's/^, ?//' | sed '/^$/d'
Content of file1
host_name Server1.domain.com
contacts ravi, raj, rahim
contact_groups DL - Desktop
host_name Server2.domain.com
contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com
contacts Services,helpdesk,manager
Output of the above command
host_name Server1.domain.com, contacts ravi, raj, rahim, contact_groups DL - Desktop
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
edited Jun 8 '16 at 7:12
answered Jun 8 '16 at 7:07
Hesham Ahmed
42428
42428
add a comment |
add a comment |
up vote
1
down vote
$ sed -e '2,$ s/^host_name/n&/' ravi.txt |
perl -n -e 'if (! m/^$/) {
chomp;
$line .= $_ . ", "
};
if (m/^$/ || eof) {
$line =~ s/ +/ /g; # merge multiple spaces into one space
$line =~ s/, $//; # strip the trailing comma
print $line,"n" ;
$line=""
}'
host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
First use sed
to convert the input to paragraphs (separated by a newline). Then perl
to join the lines in each paragraph together and print them out.
This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.
add a comment |
up vote
1
down vote
$ sed -e '2,$ s/^host_name/n&/' ravi.txt |
perl -n -e 'if (! m/^$/) {
chomp;
$line .= $_ . ", "
};
if (m/^$/ || eof) {
$line =~ s/ +/ /g; # merge multiple spaces into one space
$line =~ s/, $//; # strip the trailing comma
print $line,"n" ;
$line=""
}'
host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
First use sed
to convert the input to paragraphs (separated by a newline). Then perl
to join the lines in each paragraph together and print them out.
This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.
add a comment |
up vote
1
down vote
up vote
1
down vote
$ sed -e '2,$ s/^host_name/n&/' ravi.txt |
perl -n -e 'if (! m/^$/) {
chomp;
$line .= $_ . ", "
};
if (m/^$/ || eof) {
$line =~ s/ +/ /g; # merge multiple spaces into one space
$line =~ s/, $//; # strip the trailing comma
print $line,"n" ;
$line=""
}'
host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
First use sed
to convert the input to paragraphs (separated by a newline). Then perl
to join the lines in each paragraph together and print them out.
This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.
$ sed -e '2,$ s/^host_name/n&/' ravi.txt |
perl -n -e 'if (! m/^$/) {
chomp;
$line .= $_ . ", "
};
if (m/^$/ || eof) {
$line =~ s/ +/ /g; # merge multiple spaces into one space
$line =~ s/, $//; # strip the trailing comma
print $line,"n" ;
$line=""
}'
host_name Server1.domain.com, contacts DL - Desktop, contact_groups ravi, raj, rahim
host_name Server2.domain.com, contact_groups DL-Server
host_name Server3.domain.com
host_name Server4.domain.com, contacts Services,helpdesk,manager
First use sed
to convert the input to paragraphs (separated by a newline). Then perl
to join the lines in each paragraph together and print them out.
This could be done entirely in perl, but I was lazy and decided it was easier to just convert to paragraphs before piping into a simple perl scipt.
answered Jun 8 '16 at 7:38
cas
38.4k44999
38.4k44999
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f288324%2fdata-manipulation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This isn't the place to spam links about your website; stop posting them in comments
– Michael Mrozek♦
Jun 8 '16 at 14:39