Sponsored Content
Top Forums Shell Programming and Scripting Replace a column with a value conditional on a value in col1 Post 302374784 by durden_tyler on Wednesday 25th of November 2009 12:28:17 PM
Old 11-25-2009
Quote:
Originally Posted by genehunter
...
Code:
BPC0013 ANNUL_49610 0 0 1 2
BPC0014 ANNUL_49642 0 0 2 2
BPC0015 ANNUL_49580 0 0 1 2
BPC0016 ANNUL_49596 0 0 2 2
BPC0017 VULGO_49612 0 0 1 2
BPC0018 ANNUL_49628 0 0 1 2
BPC0019 ANNUL_49692 0 0 2 2
170291_HMG ANNUL_170291 0 0 1 1
170351_HMG ANNUL_170351 0 0 1 1
170475_HMG 170475 0 0 2 1
170497_HMG ANNUL_170497 0 0 2 1
170534_HMG ANNUL_170534 0 0 1 1
170559_HMG ANNUL_170559 0 0 1 1

I want to change the col6 value to 2 only if the line does not start with BPC.
And the lines that do start with BPC have $6 equal to 2 already.

Quote:
... but it is changing everything to 2 at col6.
So isn't this an expected outcome ?

Anyway -

Code:
$
$ cat f2
BPC0013 ANNUL_49610 0 0 1 2
BPC0014 ANNUL_49642 0 0 2 2
BPC0015 ANNUL_49580 0 0 1 2
BPC0016 ANNUL_49596 0 0 2 2
BPC0017 VULGO_49612 0 0 1 2
BPC0018 ANNUL_49628 0 0 1 2
BPC0019 ANNUL_49692 0 0 2 2
170291_HMG ANNUL_170291 0 0 1 1
170351_HMG ANNUL_170351 0 0 1 1
170475_HMG 170475 0 0 2 1
170497_HMG ANNUL_170497 0 0 2 1
170534_HMG ANNUL_170534 0 0 1 1
170559_HMG ANNUL_170559 0 0 1 1
$
$ awk '{if (!/^BPC/){print $1,$2,$3,$4,$5,"2"} else {print}}' f2
BPC0013 ANNUL_49610 0 0 1 2
BPC0014 ANNUL_49642 0 0 2 2
BPC0015 ANNUL_49580 0 0 1 2
BPC0016 ANNUL_49596 0 0 2 2
BPC0017 VULGO_49612 0 0 1 2
BPC0018 ANNUL_49628 0 0 1 2
BPC0019 ANNUL_49692 0 0 2 2
170291_HMG ANNUL_170291 0 0 1 2
170351_HMG ANNUL_170351 0 0 1 2
170475_HMG 170475 0 0 2 2
170497_HMG ANNUL_170497 0 0 2 2
170534_HMG ANNUL_170534 0 0 1 2
170559_HMG ANNUL_170559 0 0 1 2
$
$

tyler_durden
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies

2. Shell Programming and Scripting

awk help to do conditional find and replace

Hi, I have a Line input for awk as follows DROP MATERIALIZED VIEW MCR.COMM_STACK; CREATE MATERIALIZED VIEW "MCR"."COMM_STACK" ON PREBUILT TABLE WITHOUT REDUCED PRECISION USING INDEX REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 7 WITH PRIMARY KEY USING DEFAULT... (3 Replies)
Discussion started by: rajan_san
3 Replies

3. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

4. Shell Programming and Scripting

Sed conditional replace

Given this row: |lastname1|middlename1|firstname1|lastname2|middlename2|firstname2 produce this result: |lastname|middlename|firstname where the resultant names are based on the presence of the #2 names above. I.e., if a #2 name is passed (usually will be null,) use that - otherwise... (8 Replies)
Discussion started by: tiggyboo
8 Replies

5. Programming

Conditional replace after reading in a file

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (7 Replies)
Discussion started by: naveen@
7 Replies

6. Shell Programming and Scripting

Conditional Search/Replace

I'm looking for an awk or (preferably) sed solution to search a pipe delimited file for any occurrence of an email address that does not include a designed domain, and replace the email address with a blank. E.g. hello|smith@designateddomain.com|jones@anotherdomain.edu|1234| turns into: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

7. UNIX for Dummies Questions & Answers

If col1 and col2 of any line in both of two files in two files match, col1 and col2

I have two files, and I'm interested in the first two columns of each. File1 compares set1 to set2 (column1 = set1 name, column2 = set2 name). File2 compares set2 to set1 (column1 = set2 name, column2 =set1 name). I want to print the set names (column values) that appear as pairs in both... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

8. Shell Programming and Scripting

sed conditional \n replace for each line

How could be removed \n only if appearing at position 80 in the line? (4 Replies)
Discussion started by: RomanF
4 Replies

9. Shell Programming and Scripting

Filter on one column and then perform conditional calculations on another column with a Linux script

Hi, I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length. destIP time seqNo packetLength 1.2.3.4 0.01 123 500 1.2.3.5 0.03 44 1500 1.3.2.5 0.08 44 1500 1.2.3.4 0.44... (12 Replies)
Discussion started by: Zooma
12 Replies

10. Shell Programming and Scripting

Conditional Column Value

Hi Folks, I'm trying tog ain further experience with shell programming and have set my a small goal of writing a little filesystem monitoring script. So far my output is as follows: PACMYDB03 Filesystem Size Used Avail Use% Status /usr/local/mysql/data ... (5 Replies)
Discussion started by: Axleuk
5 Replies
Devel::Refcount(3pm)					User Contributed Perl Documentation				      Devel::Refcount(3pm)

NAME
"Devel::Refcount" - obtain the REFCNT value of a referent SYNOPSIS
use Devel::Refcount qw( refcount ); my $anon = []; print "Anon ARRAY $anon has " . refcount($anon) . " reference "; my $otherref = $anon; print "Anon ARRAY $anon now has " . refcount($anon) . " references "; DESCRIPTION
This module provides a single function which obtains the reference count of the object being pointed to by the passed reference value. FUNCTIONS
$count = refcount($ref) Returns the reference count of the object being pointed to by $ref. COMPARISON WITH SvREFCNT This function differs from "Devel::Peek::SvREFCNT" in that SvREFCNT() gives the reference count of the SV object itself that it is passed, whereas refcount() gives the count of the object being pointed to. This allows it to give the count of any referent (i.e. ARRAY, HASH, CODE, GLOB and Regexp types) as well. Consider the following example program: use Devel::Peek qw( SvREFCNT ); use Devel::Refcount qw( refcount ); sub printcount { my $name = shift; printf "%30s has SvREFCNT=%d, refcount=%d ", $name, SvREFCNT($_[0]), refcount($_[0]); } my $var = []; printcount 'Initially, $var', $var; my $othervar = $var; printcount 'Before CODE ref, $var', $var; printcount '$othervar', $othervar; my $code = sub { undef $var }; printcount 'After CODE ref, $var', $var; printcount '$othervar', $othervar; This produces the output Initially, $var has SvREFCNT=1, refcount=1 Before CODE ref, $var has SvREFCNT=1, refcount=2 $othervar has SvREFCNT=1, refcount=2 After CODE ref, $var has SvREFCNT=2, refcount=2 $othervar has SvREFCNT=1, refcount=2 Here, we see that SvREFCNT() counts the number of references to the SV object passed in as the scalar value - the $var or $othervar respectively, whereas refcount() counts the number of reference values that point to the referent object - the anonymous ARRAY in this case. Before the CODE reference is constructed, both $var and $othervar have SvREFCNT() of 1, as they exist only in the current lexical pad. The anonymous ARRAY has a refcount() of 2, because both $var and $othervar store a reference to it. After the CODE reference is constructed, the $var variable now has an SvREFCNT() of 2, because it also appears in the lexical pad for the new anonymous CODE block. PURE-PERL FALLBACK An XS implementation of this function is provided, and is used by default. If the XS library cannot be loaded, a fallback implementation in pure perl using the "B" module is used instead. This will behave identically, but is much slower. Rate pp xs pp 225985/s -- -66% xs 669570/s 196% -- SEE ALSO
o Test::Refcount - assert reference counts on objects AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2011-11-15 Devel::Refcount(3pm)
All times are GMT -4. The time now is 06:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy