Sponsored Content
Top Forums Shell Programming and Scripting Substituting field contents using AWK Post 302366723 by Glyn_Mo on Friday 30th of October 2009 07:37:28 AM
Old 10-30-2009
Substituting field contents using AWK

Hello, wondering if anybody may be help me. This is the output of a file, from which I need to display a number of fields regarding which users are using licences for two applications we run. Lines which end with "(linger: 1800)" denote licence use for one application and lines which don't contain this denote licence use of the other.

Code:
     1      ccm_root phys-agsdev /dev/tty z001368 (v1.0) (Phys-agsdev/19353 481), start Fri 10/30 10:36 (linger: 1800)
     2      ccm_root phys-agsdev /dev/tty z600620 (v1.0) (Phys-agsdev/19353 2478), start Fri 10/30 10:36 (linger: 1800)
     3      ccm_root phys-agsdev /dev/tty z310084 (v1.0) (Phys-agsdev/19353 360), start Fri 10/30 10:37 (linger: 1800)
     4      ccm_root phys-agsdev /dev/tty z310338 (v1.0) (Phys-agsdev/19353 1074), start Fri 10/30 10:38 (linger: 1800)
     5      ccm_root phys-agsdev /dev/tty z001383 (v1.0) (Phys-agsdev/19353 1402), start Fri 10/30 10:42 (linger: 1800)
     6      ccm_root phys-agsdev /dev/tty ChangeAdmin (v1.0) (Phys-agsdev/19353 2155), start Fri 10/30 5:30 (linger: 1800)
     7      ccm_root phys-agsdev /dev/tty u112705 (v1.0) (Phys-agsdev/19353 1613), start Fri 10/30 10:44 (linger: 1800)
     8      ccm_root phys-agsdev /dev/tty z600969 (v1.0) (Phys-agsdev/19353 1885), start Fri 10/30 9:21
     9      ccm_root phys-agsdev /dev/tty z602084 (v1.0) (Phys-agsdev/19353 2379), start Fri 10/30 9:31
    10      ccm_root phys-agsdev /dev/tty z310044 (v1.0) (Phys-agsdev/19353 1279), start Fri 10/30 9:57
    11      ccm_root phys-agsdev /dev/tty z602138 (v1.0) (Phys-agsdev/19353 903), start Fri 10/30 10:02
    12      ccm_root phys-agsdev /dev/tty z310084 (v1.0) (Phys-agsdev/19353 720), start Fri 10/30 10:40
    13      ccm_root phys-agsdev /dev/pts/29 u414014 (v1.0) (Phys-agsdev/19353 580), start Fri 10/30 7:58
    14      ccm_root phys-agsdev /dev/tty u112705 (v1.0) (Phys-agsdev/19353 1991), start Fri 10/30 10:46

I need to be display fields $4, $8, $9, $11 and $12, which I have been able to do. The problem I have though, is that in my output, I need to be able to substitute "(linger: 1800)" with "ChangeBase" and for lines which don't contain "(linger: 1800)" I want to display "CMBase".

In addition to that, I need to read field $4 (user ID) of each line and perform a ypmatch command against it, so I can display the name of the user at the end of each line.

If anybody has any suggestions or pointers, it would be much appreciated.

Many thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

2. Shell Programming and Scripting

Join file contents via common field

I have 2 files with a common parm - Jobname File 1 0507 1202 JOBA 0507 1302 JOBB 0507 1452 JOBC 0507 1552 JOBA 0507 1553 JOBA File2 JOBA abcdefg server4 JOBB defghij server22 JOBC vwxyz12 server55 I would like to take each line from File1 and match the jobname with the jobname... (8 Replies)
Discussion started by: Northerner
8 Replies

3. Shell Programming and Scripting

Substituting variable value in AWK /start/,/stop/

Hi all u brilient people on the forum... I am trying to call the variable value in awk command for search pattern /start/,/stop/ but i am nt able to do this .... wat i did is ..i have created two variable YESTERDAY and TODAY and passed the y'day n 2'days dates in it...like this ... (14 Replies)
Discussion started by: whomi
14 Replies

4. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

5. UNIX for Dummies Questions & Answers

substituting variable value in AWK

Hi All, my requirement is as below. I need to replace a value in a particular column with a substitution variable(date value) and modified value of the current column value in the same position. for ex. i have a record like 02;aaaa;bbbbb;cccccc;dddddd;123456789;hhhhh;12hs;asdf ;... (3 Replies)
Discussion started by: ganesh_248
3 Replies

6. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

7. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

8. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

9. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

10. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies
JOIN(1) 						    BSD General Commands Manual 						   JOIN(1)

NAME
join -- relational database operator SYNOPSIS
join [-a file_number | -v file_number] [-e string] [-o list] [-t char] [-1 field] [-2 field] file1 file2 DESCRIPTION
The join utility performs an ``equality join'' on the specified files and writes the result to the standard output. The ``join field'' is the field in each file by which the files are compared. The first field in each line is used by default. There is one line in the output for each pair of lines in file1 and file2 which have identical join fields. Each output line consists of the join field, the remaining fields from file1 and then the remaining fields from file2. The default field separators are tab and space characters. In this case, multiple tabs and spaces count as a single field separator, and leading tabs and spaces are ignored. The default output field separator is a single space character. Many of the options use file and field numbers. Both file numbers and field numbers are 1 based, i.e., the first file on the command line is file number 1 and the first field is field number 1. The following options are available: -a file_number In addition to the default output, produce a line for each unpairable line in file file_number. -e string Replace empty output fields with string. -o list The -o option specifies the fields that will be output from each file for each line with matching join fields. Each element of list has either the form file_number.field, where file_number is a file number and field is a field number, or the form '0' (zero), repre- senting the join field. The elements of list must be either comma (',') or whitespace separated. (The latter requires quoting to protect it from the shell, or, a simpler approach is to use multiple -o options.) -t char Use character char as a field delimiter for both input and output. Every occurrence of char in a line is significant. -v file_number Do not display the default output, but display a line for each unpairable line in file file_number. The options -v 1 and -v 2 may be specified at the same time. -1 field Join on the field'th field of file1. -2 field Join on the field'th field of file2. When the default field delimiter characters are used, the files to be joined should be ordered in the collating sequence of sort(1), using the -b option, on the fields on which they are to be joined, otherwise join may not report all field matches. When the field delimiter char- acters are specified by the -t option, the collating sequence should be the same as sort(1) without the -b option. If one of the arguments file1 or file2 is '-', the standard input is used. EXIT STATUS
The join utility exits 0 on success, and >0 if an error occurs. COMPATIBILITY
For compatibility with historic versions of join, the following options are available: -a In addition to the default output, produce a line for each unpairable line in both file1 and file2. -j1 field Join on the field'th field of file1. -j2 field Join on the field'th field of file2. -j field Join on the field'th field of both file1 and file2. -o list ... Historical implementations of join permitted multiple arguments to the -o option. These arguments were of the form file_number.field_number as described for the current -o option. This has obvious difficulties in the presence of files named 1.2. These options are available only so historic shell scripts do not require modification and should not be used. SEE ALSO
awk(1), comm(1), paste(1), sort(1), uniq(1) STANDARDS
The join command conforms to IEEE Std 1003.1-2001 (``POSIX.1''). BSD
July 5, 2004 BSD
All times are GMT -4. The time now is 12:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy