awk columns to rows break for new line
In my stats.txt
page I have the following:
Get-VMHardDiskDrive | Select-Object Path
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : .07
Prov Space : 4.00
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : 258.04
Prov Space : 906.34
I run the following AWK
command to give me this:
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VHD
Dynamic
.07
4.00
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VHD
Dynamic
258.04
906.34
The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.
awk -F " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt
text-processing awk
add a comment |
In my stats.txt
page I have the following:
Get-VMHardDiskDrive | Select-Object Path
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : .07
Prov Space : 4.00
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : 258.04
Prov Space : 906.34
I run the following AWK
command to give me this:
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VHD
Dynamic
.07
4.00
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VHD
Dynamic
258.04
906.34
The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.
awk -F " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt
text-processing awk
add a comment |
In my stats.txt
page I have the following:
Get-VMHardDiskDrive | Select-Object Path
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : .07
Prov Space : 4.00
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : 258.04
Prov Space : 906.34
I run the following AWK
command to give me this:
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VHD
Dynamic
.07
4.00
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VHD
Dynamic
258.04
906.34
The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.
awk -F " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt
text-processing awk
In my stats.txt
page I have the following:
Get-VMHardDiskDrive | Select-Object Path
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : .07
Prov Space : 4.00
ComputerName : ACD-VMH04
Path : D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VhdFormat : VHD
VhdType : Dynamic
Used Space : 258.04
Prov Space : 906.34
I run the following AWK
command to give me this:
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd
VHD
Dynamic
.07
4.00
ACD-VMH04
D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd
VHD
Dynamic
258.04
906.34
The file has about 6 items like that however other files have more so I am wondering how to get the output to look like this:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
This will somewhat do the job however I do not know how to tell it stop and make a new line when it hits a blank line.
awk -F " : " '{ ORS = " " } /Select-Object Path/ { output = 1; start = NR + 2 }; output && NR >= start { print $2, "-" }' stats.txt
text-processing awk
text-processing awk
edited Mar 30 '17 at 21:13
Stephen Rauch
3,344101428
3,344101428
asked Mar 30 '17 at 20:02
Anthony FornitoAnthony Fornito
1105
1105
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.
Code:
#!/bin/awk -f
BEGIN {FS = " : "; OFS = " - "}
NF > 0 {
gsub(/[ ]+$/, "", $1)
a[$1] = $2
}
NF == 0 {dump()}
END {dump()}
function dump() {
print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
}
Results:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic
add a comment |
perl -lne '
/Select-Object Path/ and !$a++ and <>,next;
/^ComputerNames+:/ && $a .. /^$/ || eof and do{
push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
};
' stats.txt
Output
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
add a comment |
We can use " : " as delimter and print 2nd field of every line in awk
awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f354922%2fawk-columns-to-rows-break-for-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.
Code:
#!/bin/awk -f
BEGIN {FS = " : "; OFS = " - "}
NF > 0 {
gsub(/[ ]+$/, "", $1)
a[$1] = $2
}
NF == 0 {dump()}
END {dump()}
function dump() {
print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
}
Results:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic
add a comment |
I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.
Code:
#!/bin/awk -f
BEGIN {FS = " : "; OFS = " - "}
NF > 0 {
gsub(/[ ]+$/, "", $1)
a[$1] = $2
}
NF == 0 {dump()}
END {dump()}
function dump() {
print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
}
Results:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic
add a comment |
I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.
Code:
#!/bin/awk -f
BEGIN {FS = " : "; OFS = " - "}
NF > 0 {
gsub(/[ ]+$/, "", $1)
a[$1] = $2
}
NF == 0 {dump()}
END {dump()}
function dump() {
print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
}
Results:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic
I don't have a one liner for you, but the key concept here is to store the field until you have collected them all, then print them on one line. Also a blank line will have the number of fields (NF) equal to zero.
Code:
#!/bin/awk -f
BEGIN {FS = " : "; OFS = " - "}
NF > 0 {
gsub(/[ ]+$/, "", $1)
a[$1] = $2
}
NF == 0 {dump()}
END {dump()}
function dump() {
print a["ComputerName"], a["Path"], a["VhdFormat"], a["VhdType"]
}
Results:
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic
answered Mar 30 '17 at 20:43
Stephen RauchStephen Rauch
3,344101428
3,344101428
add a comment |
add a comment |
perl -lne '
/Select-Object Path/ and !$a++ and <>,next;
/^ComputerNames+:/ && $a .. /^$/ || eof and do{
push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
};
' stats.txt
Output
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
add a comment |
perl -lne '
/Select-Object Path/ and !$a++ and <>,next;
/^ComputerNames+:/ && $a .. /^$/ || eof and do{
push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
};
' stats.txt
Output
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
add a comment |
perl -lne '
/Select-Object Path/ and !$a++ and <>,next;
/^ComputerNames+:/ && $a .. /^$/ || eof and do{
push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
};
' stats.txt
Output
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
perl -lne '
/Select-Object Path/ and !$a++ and <>,next;
/^ComputerNames+:/ && $a .. /^$/ || eof and do{
push @A, (/s+:s+K(.*)/)[0] if /^ComputerNames+:/ || /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ || eof;
};
' stats.txt
Output
ACD-VMH04 - D:HYPER-VVMDEV01Virtual Hard Disksf9314d7d-5d2d-11e0-8feb-806e6f6e6963_2012-06-29T153327.vhd - VHD - Dynamic - .07 - 4.00
ACD-VMH05 - D:HYPER-VVMDEV01Virtual Hard DisksC_2012-06-29T153327.vhd - VHD - Dynamic - 258.04 - 906.34
edited Mar 30 '17 at 21:58
answered Mar 30 '17 at 21:36
user218374
add a comment |
add a comment |
We can use " : " as delimter and print 2nd field of every line in awk
awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt
New contributor
add a comment |
We can use " : " as delimter and print 2nd field of every line in awk
awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt
New contributor
add a comment |
We can use " : " as delimter and print 2nd field of every line in awk
awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt
New contributor
We can use " : " as delimter and print 2nd field of every line in awk
awk -v FS=" : " -v ORS="" '{{print $2,"- "}; if ( $0 ~ /^$/) {print "n"}}' stats.txt
New contributor
New contributor
answered 22 mins ago
Deepika Reddy BilluriDeepika Reddy Billuri
12
12
New contributor
New contributor
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.
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%2f354922%2fawk-columns-to-rows-break-for-new-line%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