awk Varing Field Separators


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers awk Varing Field Separators
# 1  
Old 07-27-2011
Question awk Varing Field Separators

Hi Guys,

I have small dilemma which I could do with a little help solving . I currently have text HDD S.M.A.R.T report which I have pasted below:

Code:
smartctl 5.39 2008-10-24 22:33 [i686-suse-linux-gnu] (openSUSE RPM)
Copyright (C) 2002-8 by Bruce Allen, http://smartmontools.sourceforge.net
Device: COMPAQ BD036635C5 Version: B020
Serial number: 38322209 0123
Device type: disk
Local Time is: Mon Jul 26 10:46:24 2010 BST
Device supports SMART and is Enabled
Temperature Warning Enabled
SMART Health Status: OK
Current Drive Temperature: 18 C
Drive Trip Temperature: 65 C
Manufactured in week 23 of year 2001
Recommended maximum start stop count: 10000 times
Current start stop count: 209 times
Elements in grown defect list: 91
Error counter log:
Errors Corrected by Total Correction Gigabytes Total
ECC rereads/ errors algorithm processed uncorrected
           fast | delayed rewrites corrected invocations [10^9 bytes] errors
read:      0         906       155          60              0           38098.027     95
write:     0         109         0             0               0           1017.565        0
verify:    0            0           0             0               0            36.420           0
Non-medium error count: 258
SMART Self-test log
Num Test Status segment LifeTime LBA_first_err [SK ASC ASQ]
Description number (hours)
# 1 Background short Interrupted (bus reset ?) 2 22862 - [- - -]
# 2 Background short Failed in segment --> 3 22841 3525970 [0x4 0x15 0x1]
# 3 Background short Failed in segment --> 3 22838 1818820 [0x4 0x15 0x1]
# 4 Background short Failed in segment --> 3 22809 519934 [0x4 0x15 0x1]
Long (extended) Self Test duration: 2070 seconds [34.5 minutes]
Short Background Self Test has



I have written a small script to extract the few fields at the top of the report as show below which eventually gets saved as to a CSV file so I can import it into my database:

Code:
BEGIN {

Code:
 FS="[:]"
}
$1 ~ /^Device$/  {device = $2 $3}
$1 ~ /^Serial number$/     {ser = $NF}
$1 ~ /Device type$/     {devt = $NF}
$1 ~ /^Transport protocol$/ {tranp = $NF}
$1 ~ /^Local Time is$/ {time = $2 $3 $4}
$1 ~ /^SMART Health Status$/ {smrtstat = $NF}
$1 ~ /^Elements in grown defect list$/ {glist = $2}

END {print device "," ser "," devt "," tranp "," time "," smrtstat "," glist "," readeccf "," readeccd "," readrereadrewt "," readtoterrcorrec "," readcoralginvocat "," readgigpro "," readtotuncorerr "," writeeccf "," writeeccd "," writerewriterewt "," writetoterrcorrec "," writecoralginvocat "," writegigpro "," writetotuncorerr; exit}



This extracts the desired fields perfectly but.... I don't just want these feilds. My trouble starts when I begin to try and extract the data in the fields "read:" & "write:". I'm trying to extract the 7 columns individually so they appear in me csv file as

Code:
device, serial, etc.....0, 906, 155, 60, 0, 38098.027, 95.....



My problem seems to be that my field separator is set to a ":" and these fields are separated by blank space. Can I get awk to change the Fields Separator half way through my script? I'm a bit of a newbie to this but I'm picking it up pretty quick.

Any help would be gratefully appreciated.

Thanks
bb
# 2  
Old 07-27-2011
Just a hint:
Code:
echo 'read:      0         906       155          60              0           38098.027     95'  \
| awk '{split($0, a); print a[2] "," a[3] ",..."}'

# 3  
Old 07-27-2011
Hi yazu!!

Thank you for your promt reply, unfortunatly I don't belive this will work. I forgot to metion that this is just one report of many so obviously the data in the read fields verry from report to report. Thank you for your input though.

---------- Post updated at 05:56 PM ---------- Previous update was at 01:53 PM ----------

Ahhh I solved my problem by creating a seperate awk script to parse out the data using the following Feild Separator:
Code:
FS="[: ]+"

I then added a line to my bash script to compensate for this extra extraction and then merged my two output files together using sed. Its about as elegant as midnight bar brawl but it works for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing out data with multiple field separators

I have a large file that I need to print certain sections out of. file.txt /alpha/beta/delta/gamma/425/590/USC00015420.blah.lt.0.01.str:USC00015420Y2017M10BLALT.01 12 13 14 -9 1 -9 -9 -9 -9 -9 1 2 3 4 5 -9 -9 I need to print the "USC00015420" and... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

2. Shell Programming and Scripting

Extract lines with min value, using two field separators.

I have a file with two ID columns followed by five columns of counts in fraction form. I'd like to print lines that have a count of at least 4 (so at least 4 in the numerator, e.g. 4/17) in at least one of the five columns. Input file: comp51820_c1_seq1 693 0/29 0/50 0/69 0/36 0/31... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

3. Shell Programming and Scripting

Multiple long field separators

How do I use multiple field separators in awk? I know that if I use awk -F"", both a and b will be field separators. But what if I need two field separators that both are longer than one letter? If I want the field separators to be "ab" and "cd", I will not be able to use awk -F"". The ... (2 Replies)
Discussion started by: locoroco
2 Replies

4. UNIX for Dummies Questions & Answers

Can one use 2 field separators in awk?

I have files such as n02-z30-dsr65-terr0.25-dc0.008-16x12drw-run1.cmd I am wondering if it is possible to define two field separators "-" and "." for these strings so that $7 is run1. (5 Replies)
Discussion started by: kristinu
5 Replies

5. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

6. Shell Programming and Scripting

Problem with changing field separators in a file

I have a file with content as shown below. cat t2 : 100,100,"X",1234,"12A",,,"ab,c" Comma is the field seperator, however string fields will be within double quotes and comma within double quotes should not be treated as field seperator. I am trying to replace this field seperator to a... (7 Replies)
Discussion started by: mk1216
7 Replies

7. UNIX for Dummies Questions & Answers

Multiple field separators in awk? (First a space, then a colon)

How do I deal with extracting a portion of a record when multiple field separators are involved. Let's say I have: Mike Harrington;(555) 555-5555:250:100:175 Christian Dobbins;(555) 555-2358:155:90:201 Susan Dalsass;(555) 555-6279:250:60:50 Archie McNichol;(555) 555-1348:250:100:175 Jody... (3 Replies)
Discussion started by: doubleminus
3 Replies

8. Shell Programming and Scripting

Multiple input field Separators in awk.

I saw a couple of posts here referencing how to handle more than one input field separator in awk. I figured I would share how I (just!) figured out how to turn this line in a logfile: 90000000000000000000010001 name... (4 Replies)
Discussion started by: kinksville
4 Replies

9. Shell Programming and Scripting

I need help counting the fields and field separators using Nawk

I need help counting the fields and field separators using Nawk. I have a file that has multiple lines on it and I need to read the file 1 at a time and then count the fields and field separators and then store those numbers in variables. I then need to delete the first 5 fields and the blank... (3 Replies)
Discussion started by: scrappycc
3 Replies

10. Shell Programming and Scripting

Awk Multiple Field Separators

Hi Guys, I'm tying to split a line similar to this:YO6-2000-30.htm: (3 properties found).......into separate columns, so effectively I need to check for a -, ., :, a tab and a space in the statement. Any help would be appreciated Thanks! (7 Replies)
Discussion started by: Tonka52
7 Replies
Login or Register to Ask a Question