Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 07-01-2016, 05:15 PM   #1
LQ Newbie
Registered: Feb 2016
Distribution: Solaris, Redhat Linux, Ubuntu
Posts: 2

Rep: Reputation: Disabled
Scripting question

Thanks in advance to all!
I have a txt file in the format of:

Title   Ensure Solid State Drives Do Not Contribute To Random-Number Entropy Pool
Rule    kernel_disable_entropy_contribution_for_solid_state_drives
Result  pass

Title   Ensure /tmp Located On Separate Partition
Rule    partition_for_tmp
Ident   CCE-26435-8
Ident   DISA FSO RHEL-06-000001
Result  fail

Title   Ensure /var Located On Separate Partition
Rule    partition_for_var
Ident   CCE-26639-5
Ident   DISA FSO RHEL-06-000002
Result  pass[/INDENT]
I'm trying to make a csv file out of this with Title, Rule, Ident, Ident, Result lines on the same line, separated by a comma or course. I tried using awk and set the RS to "" for an empty line to separate the blocks of text I want but I keep getting line feeds. I tries setting FS, OFS, and ORS in various setting with same or useless results. Can some tell me how to modify my code to get:

Title   Ensure /var Located On Separate Partition, Rule    partition_for_var, Ident   CCE-26639-5, Ident   DISA FSO RHEL-06-000002,Result  pass
Here is my code so far:

awk 'BEGIN { RS="" ; ORS=", \n" }

	print $1 
}' < file.tst >> out.file
I'm using ksh but I can do sh or bash as well. I know I'm missing something simple but I can't see it.

Old 07-01-2016, 06:58 PM   #2
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Awk will do it... but you have to leave the RS alone.

Something like:
BEGIN {rec="";}                     # not strictly needed, but sets rec to an empty string

/^$/  { printf("\"%s\"\n",rec);     # rule for an empty line - print rec with quotes
        rec = "";                   # starting condition for next output
        next;                       # required to skip the following rules

      { if (rec == "")              # non empty lines, if rec is empty, rec is the current line
            rec = $0;
            rec = rec "\",\"" $0;   # otherwise put "," in between
        next;                       # skip following rules
END   {if (rec != "")               # end of file handling...
           printf("\"%s\"\n",rec);  # print only if rec is not empty
I suggest trying this by putting the awk script in a file, and invoke it as "awk -f that_file < your_data"

One last thing - I assumed the data didn't start with an empty line, and that only one empty line is between your sections of data. I added the quoting just in case, but I didn't check to see if the data contained a quote (") character.

Last edited by jpollard; 07-01-2016 at 07:03 PM.
1 members found this post helpful.
Old 07-01-2016, 07:42 PM   #3
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
On the first group there are no Ident lines, do you want the script check if all fields are set?
If yes, I would go with something like jpollard posted

If not, something simple can do it as well:
awk '/^Result/{print;next} NF{printf $0", "}' file
awk '/^Result/{print;next}
print the line (with newline character) and next if line begins with Result (the last group line)

NF{printf $0", "}
print the line concatenated with ", " (without newline character) if the line is non empty (contains possible awk fields if NF)

Last edited by keefaz; 07-01-2016 at 07:49 PM.
2 members found this post helpful.
Old 07-11-2016, 01:01 PM   #4
LQ Newbie
Registered: Feb 2016
Distribution: Solaris, Redhat Linux, Ubuntu
Posts: 2

Original Poster
Rep: Reputation: Disabled
Many Thanks to jpollard and keefaz for the replys. I ended up using keefaz's command but both were helpful. I also learned a little more about awk, so a win win all the way around.
Thanks again!!!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
scripting question bino25 Linux - General 1 03-01-2012 09:56 AM
scripting question. mrbutabara Linux - Newbie 8 01-27-2009 10:43 AM
Scripting Question caps_phisto Linux - General 1 11-05-2004 03:43 PM
Scripting Question... Darklight451 Linux - Newbie 3 09-23-2004 06:03 PM
Scripting question.... Darklight451 Linux - Newbie 2 09-14-2004 12:29 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration