Sponsored Content
Top Forums Shell Programming and Scripting AWK printing formatting help please Post 302518545 by newbie_01 on Saturday 30th of April 2011 07:24:09 AM
Old 04-30-2011
AWK printing formatting help please

Hi all,

Below is my testfile:

Code:
COST,31-MAR-2011 01:01:04,31-MAR-2011 11:22:12,622
COST,21-MAR-2011 22:00:20,22-MAR-2011 11:07:23,788
FARE,23-MAR-2011 22:00:22,24-MAR-2011 10:10:46,731
FARE,02-MAR-2011 14:01:50,03-MAR-2011 08:30:54,1110

I need to append a number, for example 700, to the end of each record when the fourth field is >700 so I run the command as below which gives me the output that I wanted.

Code:
cat testfile.0 | awk -F, '$4 >= 700' | awk '{ print $0","700 }'

COST,21-MAR-2011 22:00:20,22-MAR-2011 11:07:23,788,700
FARE,23-MAR-2011 22:00:22,24-MAR-2011 10:10:46,731,700
FARE,02-MAR-2011 14:01:50,03-MAR-2011 08:30:54,1110,700

All good so far. Just want to know if there is a "better" way of writing this AWK construct?

Next thing is I wanted to add a column heading and get a clean printed output in tabular format sort of so I did as below:

Code:
cat /dev/null > testfile.1
echo "FILE_TYPE,START,END,DURATION,THRESHOLD" >> testfile.1
echo "---------,-----,---,--------,---------" >> testfile.1
cat testfile.0 | awk -F, '$4 >= 700' | awk '{ print $0","700 }' >> testfile.1
cat testfile.1 | awk -F, '{ printf "%-20s \t %-20s \t %-20s \t %-20s \t %-20s \n", $1, $2, $3, $4, $5}'

Output is as below:

FILE_TYPE                START                   END                     DURATION                THRESHOLD
---------                -----                   ---                     --------                ---------
COST                     21-MAR-2011 22:00:20    22-MAR-2011 11:07:23    788                     700
FARE                     23-MAR-2011 22:00:22    24-MAR-2011 10:10:46    731                     700
FARE                     02-MAR-2011 14:01:50    03-MAR-2011 08:30:54    1110                    700

Okay, so far so good. The only thing that I want to know now is whether there is an AWK function that will let me print x-number of dashes? Just so in matches the %-20s that am using in the printf?

I know it's a "cosmetic" thingy, I can easily just type in 20 dashes when am doing the echo. Just thought maybe there is an AWK function that will let me do it the "smart" way or any UNIX command that will let me do the same.

Any tips/suggestions will be much appreciated. Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting using awk

Let's say I write a simple script that contains the following: date | awk '{print $1}' date | awk '{print $2}' Of course, when I run the script the output will look similar to: Tue Mar What if I want my ouput to be on one line as follows: Tue Mar What changes would I need to... (2 Replies)
Discussion started by: cdunavent
2 Replies

2. Shell Programming and Scripting

AWK Formatting Problem

Hi All, I'm having a problem with the way awk is interperting a space between double quotes in a for loop. Below is the code and output from running the script: AWK for loop: for i in $(awk 'BEGIN{FS=","}{print "Probe Name:" $1};{print "Probe Temp:" $2};{ print... (2 Replies)
Discussion started by: cstovall
2 Replies

3. Shell Programming and Scripting

Help with txt formatting using AWK

Hi, Ive used unix.com to help learn the basics of AWK to format txt files however ive run out of talent and could do with some help. Im not sure if this is possible using awk but I have an input as follows L73-10 342 0 1480 L73-10 342 100 1480 L73-10 342 250 1656 L73-10 342 500 1746... (13 Replies)
Discussion started by: eknryan
13 Replies

4. Shell Programming and Scripting

html formatting using awk

Hi I have a file as given below: <table border=1> <TR><TH>Script Name</TH><TH>CVS Status</TH><TH>Script Location</TH></TR> <TR><TD><CENTER>Work Area: /home/ustst/</CENTER></TD></TR> <TR><TD><CENTER>admin_export.sh</CENTER></TD><TD><CENTER>Locally... (1 Reply)
Discussion started by: sudvishw
1 Replies

5. Shell Programming and Scripting

AWK formatting help.

Dear all I require help with AWK regarding this situation Input is : fn1 12345 fn1 23456 fn3 231513 fn1 22325 fn3 123125 Desired output is fn1 12345 23456 22325 fn3 231513 123125 (5 Replies)
Discussion started by: Peasant
5 Replies

6. Shell Programming and Scripting

formatting awk

when i try this awk its giving out put as below. awk '!(/^$/||/--/||/selected/||/^ *$/){print $1}' tmp.txt output ===== 1 2010-08-03-12.31.26.126000 how excluede the 1st line ? i mean i want output only 2nd line i.e 2010-08-03-12.31.26.126000; (5 Replies)
Discussion started by: rocking77
5 Replies

7. UNIX for Dummies Questions & Answers

awk formatting

Hi all, I'm writing a simple awk code: awk 'BEGIN {FS="|"};{print "Type\tNumber\ttypes\tTotal";};{print $1, "\t", $2, "\t", $3, "\t", $4, "\t";}' db_query.txt it gives me the result: Type Number types Total XXX 498.0 5100.0 5274.661 Type Number types Total... (7 Replies)
Discussion started by: messi777
7 Replies

8. UNIX for Dummies Questions & Answers

Help with awk'ing formatting this

Hi, I have a file below that I am wanting to awk. The lines of relevance are lines 7 and 9 $ nl /tmp/x 1 ADRCI: Release 11.2.0.3.0 - Production on Sun Jun 23 17:01:02 2013 2 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. 3 ADR base =... (2 Replies)
Discussion started by: newbie_01
2 Replies

9. Shell Programming and Scripting

Data formatting using awk

Need assistance on the data extraction using awk Below is the format and would like to extract the data in another format ------------------------------------------------------------------------------------------------- Minimum Temperature (deg F ) DAY 1 2 3 4 5 6 7 8 9 10 11... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

10. Shell Programming and Scripting

Suffix formatting with awk

i would like to format the 9 character with suffix as "0". i tried below it doesn't work. >a=12345 > echo $a | awk '{printf "%-09s\n",$1}' >12345 required output is 123450000 can you guys help me out ? (7 Replies)
Discussion started by: expert
7 Replies
bup-margin(1)						      General Commands Manual						     bup-margin(1)

NAME
bup-margin - figure out your deduplication safety margin SYNOPSIS
bup margin [options...] DESCRIPTION
bup margin iterates through all objects in your bup repository, calculating the largest number of prefix bits shared between any two entries. This number, n, identifies the longest subset of SHA-1 you could use and still encounter a collision between your object ids. For example, one system that was tested had a collection of 11 million objects (70 GB), and bup margin returned 45. That means a 46-bit hash would be sufficient to avoid all collisions among that set of objects; each object in that repository could be uniquely identified by its first 46 bits. The number of bits needed seems to increase by about 1 or 2 for every doubling of the number of objects. Since SHA-1 hashes have 160 bits, that leaves 115 bits of margin. Of course, because SHA-1 hashes are essentially random, it's theoretically possible to use many more bits with far fewer objects. If you're paranoid about the possibility of SHA-1 collisions, you can monitor your repository by running bup margin occasionally to see if you're getting dangerously close to 160 bits. OPTIONS
--predict Guess the offset into each index file where a particular object will appear, and report the maximum deviation of the correct answer from the guess. This is potentially useful for tuning an interpolation search algorithm. --ignore-midx don't use .midx files, use only .idx files. This is only really useful when used with --predict. EXAMPLE
$ bup margin Reading indexes: 100.00% (1612581/1612581), done. 40 40 matching prefix bits 1.94 bits per doubling 120 bits (61.86 doublings) remaining 4.19338e+18 times larger is possible Everyone on earth could have 625878182 data sets like yours, all in one repository, and we would expect 1 object collision. $ bup margin --predict PackIdxList: using 1 index. Reading indexes: 100.00% (1612581/1612581), done. 915 of 1612581 (0.057%) SEE ALSO
bup-midx(1), bup-save(1) BUP
Part of the bup(1) suite. AUTHORS
Avery Pennarun <apenwarr@gmail.com>. Bup unknown- bup-margin(1)
All times are GMT -4. The time now is 08:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy