Moving fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving fields
# 1  
Old 01-10-2015
Moving fields

Hello
I have a file with this content
=============================================================
Code:
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 Vinci code,    2003
5,  Het verloren symbool,    The lost symbol,    2009

=============================================================
I want to move the third and the fourth field.
The third field must begin at character 30 and the fourth field must begin at character 70
Between the beginning of the record en the first character of every field, I need the same distance.
Maybe someone know a solution
Thanks anyway

Last edited by Scrutinizer; 01-10-2015 at 02:39 AM.. Reason: code tags
# 2  
Old 01-10-2015
may not be exactly what you want, but worth a try

Code:
cat yourfile | column -s "," -t

This User Gave Thanks to senhia83 For This Post:
# 3  
Old 01-10-2015
Quote:
Originally Posted by thailand
Hello
I have a file with this content
=============================================================
Code:
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 Vinci code,    2003
5,  Het verloren symbool,    The lost symbol,    2009

=============================================================
I want to move the third and the fourth field.
The third field must begin at character 30 and the fourth field must begin at character 70
Between the beginning of the record en the first character of every field, I need the same distance.
Maybe someone know a solution
Thanks anyway
Hello thiland,

Could you please try following for same, let me know if this helps.
Code:
awk -F, '{printf"%s,%s,%30s,%70s\n", $1, $2, $3, $4}' OFS=, Input_file

Output will be as follows.
Code:
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 Vinci code,                                                                  2003
5,  Het verloren symbool,               The lost symbol,                                                                  2009

If it doesn't satisfy your requirements kindly show us the input and output you need it will help us to guide/advise you on same.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-10-2015
You could also try something like:
Code:
awk -F ', *' '{printf("%-4s%-25s%-40s%s\n", $1 ",", $2 ",", $3 ",", $4)}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

With your sample input file, it produces the output:
Code:
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 Vinci code,                      2003
5,  Het verloren symbool,     The lost symbol,                        2009

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

7. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

8. Shell Programming and Scripting

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 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 A ... (4 Replies)
Discussion started by: Alchemy3083
4 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