Sponsored Content
Top Forums Shell Programming and Scripting Don't understand how RS functions in awk Post 302449029 by joe228 on Friday 27th of August 2010 11:31:47 PM
Old 08-28-2010
Don't understand how RS functions in awk

I learn using RS in awk to extract portion of file in this forum which is wonderful solution to the problem. However, I don't understand how exactly it operates.

I don't quite understand the mechanism behind how searching for /DATA2/ can result in extracting the whole section under "DATA2"

sample
=====
Code:
DATA1
data11
data12

DATA2
data21
data22

DATA3
data31
data32

Code:
$cat sample | awk 'BEGIN {RS=""} /DATA2/'
DATA2
data21
data22

Since RS is set to be empty string, so each line now should be regarded as a field and so I expected printing $1 and $2 would give me the output of DATA1 and data11 but it didn't. Instead, it returned me with what is shown below:

Code:
$ cat sample | awk 'BEGIN {RS=""} { print $1 }'
DATA1
DATA2
DATA3
$ cat sample | awk 'BEGIN {RS=""} { print $2 }'
data11
data21
data31

So, can someone explain to me why it behaved this way?? Thanks!


Moderator's Comments:
Mod Comment Please use code tags, thank you!

Last edited by Franklin52; 08-28-2010 at 09:14 AM..
 

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

don't understand the unix script

if {"$my_ext_type" = MAIN]; then cd $v_sc_dir Filex.SH $v_so_dir\/$v_fr_file Can somebody tell me what does this suggest. I am pretty new to unix and I am getting confused. What i understood from here is If we have a file extension name as MAIN which we have then we change the directory to... (1 Reply)
Discussion started by: pochaman
1 Replies

2. Homework & Coursework Questions

I don't understand some basics..

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1)find all lines in file ,myf that contain all the words cat dog and mouse in any order and start with the letter... (1 Reply)
Discussion started by: cudders
1 Replies

3. UNIX for Dummies Questions & Answers

Another Simple BASH command I don't understand. Help?

I have a text file called file1 which contains the text: "ls -l" When I enter this command: bash < file1 > file1 file1 gets erased. However if I enter this command: bash < file1 > newfile the output from "ls -l" is stored in newfile. My question is why doesn't file1's text ("ls -l") get... (3 Replies)
Discussion started by: phunkypants
3 Replies

4. Shell Programming and Scripting

Perl syntax that I don't understand.

I'm just trying to confirm that I understand someone's code correctly. If someone has code that says: $foo ||= mysub(); I'm assuming that it means if $foo is nothing or undef, then assign it some value via mysub(). If I'm wrong on this, please let me know. Also, what's the difference... (4 Replies)
Discussion started by: mrwatkin
4 Replies

5. UNIX for Dummies Questions & Answers

trying to compile and don't understand error message

this is my program i am trying to compile /* filedata -- display information about a file */ #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> /* * use octarray for determing * if permission bits set */ static short octarray = {0400, 0200, 0100,... (2 Replies)
Discussion started by: heywoodfloyd
2 Replies

6. UNIX for Dummies Questions & Answers

I don't understand conditions :(

Hi there, I have a very general question. I'm rather new to (bash) shell scripting and I don't understand how conditions work... I've read numerous tutorials but I don't get it. I really don't. Sometime what I do works, sometime it doesn't and that's frustating. So what's the actual difference... (0 Replies)
Discussion started by: hypsis
0 Replies

7. Programming

Understand Virtual functions Internals

I am just trying to understand the virtual fns. concept. I know that if I have a virtual fn. in a base class and its overridden fn. in derived class then based upon the address of base/derived object stored in the base class pointer the fns. will be called. In the below code I had kept... (2 Replies)
Discussion started by: rupeshkp728
2 Replies

8. Shell Programming and Scripting

Don't understand AWK asort behaviour

Hello, I have the following script : BEGIN { print "1 ***"; split("abc",T,""); T="e"; T="z"; T="y"; for (i in T) printf("%i:%s ",i,T); print ""; for (i=1; i<=length(T); i++) printf(T); print "" print "2 ***"; asort(T,U); for (i in U) printf("%i:%s ",i,U); ... (3 Replies)
Discussion started by: jgilot
3 Replies
INSQUE(3P)						     POSIX Programmer's Manual							INSQUE(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
insque, remque - insert or remove an element in a queue SYNOPSIS
#include <search.h> void insque(void *element, void *pred); void remque(void *element); DESCRIPTION
The insque() and remque() functions shall manipulate queues built from doubly-linked lists. The queue can be either circular or linear. An application using insque() or remque() shall ensure it defines a structure in which the first two members of the structure are pointers to the same type of structure, and any further members are application-specific. The first member of the structure is a forward pointer to the next entry in the queue. The second member is a backward pointer to the previous entry in the queue. If the queue is linear, the queue is terminated with null pointers. The names of the structure and of the pointer members are not subject to any special restriction. The insque() function shall insert the element pointed to by element into a queue immediately after the element pointed to by pred. The remque() function shall remove the element pointed to by element from a queue. If the queue is to be used as a linear list, invoking insque(&element, NULL), where element is the initial element of the queue, shall ini- tialize the forward and backward pointers of element to null pointers. If the queue is to be used as a circular list, the application shall ensure it initializes the forward pointer and the backward pointer of the initial element of the queue to the element's own address. RETURN VALUE
The insque() and remque() functions do not return a value. ERRORS
No errors are defined. The following sections are informative. EXAMPLES
Creating a Linear Linked List The following example creates a linear linked list. #include <search.h> ... struct myque element1; struct myque element2; char *data1 = "DATA1"; char *data2 = "DATA2"; ... element1.data = data1; element2.data = data2; insque (&element1, NULL); insque (&element2, &element1); Creating a Circular Linked List The following example creates a circular linked list. #include <search.h> ... struct myque element1; struct myque element2; char *data1 = "DATA1"; char *data2 = "DATA2"; ... element1.data = data1; element2.data = data2; element1.fwd = &element1; element1.bck = &element1; insque (&element2, &element1); Removing an Element The following example removes the element pointed to by element1. #include <search.h> ... struct myque element1; ... remque (&element1); APPLICATION USAGE
The historical implementations of these functions described the arguments as being of type struct qelem * rather than as being of type void * as defined here. In those implementations, struct qelem was commonly defined in <search.h> as: struct qelem { struct qelem *q_forw; struct qelem *q_back; }; Applications using these functions, however, were never able to use this structure directly since it provided no room for the actual data contained in the elements. Most applications defined structures that contained the two pointers as the initial elements and also provided space for, or pointers to, the object's data. Applications that used these functions to update more than one type of table also had the problem of specifying two or more different structures with the same name, if they literally used struct qelem as specified. As described here, the implementations were actually expecting a structure type where the first two members were forward and backward pointers to structures. With C compilers that didn't provide function prototypes, applications used structures as specified in the DESCRIP- TION above and the compiler did what the application expected. If this method had been carried forward with an ISO C standard compiler and the historical function prototype, most applications would have to be modified to cast pointers to the structures actually used to be pointers to struct qelem to avoid compilation warnings. By specifying void * as the argument type, applications do not need to change (unless they specifically referenced struct qelem and depended on it being defined in <search.h>). RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
The Base Definitions volume of IEEE Std 1003.1-2001, <search.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 INSQUE(3P)
All times are GMT -4. The time now is 08:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy