Supressing and replacing the output of a field in Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Supressing and replacing the output of a field in Awk
# 1  
Old 01-21-2009
Supressing and replacing the output of a field in Awk

Wondering if anybody can help with changing the output of a field. I'm needing to change the output of a field in this file:

User Process ID Time Active Licences Type
ChangeAdmin (Phys-agsdev/19353 212), start Wed 1/21 6:30 (linger: 1800)
u414013 (Phys-agsdev/19353 1491), start Wed 1/21 12:54 (linger: 1800)
z001429 (Phys-agsdev/19353 788), start Wed 1/21 13:03 (linger: 1800)
u414014 (Phys-agsdev/19353 1009), start Wed 1/21 10:34
z311203 (Phys-agsdev/19353 2368), start Wed 1/21 10:52
u414020 (Phys-agsdev/19353 879), start Wed 1/21 11:43
z311260 (Phys-agsdev/19353 1523), start Wed 1/21 12:17
oracled (Phys-agsdev/19353 555), start Wed 1/21 9:00
u414013 (Phys-agsdev/19353 1717), start Wed 1/21 12:56

The field which says "(linger: 1800)" is field 13. What I want to do is, if it contains "(linger: 1800)", I want it to say "ChangeBase", or if it is blank, I want it to say "CMBase". Here's the code I have, with an if statement, but it has no affect whatsoever!

BEGIN {
print
print "User Process ID Time Active Licence Type"
print
}
NF > 11{changebaselicences++}
NF < 12{cmbaselicences++}
{
if ($13 == / /) {print "CMBase"}
if ($13 ~ /linger/) {print "ChangeBase"}
print $4, $6, $7, $8, $9, $10, $11, $12, $13
}
END {
printf "\nUsers of SYNERGY-ChangeBase: (Total of 11 licenses issued; Total of %-2d", changebaselicences
print " licences in use)"
printf "\nUsers of SYNERGY-CMBase: (Total of 12 licenses issued; Total of %-2d", cmbaselicences
print " licences in use)"
print
}


Many thanks in advance.
# 2  
Old 01-21-2009
Not sure if it is what yo want:

Code:
awk 'NR > 1 && /linger/ {sub(/\(linger: 1800\)/,"ChangeBase"); print; next} NR > 1 {print $0,"CMBase"; next} {print}' infile
User Process ID Time Active Licences Type
ChangeAdmin (Phys-agsdev/19353 212), start Wed 1/21 6:30 ChangeBase
u414013 (Phys-agsdev/19353 1491), start Wed 1/21 12:54 ChangeBase
z001429 (Phys-agsdev/19353 788), start Wed 1/21 13:03 ChangeBase
u414014 (Phys-agsdev/19353 1009), start Wed 1/21 10:34 CMBase
z311203 (Phys-agsdev/19353 2368), start Wed 1/21 10:52 CMBase
u414020 (Phys-agsdev/19353 879), start Wed 1/21 11:43 CMBase
z311260 (Phys-agsdev/19353 1523), start Wed 1/21 12:17 CMBase
oracled (Phys-agsdev/19353 555), start Wed 1/21 9:00 CMBase
u414013 (Phys-agsdev/19353 1717), start Wed 1/21 12:56  CMBase

# 3  
Old 01-21-2009
I have difficulty seeing 13 fields in your records. What are you setting FS to?
# 4  
Old 01-21-2009
Quote:
Originally Posted by zaxxon
Not sure if it is what yo want:

Code:
awk 'NR > 1 && /linger/ {sub(/\(linger: 1800\)/,"ChangeBase"); print; next} NR > 1 {print $0,"CMBase"; next} {print}' infile
User Process ID Time Active Licences Type
ChangeAdmin (Phys-agsdev/19353 212), start Wed 1/21 6:30 ChangeBase
u414013 (Phys-agsdev/19353 1491), start Wed 1/21 12:54 ChangeBase
z001429 (Phys-agsdev/19353 788), start Wed 1/21 13:03 ChangeBase
u414014 (Phys-agsdev/19353 1009), start Wed 1/21 10:34 CMBase
z311203 (Phys-agsdev/19353 2368), start Wed 1/21 10:52 CMBase
u414020 (Phys-agsdev/19353 879), start Wed 1/21 11:43 CMBase
z311260 (Phys-agsdev/19353 1523), start Wed 1/21 12:17 CMBase
oracled (Phys-agsdev/19353 555), start Wed 1/21 9:00 CMBase
u414013 (Phys-agsdev/19353 1717), start Wed 1/21 12:56  CMBase

Comes back with an error:

nawk: syntax error at source line 39
context is
NR > 1 && /linger/ >>> { <<< sub(/\(linger: 1800\)/,"ChangeBase"); print; next} NR > 1 {print $0,"CMBase"; next} {print}' infile
nawk: illegal statement at source line 39

I can't see anything wrong with it?
# 5  
Old 01-21-2009
Quote:
Originally Posted by fpmurphy
I have difficulty seeing 13 fields in your records. What are you setting FS to?
Definitely 13 fields (including spaces).
# 6  
Old 01-21-2009
I tried using gsub:

gsub("(linger: 1800)","ChangeBase", $0)
gsub(/ /,"CMBase", $0,$13)

It worked okay for replacing "linger: 1800", but not for when the field is empty. Unfortunately it doesn't seem like you can ask it just to check one field. It has to search the whole file...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to print output based on first field.

Hi Folks, I have one requirement, There is one file, which contains two fields. Based on first field, I need to print an output. Example will be more suitable. Input file like this. abc 5 abc 10 xyz 6 xyz 9 xyz 10 mnp 10 mnp 12 mnp 6 (2 Replies)
Discussion started by: Raza Ali
2 Replies

2. Shell Programming and Scripting

awk to print field from lookup file in output

The below awk uses $3 and $4 in search as the min and max, then takes each $2 value in lookup and compares it. If the value in lookupfalls within the range in searchthen it prints the entire line in lookup/ICODE]. What I can't seem to figure out is how to print the matching $5 from search on that... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk calculation wrong field output

The awk below is close but I can't seem to fix it to produce the desired output. Thank you :). current awk with output awk '{c1++; c2+=($2)} END{for (e in c1) print e, c1, c2}' input EFCAB5 2 50 USH2A 2 19 desired... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. UNIX for Dummies Questions & Answers

awk truncating first field output?

Hello, I'm writing an Awk script to take a command line argument (student's name) and output their relevant student#, name, and marks. For some reason, awk arbitrarily removes the first digit from the student number and doesn't show me the proper output. Here is my code: #! /usr/bin/awk -f... (6 Replies)
Discussion started by: trashmouth12
6 Replies

5. UNIX for Dummies Questions & Answers

awk - output field separator

In awk, how do I print all fields with a specified output field separator? I have tried the following, which does not print the output FS: echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}' (3 Replies)
Discussion started by: locoroco
3 Replies

6. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

7. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

8. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

9. Shell Programming and Scripting

Help supressing spool output from screen when calling sqlplus from script

I'm calling an embedded sql from my shell script file. This sql does simple task of spooling out the contents of the table (see below my sample code) into a spool file that I specify. So far so good, but the problem is that the output is also displayed on screen which I do NOT want. How can I... (3 Replies)
Discussion started by: MxC
3 Replies

10. Shell Programming and Scripting

how to include field in the output filename of awk

Im using awk and I want the output filename to contain the first field of the input file. Ex. 1 dddd wwwww 1 eeeee wwww 1 wwww eerrrr 2 eeee eeeeee I want the output files to be xxx1 and xxx2 Thank you (4 Replies)
Discussion started by: yahyaaa
4 Replies
Login or Register to Ask a Question