AWK - PRETTY PRINTING

Similar documents
Fundamentals of Programming

Fundamental of Programming (C)

Formatted Output Pearson Education, Inc. All rights reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Control Flow Statements. Execute all the statements grouped in the brackets. Execute statement with variable set to each subscript in array in turn

Unit 4. Input/Output Functions

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

Number Systems, Scalar Types, and Input and Output

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

BSM540 Basics of C Language

Formatted Input/Output

Introduction to Computing Lecture 03: Basic input / output operations

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Java Basic Datatypees

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Advanced C Programming Topics

RUBY VARIABLES, CONSTANTS AND LITERALS

Chapter 9 Strings. With this array declaration: char s[10];

STREAM EDITOR - REGULAR EXPRESSIONS

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Work relative to other classes

Chapter 2 THE STRUCTURE OF C LANGUAGE

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

11 Console Input/Output

Computer Organization & Systems Exam I Example Questions

Basic Elements of C. Staff Incharge: S.Sasirekha

Fundamentals of Programming. Lecture 11: C Characters and Strings

PYTHON MOCK TEST PYTHON MOCK TEST III

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Variables and Literals

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Variables, Constants, and Data Types

AWK - BUILT-IN FUNCTIONS

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Standard I/O in C and C++

CSC 1107: Structured Programming

'...' "..." escaping \u hhhh hhhh '''...''' """...""" raw string Example: r"abc\txyz\n" in code;

Formatting functions in C Language

Input/Output: Advanced Concepts

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Computer System and programming in C

THE FUNDAMENTAL DATA TYPES

Using printf with an AVR

Chapter 2. Lexical Elements & Operators

Chapter 8: Character & String. In this chapter, you ll learn about;

VARIABLES AND CONSTANTS

UNIT- 3 Introduction to C++

C: How to Program. Week /Mar/05

Basic Types and Formatted I/O

PYTHON- AN INNOVATION

CpSc 1111 Lab 4 Formatting and Flow Control

Operators and Control Flow. CS449 Fall 2017

CS1100 Introduction to Programming

C mini reference. 5 Binary numbers 12

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

Chapter 2 - Introduction to C Programming

printf( Please enter another number: ); scanf( %d, &num2);

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

- c list The list specifies character positions.

Lecture 3. More About C

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Programming for Engineers Introduction to C

BSM540 Basics of C Language

Number Systems & Encoding

Fundamentals of Programming Session 4

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Programming in C++ 4. The lexical basis of C++

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Python Working with files. May 4, 2017

Chapter 11 Input/Output (I/O) Functions

EL2310 Scientific Programming

JFlex Regular Expressions

ES 117. Formatted Input/Output Operations

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

بسم اهلل الرمحن الرحيم

Introduction to C Language

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Engineering Computing I

UNIVERSAL SERIAL INTERFACE

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

CC112 Structured Programming

C Basics. Chapter 2: C Basics. C Programming Language. v3.3 February 6, 2017

Data Input and Output 187

Transcription:

AWK - PRETTY PRINTING http://www.tutorialspoint.com/awk/awk_pretty_printing.htm Copyright tutorialspoint.com So far we have used AWK's print and printf functions to display data on standard output. But printf is much more powerful than what we have seen before. This function is borrowed from the C language and is very helpful while producing formatted output. Below is the syntax of the printf statement: printf fmt, expr-list In above syntax fmt is a string of format specifications and constants. expr-list is a list of arguments corresponding to format specifiers. Escape Sequences Like any string, format can also contain embedded escape sequences. Below is the list of escape sequences which are supported by AWK: New Line Following example prints Hello and World in separate lines using newline character: [jerry]$ awk 'BEGIN { printf "Hello\nWorld\n" }' Hello World Horizontal Tab Following example uses horizontal tab to display different field: [jerry]$ awk 'BEGIN { printf "Sr No\tName\tSub\tMarks\n" }' Sr No Name Sub Marks Vertical Tab Following example uses vertical tab after each filed: [jerry]$ awk 'BEGIN { printf "Sr No\vName\vSub\vMarks\n" }' Sr No Name Sub Marks Backspace Following example prints a backspace after every field except last one. This erases the last number from first three fields. For instance Field 1 is displayed as Field, because the last character is erased with backspace. However the last field Field 4 is displayed as it is, as we did not have a \b after Field 4.

