Sponsored Content
Top Forums Shell Programming and Scripting Changing exact matches with awk from subfields Post 302592676 by adamreiswig on Tuesday 24th of January 2012 12:53:00 PM
Old 01-24-2012
Changing exact matches with awk from subfields

To give you some context of my issue the following is some sample dummy data. The field delimiter is "<-->". The 4th field is going to be tags for my notes. The tags should always be unique and sorted alphabetically.
Code:
1<-->01/20/12<-->01/20/12<-->1st note<-->1st note<-NL->2 lines
2<-->01/20/12<-->01/20/12<-->2nd note<-->2nd note<-NL->more lines<-NL->here
3<-->01/20/12<-->01/20/12<-->more notes<-->3rd notes, single line
6<-->01/20/12<-->01/20/12<-->what??<-->another notes<-NL->full of <-NL->more<-NL->line

The goal is I want to be able to update a single tag across all my notes. The problem I'm having is I can't find a way to select only whole words. i.e. if I want to change the word "note" to "notez", on the 3rd line, "notes" is changed to "notezs".

How can I change only whole words from my field #4? I've tried awk's sub & index commands, different regular expressions and attempts to combine with sed. The environment I'm working in is ksh88 and I'd prefer to use just awk if possible. Perl sadly isn't an option.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies

2. Shell Programming and Scripting

Using grep returns partial matches, I need to get an exact match or nothing

