Using awk to rearrange fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to rearrange fields
# 8  
Old 09-27-2013
Quote:
Originally Posted by krishmaths
Another way to reduce some typing
Code:
awk 'BEGIN {FS=OFS=","} {$1=$15FS$1;NF--} 1' ad.data>ad.csv

Note that this is making the unstated assumption that each input line always contains exactly 15 fields. The original code seemed to be trying to end up with 15 fields no matter how many were present in the input.

The awk script here moves the fifteenth (possibly empty) field to the start of output line, but may end up with one or more output fields depending on the number of input fields.
# 9  
Old 09-27-2013
It also assumes that decrementing NF will modify $0. Perhaps most (all?) implementations behave this way, but there are no guarantees.

Regards,
Alister
# 10  
Old 09-27-2013
Quote:
Originally Posted by alister
It also assumes that decrementing NF will modify $0. Perhaps most (all?) implementations behave this way, but there are no guarantees.

Regards,
Alister
I know that decrementing NF does NOT modify $0 in awk on OS X.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 09-28-2013
I agree that decreasing NF is not guaranteed to work with every awk (for example it works with nawk on Solaris, but not with /usr/xpg4/bin/awk) ,
however, it does seem to modify $0 in my version of awk on OSX (bwk version 20070501)

---
IF there are exactly 15 columns, one could try:
Code:
awk -F, '{$1=$15 FS $1; $15=x; sub(/,$/, x)}1' OFS=, file

or
Code:
awk -F, '{f=$15; $15=x; sub(/,$/, x); print f,$0}' OFS=, file

---------edit---------

This could work if there are more than 15 fields:
Code:
awk -F, '{$1=$15 FS $1; sub("(,[^,]*){" NF-14 "}$", x)}1' OFS=, file

if your awk can use interval expressions, so it will not work with mawk and (non-POSIX nawk) and gawk 3.x or lower needs gawk --re-interval

Last edited by Scrutinizer; 09-28-2013 at 05:43 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

Use awk to count and rearrange entries

How can I use awk to count the occurrence of field 2 and rearrange the output like below: Input: OA1 FM AA OA0 FM CC ON0 FM CC FN1 FN BB OY1 FN BB OY2 FN CC OY3 FN CC YT0 FM AA KW1 FN CC KW3 FM BB YT4 FM AA FN2 FT BB OA3 FT AA ON7 FM BB (14 Replies)
Discussion started by: aydj
14 Replies

4. Shell Programming and Scripting

Rearrange Lines with awk

I need to rearrange the lines in the input file in the example below: Input: LG1 R500 A-170 F1:81 F1:22 F2:32 F1:71 LG1 R700 A-203 F2:17 E2:18 LG1 R700 B-224 E1:9 LG2 R500 C-235 E2:9 F2:17 Output: LG1 R500 A-170 F1:81 LG1 R500 A-170 F1:22 LG1 R500 A-170 F2:32 LG1 R500 A-170... (2 Replies)
Discussion started by: aydj
2 Replies

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

6. Shell Programming and Scripting

how to rearrange a matrix with awk

Hi, every one. I have two files ,one is in matrix like this, one is a list with the same data as the matrix. AB AE AC AD AA AF SA 3 4 5 6 4 6 SC 5 7 2 8 4 3 SD 4 6 5 3 8 3 SE 45 ... (5 Replies)
Discussion started by: xshang
5 Replies

7. Shell Programming and Scripting

Using awk to rearrange data according to date

Hi, I have a large data frame as shown below, where data is separated into years. 10 May 2011 Created: 10 May 11 15:05 GMT Scale: SIO-2005 and others GC-MD, Cape Grim, Tasmania, Lat.: 40.68S, Lon.: 144.69E, Alt: 94m above sea level You can use the following format in Fortran to read data... (4 Replies)
Discussion started by: gd9629
4 Replies

8. UNIX for Dummies Questions & Answers

Rearrange columns and rows with awk

Hello, I have the following problem I have two columns with numbers arranged as follows: x1 y1 x2 y2 .... .... x250 y250 Now I need them arranged as follows: "string a" x1 y1 x1 y2 "string b" "string a" x1 y2 x2 y2 (3 Replies)
Discussion started by: Tom46
3 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. Shell Programming and Scripting

Need help in AWK;Search String and rearrange columns

Hi AWK Experts, file1.txt contains: 29b11b820ddcc:-|OHad.perWrk|spn_id=AH111|spn_ordtyp=MY_REQ|msg_typ=ah.ntf.out|spn_ordid=928176|spn_nid=3|msg_strt=1175615334703|msg_que=oput|diff=371|17:48:55,074|17:48:55,084|10 file2.txt contains:... (2 Replies)
Discussion started by: spring_buck
2 Replies
Login or Register to Ask a Question