Need specific columns in a log file as excel.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need specific columns in a log file as excel.
# 1  
Old 08-18-2014
Need specific columns in a log file as excel.

Hi All...
I am in need of few columns from a log file.. in .xls file... below is what i have tried.

my log file has 16 colums with " ; " as delimiter, but i need randomn columns 1 2 3 4 5 6 10 11 16 in an excel.

I tried to awk the columns with delimiter ; and it worked, below is the log after awk.

Code:
awk -F'; ' '{ print $1,$2,$3,$4,$5,$6,$10,$11,$13,$16 }' Report.log


Code:
  

Sun Aug 10 00:02:06 2014  status  ACK  Administrator admin  0  Sat Aug 09 23:17:41 2014  loret   ctn1029  MAJOR
Sun Aug 10 00:02:17 2014  ticket_number  Administrator admin  0  Sat Aug 09 23:17:41 2014  loret   ctn1029  MAJOR
Sun Aug 10 00:04:21 2014  status  ACK  Administrator admin  0  Sat Aug 09 23:53:20 2014  loret  ctn68  WARNING
Sun Aug 10 00:04:21 2014  status  ACK  ldapuser1 admin  0  Sat Aug 09 23:53:20 2014  loret  ctn1342  WARNING

Now i need this in an excel... I tried tr command but did not work, i am unable to understand the delimiter ... tried with /t but dint help.. please suggest...

Here data in columns are
Code:
column 1: Sun Aug 10 00:02:05 2014
Column 2:  status
Column3: ACK
Column4 Administrator
Column5: admin
Count6: 0 
Column7: Sat Aug 09 23:53:20 2014  
Column8: loret  
Column9: ctn1342  
Column10: WARNING

OR..

can AWK give us output with delimiter... ? this is also fine with me as i can use tr convert to excel...


Thanks in Advance !!!
Moderator's Comments:
Mod Comment Please use CODE tags for sample output as well as for sample input and code.

Last edited by Don Cragun; 08-18-2014 at 02:09 PM.. Reason: Add missing CODE tags.
# 2  
Old 08-18-2014
The default value of the field separator FS is a string " " containing a single space, awk splits an input record into fields is controlled by the field separator. In your case if you want to open in excel try to create file using double quotes for example as you shown column 1 should be field1 to field5 would be print "\"" $1, $2, $3, $4, $5 "\"" "," where column separator is comma
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 08-19-2014
If that doesn't work, you could try one of these:-
  • save the file as a .txt and when you open it with Excel you will be prompted for information including the delimiters. You can set a space there, but it will compromise your date/time field
    .
  • Assuming that there are no other ; in the input, just a simple cut -f 1-6,10,11,16 -d";" input-file | tr ";" "," > output_file.csv
Do either of these options help?


Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 08-19-2014
Both worked out.. Robin & Akshay... Thanks much...

Another doubt... I am trying to send this csv to email.. i used send mail and mailx but dint work... once i give this i dont get the prompt it gets struck there....

I am using Solaris.. Please suggest

Code:
uuencode <filename.txt>  | mail -s "subject" mailid
uuencode <filename.txt>  | mailx -s "subject" mail id
uuencode <filename.txt>  | sendmail -f <mailid> -v -t

Please suggest
# 5  
Old 08-19-2014
Try this script

Code:
#!/bin/bash

from="sender@domain.org"
to="receiver@domain.org"
subject="test subject"
body="test message"

# string more detail read this https://tools.ietf.org/html/rfc2046#section-5.1.1
boundary="ZZ_/afg6432dfgkl.94531q"


# add attachements...here...
attachments=( "adb.log" "mail.sh")



# function......
get_mimetype(){
  file --mime-type "$1" | sed 's/.*: //' 
}

# Build headers
{

printf '%s\n' "From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"

--${boundary}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

$body
"
 
# now loop over the attachments
for file in "${attachments[@]}"; do

  [ ! -f "$file" ] && echo "Warning: attachment $file not found, skipping" >&2 && continue

  mimetype=$(get_mimetype "$file") 
 
  printf '%s\n' "--${boundary}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
"
 
  base64 "$file"
  echo
done
 
# print last boundary with closing --
printf '%s\n' "--${boundary}--"
 
} | sendmail -t -o

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 08-19-2014
I usually use comma delimited output for Excel you can do this by setting OFS or adding commas to your print line.

Mike
This User Gave Thanks to Michael Stora For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to accept specific columns from excel

Hi All, I have below perl script which writes xml from .xls file. Now i want to add below two conditions in this script : 1. to check if the the input .xls file has ony two columns , if more tahn two columns then script should pop up an error. 2. If there are two columns , then first column... (4 Replies)
Discussion started by: omkar.jadhav
4 Replies

2. Shell Programming and Scripting

Reading specific range of columns in an Excel file

Hi All, I want to read an excel file. PFA excel, I want to read the cloumn from A to G and the V to AH starting from Row number 3. Please help me on this. (7 Replies)
Discussion started by: Abhisrajput
7 Replies

3. UNIX for Dummies Questions & Answers

How to delete columns with numbers in an excel file?

Dear all, I have one file (see below) with more then 100 columns and 2500 rows, and need only column which has GType in label with Alphabets, please help me to remove these columns with numbers. input file is n.201.GType n-201.Theta n-201.R n_1.GType n_1.Theta n_1.R... (6 Replies)
Discussion started by: AAWT
6 Replies

4. Shell Programming and Scripting

Transpose whole file and specific columns

Hi, I have a file like this a b c d e f g h i j k l Case1: I want to transpose the whole file Output1 a d g j b e h k c f i l Case2 Transpose a specific column - Say 3rd (6 Replies)
Discussion started by: jacobs.smith
6 Replies

5. Shell Programming and Scripting

Converting specific Excel file tabs to CSV in Python

Hi list, This is probably something really simple, but I am not particularly familiar with Python so I thought I would ask as I know that python has an excel module. I have an excel document with multiple tabs of data and graphs. One of the tabs is just data which I require to have dumped to... (8 Replies)
Discussion started by: landossa
8 Replies

6. UNIX for Dummies Questions & Answers

To compare first two columns in an excel file

Hi All, i have a excel sheet with two columns as below. column1 column2 100 100 200 300 300 400 400 400 500 600 i need to compare the values these two columns and the output should be printed in the third column...if these values are equal the output should be green and if these... (2 Replies)
Discussion started by: arunmanas
2 Replies

7. Shell Programming and Scripting

Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this. My question: I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it... (3 Replies)
Discussion started by: mehdib
3 Replies

8. Shell Programming and Scripting

How to sort columns in excel(csv) file

i want sort columns with headers based on another file headers file1 eg: i'm having an empty file with only coumn names like lastname firstname title expirydate stlcno status etc... another file with same column names and some other as well but in different order... file2 eg:firstname... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies

9. Shell Programming and Scripting

how to convert fields from a text file to excel columns

i have this file which has the following contents: ,-0.3000 ,-0.3000 ,-0.3000 ,-0.9000 ,-0.9000 ,-0.9000 i would like to get this: -0.3-0.9-0.3-0.9-0.3-0.9 so far i am trying: awk '{for(i=1; i<=NF; i++) {printf("%f\n",$i)}}' test1 > test2 any help... (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

use awk to read randomly located columns in an excel file

Hi, I have an excel file that have a random count of columns/fields and what im trying to do is to only retrieve all the rows under 2 specific field headers. I can use the usually command for awk which is awk 'print{ $1 $2}' > output.txt, but the location of the 2 specific field headers is... (9 Replies)
Discussion started by: mdap
9 Replies
Login or Register to Ask a Question