I’m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the “create” command exits. $cstatus = `${ZADM} list -vic | grep... (3 Replies)
Discussion started by: TKD
3 Replies

3. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

4. Shell Programming and Scripting

AWK: Help inserting records between various matches

Hello, my apologizes if the title is a bit confusing. I am currently working with a series of files that have the form: 2 3 7 17 21 However, I need to insert records such that I have: 0 0 1 0 2 1 3 1 4 0 5 0 6 0 7 1 .... And so on. Currently I have the... (2 Replies)
Discussion started by: Euler2
2 Replies

5. Shell Programming and Scripting

Exact expression matches (can't seem to solve this)

I've seen dozens of similar threads but none seem to match what I'm looking for and I can't seem to make sense of how to do this so any help would be immensely appreciated. I am running a command that generates this output: Mike Smith Mike Smith Alaska Mike Smith Washington Mike Smith Alaska... (6 Replies)
Discussion started by: valgrom
6 Replies

6. Shell Programming and Scripting

How to get the exact word in awk?

Hi, i have a file that contains the following: ARTPRD01_app = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.222.3.4)(PORT = 1540)) (CONNECT_DATA = (SERVICE_NAME = artprd01.com) ARTPRD01 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 11.223.3.1)(PORT =... (2 Replies)
Discussion started by: reignangel2003
2 Replies

7. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

8. UNIX for Dummies Questions & Answers

Joining and sorting with csvs with subfields

hello masters, I am working with csv files that open just fine in excel, but have sub-fields which are comma separated as well. a 3 column csv looks like a,b,"c,d,e" f,g,h How do I make join or sort believe that "c,d,e" is just 1 field? (8 Replies)
Discussion started by: senhia83
8 Replies

9. Shell Programming and Scripting

Lookup subfields from 3 tables and insert

Hello masters, Please help on the following. I have a tab delimited file with subfields space delimited. 1 a b x y hhghd ghgf 2 v t f g gdgdgdg hghg I have 3 lookup table files tab delimited, for fields 2,3 and 4 respectively Lookup2 a 10 b 20 v 30 t 40 Lookup3 (12 Replies)
Discussion started by: ritakadm
12 Replies

10. Shell Programming and Scripting

Grep -w not printing exact matches

Dear All, Here is my input TAACGCACTTGCGGCCCCGGGATAAAAAAAAAAAAAAAAAAAAATGGATT NAGAGGGACGGCCGGGGGCATAAAAAAAAAAAAAAAAAAAAAGGGATTTC NGGGTTTTAAGCAGGAGGTGTCAAAAAAAAAAAAAAAAAAAAAGGGATTT NTGGAACCTGGCGCTAGACCAAAAAAAAAAAAAAAAAAAATGGATTTTTG ATACTTACCTGGCAGGGGAGATACCATGATCAATAAAAAAAAAAAAAAAA... (3 Replies)
Discussion started by: jacobs.smith
3 Replies
MARC::Field(3pm)					User Contributed Perl Documentation					  MARC::Field(3pm)

NAME
MARC::Field - Perl extension for handling MARC fields SYNOPSIS
use MARC::Field; # If your system uses wacky control field tags, add them MARC::Field->allow_controlfield_tags('FMT', 'LLE'); my $field = MARC::Field->new( 245, '1', '0', 'a' => 'Raccoons and ripe corn / ', 'c' => 'Jim Arnosky.' ); $field->add_subfields( "a", "1st ed." ); DESCRIPTION
Defines MARC fields for use in the MARC::Record module. I suppose you could use them on their own, but that wouldn't be very interesting. EXPORT
None by default. Any errors are stored in $MARC::Field::ERROR, which $MARC::Record usually bubbles up to $MARC::Record::ERROR. CLASS VARIABLES
extra_controlfield_tags: Some systems (notably Ex Libris's Aleph) throw extra control fields in their MARC (e.g., Aleph's MARC-XML tends to have a "FMT" control field). We keep a class-level hash to track to track them; it can be manipulated with "allow_controlfield_tags" and c<disallow_controlfield_tags>. METHODS
new() The constructor, which will return a MARC::Field object. Typically you will pass in the tag number, indicator 1, indicator 2, and then a list of any subfield/data pairs. For example: my $field = MARC::Field->new( 245, '1', '0', 'a' => 'Raccoons and ripe corn / ', 'c' => 'Jim Arnosky.' ); Or if you want to add a control field (< 010) that does not have indicators. my $field = MARC::Field->new( '001', ' 14919759' ); tag() Returns the three digit tag for the field. indicator(indno) Returns the specified indicator. Returns "undef" and sets $MARC::Field::ERROR if the indno is not 1 or 2, or if the tag doesn't have indicators. allow_controlfield_tags($tag, $tag2, ...) Add $tags to class-level list of strings to consider valid control fields tags (in addition to 001 through 009). Tags must have three characters. disallow_controlfield_tags($tag, $tag2, ...) =head2 disallow_controlfield_tags('*') Revoke the validity of a control field tag previously added with allow_controlfield_tags. As a special case, if you pass the string '*' it will clear out all previously-added tags. NOTE that this will only deal with stuff added with allow_controlfield_tags; you can't disallow '001'. is_controlfield_tag($tag) -- does the given tag denote a control field? Generally called as a class method (e.g., MARC::Field->is_controlfield_tag('001')) is_control_field() Tells whether this field is one of the control tags from 001-009. subfield(code) When called in a scalar context returns the text from the first subfield matching the subfield code. my $subfield = $field->subfield( 'a' ); Or if you think there might be more than one you can get all of them by calling in a list context: my @subfields = $field->subfield( 'a' ); If no matching subfields are found, "undef" is returned in a scalar context and an empty list in a list context. If the tag is a control field, "undef" is returned and $MARC::Field::ERROR is set. subfields() Returns all the subfields in the field. What's returned is a list of list refs, where the inner list is a subfield code and the subfield data. For example, this might be the subfields from a 245 field: ( [ 'a', 'Perl in a nutshell :' ], [ 'b', 'A desktop quick reference.' ], ) data() Returns the data part of the field, if the tag number is less than 10. add_subfields(code,text[,code,text ...]) Adds subfields to the end of the subfield list. $field->add_subfields( 'c' => '1985' ); Returns the number of subfields added, or "undef" if there was an error. delete_subfield() delete_subfield() allows you to remove subfields from a field: # delete any subfield a in the field $field->delete_subfield(code => 'a'); # delete any subfield a or u in the field $field->delete_subfield(code => ['a', 'u']); If you want to only delete subfields at a particular position you can use the pos parameter: # delete subfield u at the first position $field->delete_subfield(code => 'u', pos => 0); # delete subfield u at first or second position $field->delete_subfield(code => 'u', pos => [0,1]); You can specify a regex to for only deleting subfields that match: # delete any subfield u that matches zombo.com $field->delete_subfield(code => 'u', match => qr/zombo.com/); delete_subfields() Delete all subfields with a given subfield code. This is here for backwards compatability, you should use the more flexible delete_subfield(). update() Allows you to change the values of the field. You can update indicators and subfields like this: $field->update( ind2 => '4', a => 'The ballad of Abe Lincoln'); If you attempt to update a subfield which does not currently exist in the field, then a new subfield will be appended to the field. If you don't like this auto-vivification you must check for the existence of the subfield prior to update. if ( $field->subfield( 'a' ) ) { $field->update( 'a' => 'Cryptonomicon' ); } If you want to update a field that has no indicators or subfields (000-009) just call update() with one argument, the string that you would like to set the field to. $field = $record->field( '003' ); $field->update('IMchF'); Note: when doing subfield updates be aware that "update()" will only update the first occurrence. If you need to do anything more complicated you will probably need to create a new field and use "replace_with()". Returns the number of items modified. replace_with() Allows you to replace an existing field with a new one. You need to pass "replace()" a MARC::Field object to replace the existing field with. For example: $field = $record->field('245'); my $new_field = new MARC::Field('245','0','4','The ballad of Abe Lincoln.'); $field->replace_with($new_field); Doesn't return a meaningful or reliable value. as_string( [$subfields] ) Returns a string of all subfields run together. A space is added to the result between each subfield. The tag number and subfield character are not included. Subfields appear in the output string in the order in which they occur in the field. If $subfields is specified, then only those subfields will be included. my $field = MARC::Field->new( 245, '1', '0', 'a' => 'Abraham Lincoln', 'h' => '[videorecording] :', 'b' => 'preserving the union /', 'c' => 'A&E Home Video.' ); print $field->as_string( 'abh' ); # Only those three subfields # prints 'Abraham Lincoln [videorecording] : preserving the union /'. Note that subfield h comes before subfield b in the output. as_formatted() Returns a pretty string for printing in a MARC dump. as_usmarc() Returns a string for putting into a USMARC file. It's really only useful for "MARC::Record::as_usmarc()". clone() Makes a copy of the field. Note that this is not just the same as saying my $newfield = $field; since that just makes a copy of the reference. To get a new object, you must my $newfield = $field->clone; Returns a MARC::Field record. warnings() Returns the warnings that were created when the record was read. These are things like "Invalid indicators converted to blanks". The warnings are items that you might be interested in, or might not. It depends on how stringently you're checking data. If you're doing some grunt data analysis, you probably don't care. SEE ALSO
See the "SEE ALSO" section for MARC::Record. TODO
See the "TODO" section for MARC::Record. LICENSE
This code may be distributed under the same terms as Perl itself. Please note that these modules are not products of or supported by the employers of the various contributors to the code. AUTHOR
Andy Lester, "<andy@petdance.com>" perl v5.10.1 2010-09-09 MARC::Field(3pm)
All times are GMT -4. The time now is 01:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy