Sponsored Content
Top Forums UNIX for Beginners Questions & Answers awk Associative Array and/or Referring to Field by String (Nonconstant String Value) Post 303029785 by jvoot on Friday 1st of February 2019 12:18:42 AM
Old 02-01-2019
Thanks so much Scrutinizer. It looked like it was printing out some manner of counter (possibly string length?) as the first field of every line. I adjusted your code slightly and also for simplicity sake took out the leading space in the input file. I also needed to transcribe your code to a one-liner as I was passing output into it via pipe (I presented it as a file above for simplicity sake).

Thus, your code transcribed awk -F '[][]' '{for(i=2; i<=NF; i+=2) if($i~/<Ob>/){split($i,F," "); print i,$1 F[1]; next}}'gave me this:
Code:
4  PS028,005 M
8  PS028,005 M

I adjusted to awk -F '[][]' '{for(i=2; i<=NF; i+=2) if($i~/<Ob>/){split($i,F," "); print $1 F[1]; next}}' and while I haven't investigated in detail, that seems to have done the trick. Thanks so much!

--- Post updated at 09:18 PM ---

Quote:
Originally Posted by Don Cragun
In your sample data, the [string <0b>] always appears at the end of the line that starts with <space>s immediately followed by PS. Is that also true in your real data? If it is, we can simplify the code Scrutinizer suggested to something like:

Code:
awk '$1 ~ /^PS/ {sub(/\[/, "", $(NF - 1));print $1, $(NF - 1)}' file

or:
Code:
awk '$1 ~ /^PS/ {print $1, substr($(NF - 1), 2)}' file

Unfortunately no Don, the string with <Ob> can appear anywhere in the line. Nevertheless, I did a bit of an adjustment to Scrutinizer's code and it seems to be working very well. Thank you so much Don.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

2. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

3. Shell Programming and Scripting

awk, associative array, compare files

i have a file like this < '393200103052';'H3G';'20081204' < '393200103059';'TIM';'20110111' < '393200103061';'TIM';'20060206' < '393200103064';'OPI';'20110623' > '393200103052';'HKG';'20081204' > '393200103056';'TIM';'20110111' > '393200103088';'TIM';'20060206' Now i have to generate a file... (9 Replies)
Discussion started by: shruthi123
9 Replies

4. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

5. Shell Programming and Scripting

Help needed on Associative array in awk

Hi All, I got stuck up with shell script where i use awk. The scenario which i am working on is as below. I have a file text.txt with contents COL1 COL2 COL3 COL4 1 A 500 400 1 B 500 400 1 A 500 200 2 A 290 300 2 B 290 280 3 C 100 100 I could able to sum col 3 and col4 based on... (3 Replies)
Discussion started by: imsularif
3 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Split string into map (Associative Array)

Hi Input: { committed = 782958592; init = 805306368; max = 1051394048; used = 63456712; } Result: A map (maybe Associative Array) where I can iterate through the key/value. Something like this: for key in $map do echo key=$key value=$map done Sample output from the map: ... (2 Replies)
Discussion started by: chitech
2 Replies

8. Shell Programming and Scripting

Awk: Dealing with whitespace in associative array indicies

Is there a reliable way to deal with whitespace in array indicies? I am trying to annotate fails in a database using a table of known fails. In a begin block I have code like this: # Read in Known Fail List getline < "'"$failListFile"'"; getline < "'"$failListFile"'"; getline <... (6 Replies)
Discussion started by: Michael Stora
6 Replies

9. UNIX for Beginners Questions & Answers

String has * as the field delimiter and I need echo/awk to escape it, how?

Hi, I am trying to read an Oracle listener log file line by line and need to separate the lines into several fields. The field delimiter for the line happens to be an asterisk. I have the script below to start with but when running it, the echo command is globbing it to include other... (13 Replies)
Discussion started by: newbie_01
13 Replies

10. Shell Programming and Scripting

awk to average field if matching string in another

In the awk below I am trying to get the average of the sum of $7 if the string in $4 matches in the line below it. The --- in the desired out is not needed, it is just to illustrate the calculation. The awk executes and produces the current out. I am not sure why the middle line is skipped and the... (2 Replies)
Discussion started by: cmccabe
2 Replies
accessors(3pm)						User Contributed Perl Documentation					    accessors(3pm)

NAME
accessors - create accessor methods in caller's package. SYNOPSIS
package Foo; use accessors qw( foo bar baz ); my $obj = bless {}, 'Foo'; # generates chaining accessors # that you can set like this: $obj->foo( 'hello ' ) ->bar( 'world' ) ->baz( "! " ); # you get the values by passing no params: print $obj->foo, $obj->bar, $obj->baz; DESCRIPTION
The accessors pragma lets you create simple accessors at compile-time. This saves you from writing them by hand, which tends to result in cut-n-paste errors and a mess of duplicated code. It can also help you reduce the ammount of unwanted direct-variable access that may creep into your codebase when you're feeling lazy. accessors was designed with laziness in mind. Method-chaining accessors are generated by default. Note that you can still use accessors::chained directly for reasons of backwards compatibility. See accessors::classic for accessors that always return the current value if you don't like method chaining. GENERATED METHODS
accessors will generate methods that return the current object on set: sub foo { my $self = shift; if (@_) { $self->{-foo} = shift; return $self; } else { return $self->{-foo}; } } This way they can be chained together. Why prepend the dash? The dash ("-") is prepended to the property name for a few reasons: o interoperability with Error. o to make it difficult to accidentally access the property directly ala: use accessors qw( foo ); $obj->{foo}; # prevents this by mistake $obj->foo; # when you probably meant this (this might sound woolly, but it's easy enough to do). o syntactic sugar (this is woolly :). You shouldn't care too much about how the property is stored anyway - if you do, you're likely trying to do something special (and should really consider writing the accessors out long hand), or it's simply a matter of preference in which case you can use accessors::classic, or sub-class this module. PERFORMANCE
There is little-to-no performace hit when using generated accessors; in fact there is usually a performance gain. o typically 10-30% faster than hard-coded accessors (like the above example). o typically 1-15% slower than optimized accessors (less readable). o typically a small performance hit at startup (accessors are created at compile-time). o uses the same anonymous sub to reduce memory consumption (sometimes by 80%). See the benchmark tests included with this distribution for more details. MOTIVATION
The main difference between the accessors pragma and other accessor generators is simplicity. o interface use accessors qw( ... ) is as easy as it gets. o a pragma it fits in nicely with the base pragma: use base qw( Some::Class ); use accessors qw( foo bar baz ); and accessors get created at compile-time. o no bells and whistles The module is extensible instead. SUB-CLASSING If you prefer a different style of accessor or you need to do something more complicated, there's nothing to stop you from sub-classing. It should be pretty easy. Look through accessors::classic, accessors::ro, and accessors::rw to see how it's done. CAVEATS
Classes using blessed scalarrefs, arrayrefs, etc. are not supported for sake of simplicity. Only hashrefs are supported. THANKS
Thanks to Michael G. Schwern for indirectly inspiring this module, and for his feedback & suggestions. Also to Paul Makepeace and David Wright for showing me faster accessors, to chocolateboy for his contributions, the CPAN Testers for their bug reports, and to James Duncan and people on London.pm for their feedback. AUTHOR
Steve Purkis <spurkis@cpan.org> SEE ALSO
accessors::classic, accessors::chained Similar and related modules: base, fields, Class::Accessor, Class::Struct, Class::Methodmaker, Class::Generate, Class::Class, Class::Tangram, Object::Tiny perl v5.12.4 2011-10-16 accessors(3pm)
All times are GMT -4. The time now is 06:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy