Moving characters instead of fields with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving characters instead of fields with awk
# 1  
Old 03-19-2010
Moving characters instead of fields with awk

Is there a simple way to select and replace characters based on character position in the line rather than as delimited columns?

I have input $FILE.1
Code:
    0.9318    1.4966    0.6361 A   
    0.4709   -0.2878   -0.5030 Bb  
   -0.5738   -1.3981    0.8936 C

And desire output $FILE.2
Code:
A   0.9318   1.4966   0.6361 
Bb  0.4709  -0.2878  -0.5030 
C  -0.5738  -1.3981   0.8936

I've tried
Code:
awk '{ print $4, $1, $2, $3}' $FILE.1 >$FILE.2

which of course gives
Code:
A 0.9318 1.4966 0.6361 
Bb 0.4709 -0.2878 -0.5030 
C -0.5738 -1.3981 0.8936

Which obviously doesn't work because awk is manipulating fields instead of individual characters and separating them with a space. I suppose that I can measure field length and print the correct number of spaces to maintain position (using code similar to this) but if there's a way to manipulate characters based on position in the line I'd rather do that.

Thanks for any help!
# 2  
Old 03-19-2010
Try this:
Code:
sed 's/\(.*\) \([A-Z].*\)/\2\1/'  file

# 3  
Old 03-19-2010
nawk -f al.awk $FILE.1
Code:
function setFieldsByWidth(   i,n,FWS,start,copyd0) {
  # Licensed under GPL Peter S Tillier, 2003
  # NB corrupts $0
  copyd0 = $0                             # make copy of $0 to work on
  if (length(FIELDWIDTHS) == 0) {
    print "You need to set the width of the fields that you require" > "/dev/sterr"
    print "in the variable FIELDWIDTHS (NB: Upper case!)" > "/dev/stderr"
    exit(1)
  }

  if (!match(FIELDWIDTHS,/^[0-9 ]+$/)) {
    print "The variable FIELDWIDTHS must contain digits, separated" > "/dev/stdrr"
    print "by spaces." > "/dev/stderr"
    exit(1)
  }

  n = split(FIELDWIDTHS,FWS)

  if (n == 1) {
    print "Warning: FIELDWIDTHS contains only one field width." > "/dev/stderr"
    print "Attempting to continue." > "/dev/stderr"
  }

  start = 1
  for (i=1; i <= n; i++) {
    $i = substr(copyd0,start,FWS[i])
    start = start + FWS[i]
  }
}

BEGIN {
  #FIELDWIDTHS="7 6 5 4 3 2 1" # for example
  FIELDWIDTHS="10 10 10 4" 
  #OFS=" "
}
!/^[  ]*$/ {
  saveDollarZero = $0 # if you want it later
  setFieldsByWidth()
  # now we can manipulate $0, NF and $1 .. $NF as we wish
  print $4,$1,$2,$3
  next
}

# 4  
Old 03-21-2010
sed 's/\(.*\) \([A-Z].*\)/\2\1/' file
This is perfect!

IIUC, this uses sed to split the line into two fields and then switch them with \2\1.

Which is great because it led me to realize I could do this by column number as well, like this, I think:

Code:
sed 's/\(^.\{31\}\)\(.*$\)/\2\1/'

# 5  
Old 03-22-2010
Code:
sed 's/[ \t]*$//;s/\(.*\) \([^ \t]*\)$/\2\1/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with replacing characters without moving the spaces.

Could you please advise what is the best way to edit a file without disrupting the spaces? For example: I have within my file the value below, wherein I wanted to change VALUE2 to VALUETEST. The total characters on the field of VALUE2 is 15 characters. VALUE1|VALUE2<9 spaces>|VALUE3 but... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

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

3. Shell Programming and Scripting

Removinf newline characters in first 62 fields

Hi All, I receive a | delimited text file containing 63 columns. There is no delimiter at the end of the 63rd field, instead there would be a newline character at the end of the text in 63rd column. I wanted to retain this newline character at the end of the 63rd column, as it is desired newline... (1 Reply)
Discussion started by: sagarparadkar
1 Replies

4. Shell Programming and Scripting

Moving fields

Hello I have a file with this content ============================================================= 1, Het Juvenalis dilemma, Digital fortress, 1998 2, Het Bernini mysterie, Angels & demons, 2000 3, De Delta deceptie, Deception point, 2001 4, De Da Vinci code, The Da... (3 Replies)
Discussion started by: thailand
3 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. 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

7. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

8. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

9. Shell Programming and Scripting

Adding field to file and moving the last 2 fields

I have a file with 32 fields each separated by ‘|”. I need to add a file date exactly in the format “ "20100120" “ as the 32nd field moving the existing 32nd field to 33. so the field I added should be 32nd and the 33rd field is the last field before I added the file date. I know we can... (8 Replies)
Discussion started by: dsravan
8 Replies

10. 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
Login or Register to Ask a Question