Here is a copy of Scrutinizer's script with comments added. I hope this will help you understand how it works. Let us know if there is still something you don't understand after
looking at these comments (along with the awk man page on your system).
Code:
awk ' # Use awk to process the following script:
{
if($1 in A) { # If the value in the 1st field has been seen before
if(!c) c=p # if c has not been set, set it to p. (Since p is
# later set to the previous input line number and $1
# will have been seen before and c will not have been
# set only when we see the 2nd occurrence of the 1st
# value in the 1st field, this sets c to the number
# of different values that appear in the 1st field.)
}
else # otherwise (the value in the 1st field has not been
# seen)
h=h $1 OFS # add the 1st field and a field separator to the
# header string.
}
c && !(p%c) { # If c is non-zero and p is an even multiple of c
if(h) print h # if h (the header line) is not an empty string print
# it,
print s # print the string of accumulated $2 values, and
h=s=x # clear the header line and the string of accumulated
# $2 values.
}
{
A[$1] # Record that we have seen this $1 value before,
s=s $2 OFS # add the current $2 value to the string of accumulated
# $2 values, and
p=FNR # set p to the current input line number.
}
END {
print s # When we hit EOF on the input file, print the last
# string of accumulated $2 values.
}
' OFS='\t' file # Mark the end of the awk script text, set the output
# field separator to a tab and name the input file to
# be processed.
Last edited by Don Cragun; 01-13-2017 at 12:36 AM..
This User Gave Thanks to Don Cragun For This Post:
Hi,
I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value .
For Eg .
if the input file has a row :
abc xyz lmn 89 lm nk o p
I would like the script to create something like... (9 Replies)
Hello,
I have an input file like the following:
11_3_4
2_1_35
3_15__
_16989
Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Hi, Please read the whole thread.
I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well.
example:
PACKET DATA PROTOCOL CONTEXT DATA
APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID
10 ... (6 Replies)
Hi Friends,
I have a single column data like below.
1
2
3
4
5
I need the output like below.
0
1
2
3
4
where each row (including first row) subtracting from first row and the result should print below like the way shown in output file.
Thanks
Sid (11 Replies)
Hi ,
Can anyone help me suggesting - how to do the below trick with awk
Input
120
130
140
210
310
410
645
729
800
Output
120 130 140 (6 Replies)
Discussion started by: Indra2011
6 Replies
LEARN ABOUT BSD
join
JOIN(1) General Commands Manual JOIN(1)NAME
join - relational database operator
SYNOPSIS
join [ options ] file1 file2
DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If file1 is `-', the standard
input is used.
File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in
each line.
There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con-
sists of the common field, then the rest of the line from file1, then the rest of the line from file2.
Fields are normally separated by blank, tab or newline. In this case, multiple separators count as one, and leading separators are dis-
carded.
These options are recognized:
-an In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2.
-e s Replace empty output fields by string s.
-jn m Join on the mth field of file n. If n is missing, use the mth field in each file.
-o list
Each output line comprises the fields specified in list, each element of which has the form n.m, where n is a file number and m is a
field number.
-tc Use character c as a separator (tab character). Every appearance of c in a line is significant.
SEE ALSO sort(1), comm(1), awk(1)BUGS
With default field separation, the collating sequence is that of sort -b; with -t, the sequence is that of a plain sort.
The conventions of join, sort, comm, uniq, look and awk(1) are wildly incongruous.
7th Edition April 29, 1985 JOIN(1)