Regarding change in column numbers after some commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regarding change in column numbers after some commands
# 1  
Old 11-08-2013
Regarding change in column numbers after some commands

Hi All,

I was using some commands to:
  1. replace a column by a constant string character
    Code:
     awk -v a=CA 'NF>1{ $3=a; print; } ' $line>$line"_1"

  2. to copy a column and paste it in another place
    Code:
     awk '$5=$2" "$5' $line>$line"_2"

  3. to delete the extra columns
    Code:
    awk '{for(i=1;i<=NF;i++) line=(i==1)?$i:((i==6)?line:line OFS $i);print line}' $line>$line"_3"

But by performing these sequence of functions, the columns of file format is changed which is not required.

Since, I need to use these files as input for running another code, and it is giving me the error just because the column numbers is changed.
Can anyone help me in doing these functions without changing the columns format.
# 2  
Old 11-08-2013
If you show input and expected output it would be better to help you.
# 3  
Old 11-08-2013
Initially, The file was like:
Code:
ATOM      1  N   LYS     1       3.440  10.397  -1.989  1.00 18.23
ATOM      2  CA  LYS     1       3.897  11.093  -3.203  1.00 17.09
ATOM      3  C   LYS     1       4.920  10.272  -3.962  1.00 16.14
ATOM      4  O   LYS     1       5.472   9.269  -3.488  1.00 15.98

after applying
Code:
awk -v a=CA 'NF>1{ $3=a; print; } ' $line>$line"_1"

for replacing N, C and O with CA the file was like:
Code:
ATOM 1 CA LYS 1 3.440 10.397 -1.989 1.00 18.23
ATOM 2 CA LYS 1 3.897 11.093 -3.203 1.00 17.09
ATOM 3 CA LYS 1 4.920 10.272 -3.962 1.00 16.14
ATOM 4 CA LYS 1 5.472 9.269 -3.488 1.00 15.98

Then, after applying
Code:
awk '$5=$2" "$5' $line>$line"_2"

for copy 2nd column and paste as a 5th column,we get:
Code:
ATOM 1 CA LYS 1 1 3.440 10.397 -1.989 1.00 18.23
ATOM 2 CA LYS 2 1 3.897 11.093 -3.203 1.00 17.09
ATOM 3 CA LYS 3 1 4.920 10.272 -3.962 1.00 16.14
ATOM 4 CA LYS 4 1 5.472 9.269 -3.488 1.00 15.98

Further, I applied
Code:
awk '{for(i=1;i<=NF;i++) line=(i==1)?$i:((i==6)?line:line OFS $i);print line}' $line>$line"_3"

for deleting the extra 6th column, and the output is like:
Code:
ATOM 1 CA LYS 1 3.440 10.397 -1.989 1.00 18.23
ATOM 2 CA LYS 2 3.897 11.093 -3.203 1.00 17.09
ATOM 3 CA LYS 3 4.920 10.272 -3.962 1.00 16.14
ATOM 4 CA LYS 4 5.472 9.269 -3.488 1.00 15.98

Actually, after all this modifications in my file, I need it to be like:
Code:
ATOM      1  CA   LYS     1       3.440  10.397  -1.989  1.00 18.23
ATOM      2  CA   LYS     2       3.897  11.093  -3.203  1.00 17.09
ATOM      3  CA   LYS     3       4.920  10.272  -3.962  1.00 16.14
ATOM      4  CA   LYS     4       5.472   9.269  -3.488  1.00 15.98

Please have a look at the column numbers and number of spaces between columns. After posting this reply, the number of spaces between the columns are showing same, but this is not the scene the blank space between ATOM and 1 is of 6 blank space in original file whereas it is only 1 after modifications.

Last edited by Don Cragun; 11-08-2013 at 07:43 AM.. Reason: Fix CODE tag.
# 4  
Old 11-08-2013
That's because awk treats any number of spaces as field separator (unless told otherwise). As you can see, the formatting has already gone in the first action's output. If you need exactly formatted output, use printf for all the fields.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-08-2013
Hi RudiC,

Thanks for your reply.
I actually dont know the use of printf in shell script. can you plz giv e me a format by which I can convert my awk commands in printf form
# 6  
Old 11-08-2013
man printf

In your case, start with printf "%-9s%-3s\n", $1, $2 and then expand.
# 7  
Old 11-08-2013
I tried this one:
Code:
gawk '{print $1,"     ", $2," ",$3," ",$4,"  ",$5,"     ",$6,"",$7,"",$8,"",$9,"",$10}' $line>new

After this I am getting:

Code:
ATOM       1   CA   LYS    1       4.816  8.341  0.644  1.00  0.18
ATOM       2   CA   LYS    2       4.790  8.614  -0.804  1.00  0.00
ATOM       3   CA   LYS    3       4.989  10.100  -1.068  1.00  0.83
ATOM       4   CA   LYS    4       3.902  10.937  -0.409  1.00  1.35
ATOM       5   CA   LYS    5       4.120  12.421  -0.673  1.00  1.50
ATOM       6   CA   LYS    6       3.037  13.264  -0.007  1.00  1.72
ATOM       7   CA   LYS    7       3.250  14.696  -0.268  1.00  1.92
ATOM       8   CA   LYS    8       5.891  7.843  -1.514  1.00  0.05
ATOM       9   CA   LYS    9       6.558  6.986  -0.925  1.00  0.17
ATOM       10   CA   SER    10       6.078  8.168  -2.782  1.00  0.03
ATOM       11   CA   SER    11       7.151  7.551  -3.567  1.00  0.00
ATOM       12   CA   SER    12       6.995  7.953  -5.030  1.00  0.15
ATOM       13   CA   SER    13       7.629  9.216  -5.194  1.00  0.08
ATOM       14   CA   SER    14       8.489  8.067  -3.059  1.00  0.07

But now, I need to right align the columns. For which I again tried printf command, which is not working for entire file columns and for multiple files in script.
Can you plz help me in finding a way to right justify the columns for multiple files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Grep commands for numbers w/decimal points

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Is there a grep commands for numbers w/decimal points Display lines for students with GPA above 3.69 but less... (3 Replies)
Discussion started by: jetoutant
3 Replies

2. Shell Programming and Scripting

Change numbers

Hallo This is the content of the file 3 4 5 6 7 8 9 10 11 12 And I want the following output 1 2 3 4 5 6 7 (4 Replies)
Discussion started by: thailand
4 Replies

3. Shell Programming and Scripting

Change the numbers

Hi friends, i need a command which can be used to change the values in file. I have file which contain some values. Data_Center_costing=XXXX Financial_loss=XXXX operational_cost=XXX I am not aware about the values of XXXX, They may be 4 digit or less/more than that. but i need these... (12 Replies)
Discussion started by: Nakul_sh
12 Replies

4. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

5. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

6. Shell Programming and Scripting

Change numbers in a file, incrementing them

Hello, Here's a file of mine: key1:431 key2:159 key3:998 I need to change these keys to something bigger - and I actually need to shift them all by a range of 3. The output would be: key1:434 key2:162 key3:1001 I can't find the propper sed/awk line that would alter all my... (4 Replies)
Discussion started by: fzd
4 Replies

7. Shell Programming and Scripting

Change file content 4 column to one Column using script

Hi Gurus, I have file content sample: ,5113955056,,TAgent-Suspend ,5119418233,,TAgent-Suspend ,5102119078,,TAgent-Suspend filenames 120229H5_suspend, 120229H6_unsuspend I receive those files one of directory /home/temp/ I need following: 1. Backup first /home/temp/ file to... (5 Replies)
Discussion started by: thepurple
5 Replies

8. Shell Programming and Scripting

Change names in a column based on the symbols in another column

If the 4th column has - sign then the names in 3rd column has to change to some user defined names (as shown in output). Thanx input1 1 a aaaaa + 2 b bbbbb + 3 c ccccc + 4 d ddddd + 5 e eeeee + 6 f xxxxx + 8 h hhhhh +... (8 Replies)
Discussion started by: repinementer
8 Replies

9. Shell Programming and Scripting

script to change filename with numbers

ok, this one is definitely too hard for my shell-script-skills. Hopefully, there is somebody who can help me with this: I have a folder with files in it named 0.ppm 10.ppm 2.ppm ... 5.ppm 50.ppm 55.ppm ... 355.ppm 360.ppm etc. As you will notice, the order in which the files are... (5 Replies)
Discussion started by: silversurfer202
5 Replies

10. Shell Programming and Scripting

Numbers & stack commands

Hi everyone, Sorry I keep asking these stupid perl questions here. They're not any sort of assignment or anything like that, I'm just browsing through the book "Learning Perl" and I'm finding that I can do some of their practice exercises just fine, but others give me some trouble. Right now... (1 Reply)
Discussion started by: jason_v
1 Replies
Login or Register to Ask a Question