Sponsored Content
Top Forums Shell Programming and Scripting awk sed cut? to rearrange random number of fields into 3 fields Post 302311563 by radoulov on Wednesday 29th of April 2009 04:18:02 AM
Old 04-29-2009
With Perl:

Code:
perl -F, -lane'
print @F > 3
  ? join ",", $F[0], $F[-1], qw(") . ( join ", ", @F[ 1 .. @F - 2 ] ) . qw(")
  : $_
  ' infile

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

help with ksh/awk/sed script, random # of fields

Hello all, I'm working on an attendance callout script for a school district. I need to change our current layout for the vendor. Currently the data is in the form of: studentid,period,building, Heres a sample of some made up records: 500,1,30, 500,2,30, 500,3,30, 500,6,30,... (7 Replies)
Discussion started by: axo959
7 Replies

2. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies

3. Shell Programming and Scripting

awk,cut fields by change field format

Hi Everyone, # cat 1.txt 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 # cat 1.txt | awk -F, '{OFS=",";print $1,$3,$4,$5}' 1321631,19,20091001011859,20091001011907... (7 Replies)
Discussion started by: jimmy_y
7 Replies

4. Shell Programming and Scripting

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 : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

Using awk to rearrange fields

Hi, I am required to arrange columns of a file i.e make the 15th column into the 1st column. I am doing awk 'begin {fs=ofs=","} {print $15,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}' ad.data>ad.csv the problem is that column 15 gets to column 1 but it is not comma separated with the... (10 Replies)
Discussion started by: seddoubt
10 Replies

6. 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

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

Pattern Match and Rearrange the Fields in UNIX

For an Output like below Input : <Subject A="I" B="1039502" C="2015-06-30" D="010101010101"> Output : <Subject D="010101010101" B="1039502" C="2015-06-30" A="I"> I have been using something like below but not getting the desired output : awk -F ' ' '/Subject/ BEGIN{OFS=" ";}... (19 Replies)
Discussion started by: arunkesi
19 Replies

9. Shell Programming and Scripting

Rearrange fields of delimited text file

I want to rearrange the fields of delimited text file after sorting first line (only): input file: a_13;a_2;a_1;a_10 13;2;1;10 the result should be: a_1;a_2;a_10;a_13 1;2;10;13 any help would be appreciated andy (20 Replies)
Discussion started by: andy2000
20 Replies

10. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies
Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommUseraContPerl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements(3pm)

NAME
Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements - Don't use the comma operator as a statement separator. AFFILIATION
This Policy is part of the core Perl::Critic distribution. DESCRIPTION
Perl's comma statement separator has really low precedence, which leads to code that looks like it's using the comma list element separator not actually doing so. Conway suggests that the statement separator not be used in order to prevent this situation. The confusion that the statement separator causes is primarily due to the assignment operators having higher precedence. For example, trying to combine two arrays into another like this won't work: @x = @y, @z; because it is equivalent to @x = @y; @z; Conversely, there are the built-in functions, like "print", that normally force the rest of the statement into list context, but don't when called like a subroutine. This is not likely to produce what is intended: print join q{, }, 2, 3, 5, 7, ": the single-digit primes. "; The obvious fix is to add parentheses. Placing them like print join( q{, }, 2, 3, 5, 7 ), ": the single-digit primes. "; will work, but print ( join q{, }, 2, 3, 5, 7 ), ": the single-digit primes. "; will not, because it is equivalent to print( join q{, }, 2, 3, 5, 7 ); ": the single-digit primes. "; CONFIGURATION
This policy can be configured to allow the last statement in a "map" or "grep" block to be comma separated. This is done via the "allow_last_statement_to_be_comma_separated_in_map_and_grep" option like so: [ValuesAndExpressions::ProhibitCommaSeparatedStatements] allow_last_statement_to_be_comma_separated_in_map_and_grep = 1 With this option off (the default), the following code violates this policy. %hash = map {$_, 1} @list; With this option on, this statement is allowed. Even if this option is off, using a fat comma "=>" works, but that forces stringification on the first value, which may not be what you want. BUGS
Needs to check for "scalar( something, something )". AUTHOR
Elliot Shank "<perl@galumph.com>" COPYRIGHT
Copyright (c) 2007-2011 Elliot Shank. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module. perl v5.14.2 Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements(3pm)
All times are GMT -4. The time now is 08:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy