awk columns to rows break for new lineConvert two rows to two columnsAdding entries to the end of a row via awkAWK sum every two rowsHow to add “wrappers” around methods in source code based on a pattern, using sed, awk, grep and friendsmerge duplicate rows in columnsConvert rows to columnsTransposing rows into columns in absence of few rows using AWKSubset rows with awkRearranging columns using awkhow to concatenate columns with multiple rows using awk
Rationale to prefer local variables over instance variables?
Are small insurances worth it?
Limpar string com Regex
What can I do if someone tampers with my SSH public key?
Precision notation for voltmeters
Why do we call complex numbers “numbers” but we don’t consider 2-vectors numbers?
Was it really inappropriate to write a pull request for the company I interviewed with?
An Undercover Army
How can I have x-axis ticks that show ticks scaled in powers of ten?
Issue with units for a rocket nozzle throat area problem
3.5% Interest Student Loan or use all of my savings on Tuition?
Why do phishing e-mails use faked e-mail addresses instead of the real one?
What does *dead* mean in *What do you mean, dead?*?
If nine coins are tossed, what is the probability that the number of heads is even?
Short SF story. Females use stingers to implant eggs in yearfathers
How to educate team mate to take screenshots for bugs with out unwanted stuff
Should we avoid writing fiction about historical events without extensive research?
Why does a car's steering wheel get lighter with increasing speed
A running toilet that stops itself
Why is there an extra space when I type "ls" on the Desktop?
Why aren't there more Gauls like Obelix?
Can I challenge the interviewer to give me a proper technical feedback?
Giving a career talk in my old university, how prominently should I tell students my salary?
Cycles on the torus
awk columns to rows break for new line
Convert two rows to two columnsAdding entries to the end of a row via awkAWK sum every two rowsHow to add “wrappers” around methods in source code based on a pattern, using sed, awk, grep and friendsmerge duplicate rows in columnsConvert rows to columnsTransposing rows into columns in absence of few rows using AWKSubset rows with awkRearranging columns using awkhow to concatenate columns with multiple rows using 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
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
1085
1085
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 /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ ;
' 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 /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ ;
' 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 /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ ;
' 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 /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ ;
' 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 /./;
print join(" - ", splice(@A,0,@A)),"n" if /^$/ ;
' 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 1 hour ago
DeepuDeepu
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