[jerry]$ awk 'BEGIN { printf "Field 1\bField 2\bField 3\bField 4\n" }' Field Field Field Field 4 Carriage Return In the following example, after printing every field, we do a Carriage Return and print the next value on top of the current printed value. This means, in the final output you will see only Field 4, as it was the last thing to be printed on top of all the previous fields. [jerry]$ awk 'BEGIN { printf "Field 1\rField 2\rField 3\rField 4\n" }' Field 4 Form Feed Below example uses form feed after printing each field. [jerry]$ awk 'BEGIN { printf "Sr No\fName\fSub\fMarks\n" }' Sr No Name Sub Marks Format Specifier Like C-language AWK also has format specifiers. The AWK versions of the printf statement accepts the following conversion specification formats: %c It prints a single character. If the argument used for %c is numeric, it is treated as a character and printed. Otherwise, the argument is assumed to be a string, and the only first character of that string is printed. [jerry]$ awk 'BEGIN { printf "ASCII value 65 = character %c\n", 65 }' ASCII value 65 = character A %d and %i It prints only integer part of a decimal number. [jerry]$ awk 'BEGIN { printf "Percentags = %d\n", 80.66 }' Percentags = 80 %e and %E

It prints a floating point number of the form [-]d.dddddde[+-]dd. [jerry]$ awk 'BEGIN { printf "Percentags = %E\n", 80.66 }' Percentags = 8.066000e+01 The %E format uses E instead of e. [jerry]$ awk 'BEGIN { printf "Percentags = %e\n", 80.66 }' Percentags = 8.066000E+01 %f It prints a floating point number of the form [-]ddd.dddddd. [jerry]$ awk 'BEGIN { printf "Percentags = %f\n", 80.66 }' Percentags = 80.660000 %g and %G Uses %e or %f conversion, whichever is shorter, with non-significant zeros suppressed. [jerry]$ awk 'BEGIN { printf "Percentags = %g\n", 80.66 }' Percentags = 80.66 The %G format uses %E instead of %e. [jerry]$ awk 'BEGIN { printf "Percentags = %G\n", 80.66 }' Percentags = 80.66 %o It prints an unsigned octal number. [jerry]$ awk 'BEGIN { printf "Octal representation of decimal number 10 = %o\n", 10}' Octal representation of decimal number 10 = 12 %u It prints an unsigned decimal number.

[jerry]$ awk 'BEGIN { printf "Unsigned 10 = %u\n", 10 }' Unsigned 10 = 10 %s It prints a character string. [jerry]$ awk 'BEGIN { printf "Name = %s\n", "Sherlock Holmes" }' Name = Sherlock Holmes %x and %X It prints an unsigned hexadecimal number. The %X format uses uppercase letters instead of lowercase. [jerry]$ awk 'BEGIN { printf "Hexadecimal representation of decimal number 15 = %x\n", 15}' Hexadecimal representation of decimal number 15 = f Now let use use %X and observe the result: [jerry]$ awk 'BEGIN { printf "Hexadecimal representation of decimal number 15 = %X\n", 15}' Hexadecimal representation of decimal number 15 = F %% It prints a single % character and no argument is converted. [jerry]$ awk 'BEGIN { printf "Percentags = %d%%\n", 80.66 }' Percentags = 80% Optional Parameters with % With % we can use following optional parameters: Width The field will be padded to the width. By default field is padded with spaces but when 0 flag is used, it is padded with zeroes. [jerry]$ awk 'BEGIN { num1 = 10; num2 = 20; printf "Num1 = %10d\nNum2 = %10d\n", num1,

Num1 = 10 Num2 = 20 Leading Zeros A leading 0 zero acts as a flag, that indicates output should be padded with zeroes instead of spaces. Please note that this flag only has an effect when the field width is wider than the value to be printed. Below example describes this: [jerry]$ awk 'BEGIN { num1 = -10; num2 = 20; printf "Num1 = %05d\nNum2 = %05d\n", num1, Num1 = -0010 Num2 = 00020 Left Justification The expression should be left-justified within its field. When the input-string is less than the number of characters specified, and you would like it to be left justified i.e. by adding spaces to the right, use a minus symbol immediately after the % and before the number. In below example output of the AWK command is pipped to the cat command to display END OF LINE$ character. [jerry]$ awk 'BEGIN { num = 10; printf "Num = %-5d\n", num }' cat -vte Num = 10 $ Prefix Sign It always prefixes numeric values with a sign, even if the value is positive. [jerry]$ awk 'BEGIN { num1 = -10; num2 = 20; printf "Num1 = %+d\nnum2 = %+d\n", num1, Num1 = -10 Num2 = +20 Hash For %o, it supplies a leading zero. For %x, and %X, it supplies a leading 0x or 0X respectively only if result is nonzero. For %e, %E, %f and %F, the result always contains a decimal point. For %g, and %G, trailing zeros are not removed from the result. Below simple example describes this: [jerry]$ awk 'BEGIN { printf "Octal representation = %#o\nhexadecimal representaion = %#X\n", 10, 10}' Octal representation = 012 Hexadecimal representation = 0XA Loading [MathJax]/jax/output/HTML-CSS/jax.js