LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2008, 01:20 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
Convert into CSV file


Hi All,

I have one file which has account names (Bold), successful (green colour)and failed (red colur)logins. Below is some extract of this file.

$cat logins
lbamcmbs
Successful Logins.
Internal: 12/07/2008 00:01:22 (10.56.73.107)
External: n/a (0)

Failed Logins.
Internal: n/a (0)
External: n/a (0)

ntrs4
Successful Logins.
Internal: n/a (0)
External: 15/07/2008 09:21:55 (192.77.161.22)

Failed Logins.
Internal: n/a (0)
External: n/a (0)


I want this file to be converted into CSV format which should have format of

AccountName,SuccessfulLogins,FailedLogins
lbamcmbs,Internal: 12/07/2008 00:01:22 External: n/a (0),Internal: n/a (0)External: n/a (0)
ntrs4,Internal: n/a (0) External: 15/07/2008 09:21:55 (192.77.161.22),Internal: n/a (0)External: n/a (0)


There are many accounts in this file. Can anyone please suggest me any script to get this work done. My manger wants it to be done by EOD and there are many accounts which will cost my whole day

Any help will be much appreciated.

Thanks
Ravi
 
Old 07-16-2008, 04:40 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Something like this may work:

Code:
awk 'BEGIN { 
  FS = ","
  print "AccountName,SuccessfulLogins,FailedLogins"
  }
!/Logins/ &&
    ORS = (/External/ && !(++c % 2)) ?
      RS : (!/Internal/ ? FS : OFS)
' logins
 
Old 07-16-2008, 07:13 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by say_hi_ravi View Post
My manger wants it to be done by EOD and there are many accounts which will cost my whole day
while radoulov has done it for you, however you should get your hands dirty since you are being paid to do the job. See my sig for shell scripting manual.
 
Old 07-17-2008, 03:09 AM   #4
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Original Poster
Rep: Reputation: 15
ghostdog74,

Completely agreed. I tried to write a code to fulfill my need. But I was not successful in that

Was thinking of taking account_name,successful logins, failed logins in different files and then merge them in one CSV file. I was able to parse output to three files but was getting stuck how to combine them. But at last your comment made me itching to complete my work. and i am glad i was successful. Thanks... you made my day.

My work around:

$cat logins|sed 's/,//g'|grep -v "Successful Logins."|grep -v "Failed Logins."|sed -n '1,${p,n;n;n;n;}' > account_name
$cat logins|sed 's/,//g'|grep -v "Successful Logins."|grep -v "Failed Logins."|sed -n '2,${p,n;n;n;n;}' > success_internal
$cat logins|sed 's/,//g'|grep -v "Successful Logins."|grep -v "Failed Logins."|sed -n '3,${p,n;n;n;n;}' > success_external
$cat logins|sed 's/,//g'|grep -v "Successful Logins."|grep -v "Failed Logins."|sed -n '4,${p,n;n;n;n;}' > fail_internal
$cat logins|sed 's/,//g'|grep -v "Successful Logins."|grep -v "Failed Logins."|sed -n '5,${p,n;n;n;n;}' > fail_external

it created 5 files

and then

$paste -d, account_name success_internal success_external fail_internal fail_external > final_result

It gave although not a desired result, but the result which i could produce, manager is happy with that

Thanks everyone. Thanks to radoulov. I have not tested this code but will soon be testing it. Curious to get understand each and every line of this code.

-Ravi
 
Old 07-17-2008, 03:25 AM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Note that the code I posted works with GNU Awk only. It will work with New Awk (nawk) and POSIX Awk (/usr/xpg4/bin/awk on Solaris) if the ternary operators do not span over multiple lines:

Code:
awk 'BEGIN { 
  FS = ","
  print "AccountName,SuccessfulLogins,FailedLogins"
  }
!/Logins/ && \
    ORS = (/External/ && !(++c % 2)) ? \
      RS : (!/Internal/ ? FS : OFS)
' logins
Regards
radoulov
 
  


Reply



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
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
looking for a perl script to convert html table data into a csv file swiftguy121 Linux - Software 2 04-25-2007 07:28 PM
convert excel to csv and html thelonius Programming 3 03-28-2007 11:16 AM
Script to convert csv 2 xls or odt xowl Linux - Software 1 01-16-2007 09:06 PM
convert CSV (TEXT) files to UTF-16 cccc Programming 1 07-01-2004 01:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

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