Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK? Post 302241169 by rickdini on Sunday 28th of September 2008 03:32:43 PM
Old 09-28-2008
Oh yea, and I have to make sure I don't run this command on the Column Headers....
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

2. Shell Programming and Scripting

Using sed on 1st column of tab delimited file

Hi all, I'm new to Unix and work primarily in bioinformatics. I am in need of a script which will allow me to replace "1" with "chr1" in only the first column of a file which looks like such: 1 10327 rs112750067 T C . PASS ASP;RSPOS=10327;... (4 Replies)
Discussion started by: Hkins552
4 Replies

3. UNIX for Dummies Questions & Answers

Using awk to log transform a column in a tab-delimited text file?

How do I use awk to log transform the fifth column of a tab-delimited text file? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Extract second column tab delimited file

I have a file which looks like this: 73450 articles and news developmental psychology 2006-03-30 16:22:40 1 http://www.usnews.com 73450 articles and news developmental psychology 2006-03-30 16:22:40 2 http://www.apa.org 73450 articles and news developmental psychology 2006-03-30... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

5. UNIX for Dummies Questions & Answers

add (append) a column in a tab delimited file

I have a file having the following entries: test1 test2 test3 11 22 33 22 44 66 99 99 44 --- I want to add a column so that the above file becomes: test1 test2 test3 notest 11 22 33 * 22 44 66 * 99 99 44 * --- Thanks (6 Replies)
Discussion started by: mary271
6 Replies

6. Shell Programming and Scripting

Delete string between 3rd tab and first pattern with SED

Hello, I have this sentence :Pattern1 Pattern2 Pattern3 Pattern4-which-contains-HELLO-string-and-other-stuff-and-second-HELLO-and-third-HELLO I want to delete everything between the 3rd tab (\t) and the FIRST pattern "HELLO" of the line. Result expected is : Pattern1 ... (7 Replies)
Discussion started by: theclem35
7 Replies

7. Shell Programming and Scripting

Delete an entire column from a tab delimited file

Hi, Can anyone please tell me about how we can delete an entire column from a tab delimited file? Mu input_file.txt looks like this: And I want the output as: I used the below code nawk -v d="1" 'BEGIN{FS=OFS="\t"}{$d=""}{print}' input_file.txtBut in the output, the first column is... (5 Replies)
Discussion started by: sampoorna
5 Replies

8. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

9. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

10. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies
Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion(3)	User Contributed Perl Documentation   Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion(3)

NAME
Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion - Demonstrates subtypes and coercion use HTTP-related classes (Request, Protocol, etc.) VERSION
version 2.0604 SYNOPSIS
package Request; use Moose; use Moose::Util::TypeConstraints; use HTTP::Headers (); use Params::Coerce (); use URI (); subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers'); coerce 'My::Types::HTTP::Headers' => from 'ArrayRef' => via { HTTP::Headers->new( @{$_} ) } => from 'HashRef' => via { HTTP::Headers->new( %{$_} ) }; subtype 'My::Types::URI' => as class_type('URI'); coerce 'My::Types::URI' => from 'Object' => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ); } => from 'Str' => via { URI->new( $_, 'http' ) }; subtype 'Protocol' => as 'Str' => where { /^HTTP/[0-9].[0-9]$/ }; has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); has 'uri' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); has 'method' => ( is => 'rw', isa => 'Str' ); has 'protocol' => ( is => 'rw', isa => 'Protocol' ); has 'headers' => ( is => 'rw', isa => 'My::Types::HTTP::Headers', coerce => 1, default => sub { HTTP::Headers->new } ); DESCRIPTION
This recipe introduces type coercions, which are defined with the "coerce" sugar function. Coercions are attached to existing type constraints, and define a (one-way) transformation from one type to another. This is very powerful, but it can also have unexpected consequences, so you have to explicitly ask for an attribute to be coerced. To do this, you must set the "coerce" attribute option to a true value. First, we create the subtype to which we will coerce the other types: subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers'); We are creating a subtype rather than using "HTTP::Headers" as a type directly. The reason we do this is that coercions are global, and a coercion defined for "HTTP::Headers" in our "Request" class would then be defined for all Moose-using classes in the current Perl interpreter. It's a best practice to avoid this sort of namespace pollution. The "class_type" sugar function is simply a shortcut for this: subtype 'HTTP::Headers' => as 'Object' => where { $_->isa('HTTP::Headers') }; Internally, Moose creates a type constraint for each Moose-using class, but for non-Moose classes, the type must be declared explicitly. We could go ahead and use this new type directly: has 'headers' => ( is => 'rw', isa => 'My::Types::HTTP::Headers', default => sub { HTTP::Headers->new } ); This creates a simple attribute which defaults to an empty instance of HTTP::Headers. The constructor for HTTP::Headers accepts a list of key-value pairs representing the HTTP header fields. In Perl, such a list could be stored in an ARRAY or HASH reference. We want our "headers" attribute to accept those data structures instead of an HTTP::Headers instance, and just do the right thing. This is exactly what coercion is for: coerce 'My::Types::HTTP::Headers' => from 'ArrayRef' => via { HTTP::Headers->new( @{$_} ) } => from 'HashRef' => via { HTTP::Headers->new( %{$_} ) }; The first argument to "coerce" is the type to which we are coercing. Then we give it a set of "from"/"via" clauses. The "from" function takes some other type name and "via" takes a subroutine reference which actually does the coercion. However, defining the coercion doesn't do anything until we tell Moose we want a particular attribute to be coerced: has 'headers' => ( is => 'rw', isa => 'My::Types::HTTP::Headers', coerce => 1, default => sub { HTTP::Headers->new } ); Now, if we use an "ArrayRef" or "HashRef" to populate "headers", it will be coerced into a new HTTP::Headers instance. With the coercion in place, the following lines of code are all equivalent: $foo->headers( HTTP::Headers->new( bar => 1, baz => 2 ) ); $foo->headers( [ 'bar', 1, 'baz', 2 ] ); $foo->headers( { bar => 1, baz => 2 } ); As you can see, careful use of coercions can produce a very open interface for your class, while still retaining the "safety" of your type constraint checks. (1) Our next coercion shows how we can leverage existing CPAN modules to help implement coercions. In this case we use Params::Coerce. Once again, we need to declare a class type for our non-Moose URI class: subtype 'My::Types::URI' => as class_type('URI'); Then we define the coercion: coerce 'My::Types::URI' => from 'Object' => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ); } => from 'Str' => via { URI->new( $_, 'http' ) }; The first coercion takes any object and makes it a "URI" object. The coercion system isn't that smart, and does not check if the object is already a URI, so we check for that ourselves. If it's not a URI already, we let Params::Coerce do its magic, and we just use its return value. If Params::Coerce didn't return a URI object (for whatever reason), Moose would throw a type constraint error. The other coercion takes a string and converts it to a URI. In this case, we are using the coercion to apply a default behavior, where a string is assumed to be an "http" URI. Finally, we need to make sure our attributes enable coercion. has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); has 'uri' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 ); Re-using the coercion lets us enforce a consistent API across multiple attributes. CONCLUSION
This recipe showed the use of coercions to create a more flexible and DWIM-y API. Like any powerful feature, we recommend some caution. Sometimes it's better to reject a value than just guess at how to DWIM. We also showed the use of the "class_type" sugar function as a shortcut for defining a new subtype of "Object". FOOTNOTES
(1) This particular example could be safer. Really we only want to coerce an array with an even number of elements. We could create a new "EvenElementArrayRef" type, and then coerce from that type, as opposed to a plain "ArrayRef" AUTHOR
Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in Moose and "CONTRIBUTORS" in Moose for details. COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.16.2 2012-09-19 Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion(3)
All times are GMT -4. The time now is 11:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy