Sponsored Content
Top Forums Shell Programming and Scripting How to print 1st field and last 2 fields together and the rest of the fields after it using awk? Post 302825719 by 100bees on Tuesday 25th of June 2013 03:23:59 AM
Old 06-25-2013
How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts,

I need to print the first field first then last two fields should come next and then i need to print rest of the fields.

Input :
Code:
a1,abc,jsd,fhf,fkk,b1,b2
a2,acb,dfg,ghj,b3,c4
a3,djf,wdjg,fkg,dff,ggk,d4,d5

Expected output:
Code:
a1,b1,b2,abc,jsd,fhf,fkk
a2,b3,c4,acb,dfg,ghj
a3,d4,d5,djf,wdjg,fkg,dff,ggk


I got how to print the first and last two fields together ,but not sure how to print the rest of the fields after this

Code:
 
awk -F ',' '{
print $1,$(NF-1),$NF}' infile

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

2. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

3. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

4. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. UNIX for Dummies Questions & Answers

Remove zeros from first field, but print all fields

Hello Everyone, I've got a comma-delimited file that looks like this: 0012,123 ,456 ,05/12/2014 0123,525 ,286 ,05/12/2014 0456,791 ,300 ,05/12/2014 1095,759 ,300 ,05/12/2014 1344,576 ,292 ,05/12/2014 1558,551 ,283 ,05/12/2014 002183719, , ... (9 Replies)
Discussion started by: Scottie1954
9 Replies

7. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

8. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

9. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

10. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies
Flow(3pm)						User Contributed Perl Documentation						 Flow(3pm)

NAME
Data::Flow - Perl extension for simple-minded recipe-controlled build of data. SYNOPSIS
use Data::Flow; $recipes = { path => { default => './MANIFEST'}, contents => { prerequisites => ['path', 'x'] , process => sub { my $data = shift; $data->{ shift() } = `cat $data->{'path'}` x $data->{'x'}; } }, }; $request = new Data::Flow $recipes; $request->set( x => 1); print $request->get('contents'); tie %request, Data::Flow, $recipes; $request{x} = 1; print $request{contents}; DESCRIPTION
The module Data::Flow provides its services via objects. The objects may be obtained by the usual $request = new Data::Flow $recipes; paradigm. The argument $recipes is a hash reference, which provides the rules for request processing. The objects support three methods, set(), get(), aget(), and already_set(). The first one is used to provide input data for processing, the second one to obtain the output. The third one to obtain a reference to an array with results of repeated get(), and the last one to query whether a field is already known. The unit of requested information is a field. The method set() takes a pair "field => value", the methods get() and already_set() take one argument: the "field", and the method aget() takes multiple fields. Every object is created without any fields filled, but it knows how to construct fields basing on other fields or some global into. This knowledge is provided in the argument $recipe of the new() function. This is a reference to a hash, keyed by fields. The values of this hash are hash references themselves, which describe how to acquire the field which is the corresponding key of the initial hash. The internal hashes may have the following keys: "default" describes the default value for the key, if none is provided by set(). The value becomes the value of the field of the object. No additional processing is performed. Example: default => $Config{installdir} "prerequisites" gives the fields which are needed for the construction of the given field. The corresponding value is an array references. The array contains the required fields. If "defaults" did not satisfy the request for a field, but "$recipe->{field}{prerequisites}" exists, the required fields are build before any further processing is done. Example: prerequisites => [ qw(prefix arch) ] "process" contains the rule to build the field. The value is a reference to a subroutine taking 2 arguments: the reference to a hash with all the fields which have been set, and the name of the required field. It is up to the subroutine to actually fill the corresponding field of the hash, an error condition is raised if it did not. Example: process => sub { my $data = shift; $data->{time} = localtime(time) } } "oo_process" contains the rule to build the field. The value is a reference to a subroutine taking 2 arguments: the object $request, and the name of the required field. It is up to the subroutine to actually fill the corresponding field of $request, an error condition is raised if it did not. Example: oo_process => sub { my $data = shift; $data->set( time => localtime(time) ) } "output" the corresponing value has the same meaning as for "process", but the return value of the subroutine is used as the value of the field. Example: output => sub { localtime(time) } "oo_output" the corresponing value has the same meaning as for "process", but the return value of the method is used as the value of the field. Example: output => sub { my $self = shift; $self->get('r') . localtime(time) } "filter" contains the rule to build the field basing on other fields. The value is a reference to an array. The first element of the array is a reference to a subroutine, the rest contains names of the fields. When the subroutine is called, the arguments are the values of fields of the object $request which appear in the array (in the same order). The return value of the subroutine is used as the value of the field. Example: filter => [ sub { shift + shift }, 'first_half', 'second_half' ] Note that the mentioned field will be automatically marked as prerequisites. "self_filter" is similar to "filter", but an extra argument, the object itself, is put in front of the list of arguments. Example: self_filter => [ sub { my ($self, $first_half = (shift, shift); $first_half *= -$self->get('total')*100 if $first_half < 0; # negative means percentage $first_half + shift }, 'first_half', 'second_half' ] "class_filter" is similar to "filter", but the first argument is the name of the method to call, second one is the name of the package to use for the method invocation. The rest contains names of field to provide as method arguments. Example: class_filter => [ 'new', 'FileHandle', 'filename' ] "method_filter" is similar to "class_filter", but the second argument is the name of the field which is used to call the method upon. Example: method_filter => [ 'show', 'widget_name', 'current_display' ] Tied interface The access to the same functionality is available via tied hash interface. AUTHOR
Ilya Zakharevich, cpan@ilyaz.org, with multiple additions from Terrence Monroe Brannon and Radoslav Nedyalkov. SEE ALSO
perl(1), make(1). perl v5.10.0 2008-05-11 Flow(3pm)
All times are GMT -4. The time now is 09:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy