in Education by
In trying to process the output of "mccli activity show" (for example), the output might be like this: ID Status Error Code Start Time Elapsed End Time Type Progress Bytes New Bytes ---------------- ------------------------ ---------- -------------------- ----------- -------------------- ---------------- ----------------- --------- 9133910640004809 Completed w/Exception(s) 10020 2012-06-07 18:00 EDT 00h:53m:46s 2012-06-07 18:53 EDT Scheduled Backup 215 0.2% 9133914600006909 Completed 0 2012-06-08 05:00 EDT 00h:00m:04s 2012-06-08 05:00 EDT Scheduled Backup 0 0% Desired Output ID, Status, Error Code, Start Time ,Elapsed ,End Time ,Type ,Progress Bytes ,New Bytes ----------------,------------------------, ---------- ,--------------------, -----------, --------------------, ---------------- ,----------------- ,--------- 9133910640004809, Completed w/Exception(s), 10020, 2012-06-07 18:00 EDT, 00h:53m:46s ,2012-06-07 18:53 EDT, Scheduled Backup, 215 , 0.2% 9133914600006909, Completed , 0, ,2012-06-08 05:00 EDT, 00h:00m:04s ,2012-06-08 05:00 EDT, Scheduled Backup, 0 , 0% Code I Tried awk '!/---/{$1=x; sub(/,+$/,x)}1' OFS=, but its not working as its putting comma for every space JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You posted expected output doesn't really make sense (e.g. 2 commas for one apparent field at 0, , and sometimes you have blanks before the comma, sometimes after it, and sometimes before AND after it) and I think your sample input isn't accurate (e.g. 215 and 0.2% appears too close together) so I'm guessing but - using GNU awk for FIEDWIDTHS and nextfile, this may be what you really want to do: $ cat tst.awk NR == FNR { if ( /^[- ]+$/ ) { wids = length($1) for ( i=2; i<=NF; i++ ) { wids = wids " " length(FS $i) } nf = NF nextfile } next } FNR == 1 { FIELDWIDTHS = wids OFS = "," $0 = $0 } NF { $nf = $nf } { print } $ awk -f tst.awk file file ID , Status , Error Code, Start Time , Elapsed , End Time , Type , Progress Bytes , New Bytes ----------------, ------------------------, ----------, --------------------, -----------, --------------------, ----------------, -----------------, --------- 9133910640004809, Completed w/Exception(s), 10020 , 2012-06-07 18:00 EDT, 00h:53m:46s, 2012-06-07 18:53 EDT, Scheduled Backup, 215 0.2%, 9133914600006909, Completed , 0 , 2012-06-08 05:00 EDT, 00h:00m:04s, 2012-06-08 05:00 EDT, Scheduled Backup, 0 , 0% or: $ cat tst.awk NR == FNR { if ( /^[- ]+$/ ) { wids = length($1) for ( i=2; i<=NF; i++ ) { wids = wids " " length(FS $i) } nf = NF nextfile } next } FNR == 1 { FIELDWIDTHS = wids OFS = "," $0 = $0 } NF { $nf = $nf $0 = gensub(/( +),/,",\\1","g") } { print } $ awk -f tst.awk file file ID, Status, Error Code, Start Time, Elapsed, End Time, Type, Progress Bytes, New Bytes ----------------, ------------------------, ----------, --------------------, -----------, --------------------, ----------------, -----------------, --------- 9133910640004809, Completed w/Exception(s), 10020, 2012-06-07 18:00 EDT, 00h:53m:46s, 2012-06-07 18:53 EDT, Scheduled Backup, 215 0.2%, 9133914600006909, Completed, 0, 2012-06-08 05:00 EDT, 00h:00m:04s, 2012-06-08 05:00 EDT, Scheduled Backup, 0, 0%

Related questions

0 votes
    When coding in elisp, I find that I'm stopping at hyphens when moving by words, and would prefer ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    In shell script, How can I set 0 as default value to parameter. param2=0 ${2:-0} and ${2 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    In shell script, How can I set 0 as default value to parameter. param2=0 ${2:-0} and ${2 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    In shell script, How can I set 0 as default value to parameter. param2=0 ${2:-0} and ${2 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I have inherited a corporate server & application that consists of several python scripts, html files, and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    How do I write a function to split and return an array for a string with delimiters in the C ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I have a parameters.ini file, such as: [parameters.ini] database_user = user database_version = 20110611142248 I want to ... I do this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How can we pretty-print JSON in a shell script?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    In Intellij IDE's such as PyCharm or Idea there is an option to customize the menu and toolbar by ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format? Here's what I'm doing now: mysql -u uid -ppwd -D dbname...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I have trained a binary classification model with CNN, and here is my code model = Sequential() model.add(Convolution2D ... I do that? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Question Type: Single-Select vars: score: 3 tasks: - shell: echo "**** on!! You won." when: ?? How do you use a variable to apply ... 3. when: score == 3 4. when: {{ score }} == 3...
asked Jan 26, 2023 in Technology by JackTerrance
0 votes
    I want to run a python script from a shell script. I have been able to run it from the "command ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I want to run a python script from a shell script. I have been able to run it from the "command ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    Following query is perfectly working in mongo shell. db.collection.find({ "_id" : UUID("87aa9ed6-8485-4517 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
...