Sponsored Content
Top Forums Shell Programming and Scripting Data Splitting into two files from one file Post 302555121 by karumudi7 on Wednesday 14th of September 2011 05:31:12 AM
Old 09-14-2011
MySQL

Quote:
Originally Posted by Corona688
Code:
# Print the last field into file2:   print $NF >"file2"
# Remove the last field:             $NF=""
# Print everything else into file1:  print >"file1"
awk '{ print $NF >"file2" ; $NF="" ; print >"file1" }' < input

Your idea was good! I have to test it.
Will post the result, after I test it!!!

Thanks.

---------- Post updated at 10:58 AM ---------- Previous update was at 10:55 AM ----------

Quote:
Originally Posted by durden_tyler
tyler_durden
Thanks for ur solution! But I don't have Idea on Perl .

---------- Post updated at 02:31 PM ---------- Previous update was at 10:58 AM ----------

Thank U, its worked for me!!!

---------- Post updated at 02:31 PM ---------- Previous update was at 02:31 PM ----------

Thank U, its worked for me!!!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting data file

Hello, I'm trying to split a file by lines. I know that I can use the split command to do this, but the one problem I'm having is, each file created, the first line needs to be a header. I can use the split command the create another file with the header, then append the new split file to... (4 Replies)
Discussion started by: ctcuser
4 Replies

2. Shell Programming and Scripting

Splitting files from one file

Hi, I have an input file like: 111 abcdefgh asdfghjk dfghjkl 222 aaaaaaa bbbbbb 333 djfhfgjktitjhgfkg 444 djdhfjkhfjkghjkfg hsbfjksdbhjkgherjklg fjkhfjklsahjgh fkrjkgnj I want to read this input file and make separate output files with the header as numric value like "111"... (9 Replies)
Discussion started by: saltysumi
9 Replies

3. Shell Programming and Scripting

Splitting file into 2 files ?

Hi extending to one of my previous posted query .... I am using nawk -v invar1="$aa" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_NETWORKID.txt")}' spfile.txt to get 2 different files based on split condition i.e. "|" Similar to invar1 variable in nawk I also need one more variable... (18 Replies)
Discussion started by: shekharjchandra
18 Replies

4. UNIX for Dummies Questions & Answers

Splitting Data in File

I have a file with the below Data 1,nj@ny@pa@caa 2,ct 3,ca@vaa@txI want the output to be 1,nj 1,ny 1,pa 1,caa 2,ct 3,ca 3,vaa 3,tx I need to split the second column based on @ as delimiter The number of delimiters is unknown (4 Replies)
Discussion started by: traininfa
4 Replies

5. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

6. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

7. Shell Programming and Scripting

Splitting a file into 4 files containing the same name pattern

Hello, I have one file which is in size around 20 MB , wanted to split up into four files of each size of 5 MB. ABCD_XYZ_20130302223203.xml. Requirement is that to write script which should do as : first three file should be of size 5 MB each, the fourth one content should be in the last... (8 Replies)
Discussion started by: ajju
8 Replies

8. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

9. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

10. Shell Programming and Scripting

Splitting the XML file into three different files

Hello Shell Guru's I have a requirement to split the source xml file into three different text file. And i need your valuable suggestion to finish this. Here is my source xml snippet, here i am using only one entry of <jms-system-resource>. There may be multiple entries in the source file. ... (5 Replies)
Discussion started by: Siv51427882
5 Replies
Moose::Manual::Roles(3pm)				User Contributed Perl Documentation				 Moose::Manual::Roles(3pm)

NAME
Moose::Manual::Roles - Roles, an alternative to deep hierarchies and base classes VERSION
version 2.0603 WHAT IS A ROLE
? A role encapsulates some piece of behavior or state that can be shared between classes. It is something that classes do. It is important to understand that roles are not classes. You cannot inherit from a role, and a role cannot be instantiated. We sometimes say that roles are consumed, either by classes or other roles. Instead, a role is composed into a class. In practical terms, this means that all of the methods, method modifiers, and attributes defined in a role are added directly to (we sometimes say "flattened into") the class that consumes the role. These attributes and methods then appear as if they were defined in the class itself. A subclass of the consuming class will inherit all of these methods and attributes. Moose roles are similar to mixins or interfaces in other languages. Besides defining their own methods and attributes, roles can also require that the consuming class define certain methods of its own. You could have a role that consisted only of a list of required methods, in which case the role would be very much like a Java interface. Note that attribute accessors also count as methods for the purposes of satisfying the requirements of a role. A SIMPLE ROLE
Creating a role looks a lot like creating a Moose class: package Breakable; use Moose::Role; has 'is_broken' => ( is => 'rw', isa => 'Bool', ); sub break { my $self = shift; print "I broke "; $self->is_broken(1); } Except for our use of Moose::Role, this looks just like a class definition with Moose. However, this is not a class, and it cannot be instantiated. Instead, its attributes and methods will be composed into classes which use the role: package Car; use Moose; with 'Breakable'; has 'engine' => ( is => 'ro', isa => 'Engine', ); The "with" function composes roles into a class. Once that is done, the "Car" class has an "is_broken" attribute and a "break" method. The "Car" class also "does('Breakable')": my $car = Car->new( engine => Engine->new ); print $car->is_broken ? 'Busted' : 'Still working'; $car->break; print $car->is_broken ? 'Busted' : 'Still working'; $car->does('Breakable'); # true This prints: Still working I broke Busted We could use this same role in a "Bone" class: package Bone; use Moose; with 'Breakable'; has 'marrow' => ( is => 'ro', isa => 'Marrow', ); See also Moose::Cookbook::Roles::Comparable_CodeReuse for an example. REQUIRED METHODS
As mentioned previously, a role can require that consuming classes provide one or more methods. Using our "Breakable" example, let's make it require that consuming classes implement their own "break" methods: package Breakable; use Moose::Role; requires 'break'; has 'is_broken' => ( is => 'rw', isa => 'Bool', ); after 'break' => sub { my $self = shift; $self->is_broken(1); }; If we try to consume this role in a class that does not have a "break" method, we will get an exception. You can see that we added a method modifier on "break". We want classes that consume this role to implement their own logic for breaking, but we make sure that the "is_broken" attribute is always set to true when "break" is called. package Car use Moose; with 'Breakable'; has 'engine' => ( is => 'ro', isa => 'Engine', ); sub break { my $self = shift; if ( $self->is_moving ) { $self->stop; } } Roles Versus Abstract Base Classes If you are familiar with the concept of abstract base classes in other languages, you may be tempted to use roles in the same way. You can define an "interface-only" role, one that contains just a list of required methods. However, any class which consumes this role must implement all of the required methods, either directly or through inheritance from a parent. You cannot delay the method requirement check so that they can be implemented by future subclasses. Because the role defines the required methods directly, adding a base class to the mix would not achieve anything. We recommend that you simply consume the interface role in each class which implements that interface. Required Attributes As mentioned before, a role's required method may also be satisfied by an attribute accessor. However, the call to "has" which defines an attribute happens at runtime. This means that you must define the attribute before consuming the role, or else the role will not see the generated accessor. package Breakable; use Moose::Role; requires 'stress'; package Car; use Moose; has 'stress' => ( is => 'rw', isa => 'Int', ); with 'Breakable'; USING METHOD MODIFIERS
Method modifiers and roles are a very powerful combination. Often, a role will combine method modifiers and required methods. We already saw one example with our "Breakable" example. Method modifiers increase the complexity of roles, because they make the role application order relevant. If a class uses multiple roles, each of which modify the same method, those modifiers will be applied in the same order as the roles are used: package MovieCar; use Moose; extends 'Car'; with 'Breakable', 'ExplodesOnBreakage'; Assuming that the new "ExplodesOnBreakage" role also has an "after" modifier on "break", the "after" modifiers will run one after the other. The modifier from "Breakable" will run first, then the one from "ExplodesOnBreakage". METHOD CONFLICTS
If a class composes multiple roles, and those roles have methods of the same name, we will have a conflict. In that case, the composing class is required to provide its own method of the same name. package Breakdancer; use Moose::Role sub break { } If we compose both "Breakable" and "Breakdancer" in a class, we must provide our own "break" method: package FragileDancer; use Moose; with 'Breakable', 'Breakdancer'; sub break { ... } A role can be a collection of other roles: package Break::Bundle; use Moose::Role; with ('Breakable', 'Breakdancer'); METHOD EXCLUSION AND ALIASING
If we want our "FragileDancer" class to be able to call the methods from both its roles, we can alias the methods: package FragileDancer; use Moose; with 'Breakable' => { -alias => { break => 'break_bone' } }, 'Breakdancer' => { -alias => { break => 'break_dance' } }; However, aliasing a method simply makes a copy of the method with the new name. We also need to exclude the original name: with 'Breakable' => { -alias => { break => 'break_bone' }, -excludes => 'break', }, 'Breakdancer' => { -alias => { break => 'break_dance' }, -excludes => 'break', }; The excludes parameter prevents the "break" method from being composed into the "FragileDancer" class, so we don't have a conflict. This means that "FragileDancer" does not need to implement its own "break" method. This is useful, but it's worth noting that this breaks the contract implicit in consuming a role. Our "FragileDancer" class does both the "Breakable" and "BreakDancer", but does not provide a "break" method. If some API expects an object that does one of those roles, it probably expects it to implement that method. In some use cases we might alias and exclude methods from roles, but then provide a method of the same name in the class itself. Also see Moose::Cookbook::Roles::Restartable_AdvancedComposition for an example. ROLE EXCLUSION
A role can say that it cannot be combined with some other role. This should be used with great caution, since it limits the re-usability of the role. package Breakable; use Moose::Role; excludes 'BreakDancer'; ADDING A ROLE TO AN OBJECT INSTANCE
You may want to add a role to an object instance, rather than to a class. For example, you may want to add debug tracing to one instance of an object while debugging a particular bug. Another use case might be to dynamically change objects based on a user's configuration, as a plugin system. The best way to do this is to use the "apply_all_roles()" function from Moose::Util: use Moose::Util qw( apply_all_roles ); my $car = Car->new; apply_all_roles( $car, 'Breakable' ); This function can apply more than one role at a time, and will do so using the normal Moose role combination system. We recommend using this function to apply roles to an object. This is what Moose uses internally when you call "with". 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.14.2 2012-06-28 Moose::Manual::Roles(3pm)
All times are GMT -4. The time now is 03:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy