![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| changing permission using FTP | panknil | Shell Programming and Scripting | 3 | 11-07-2007 08:08 PM |
| Changing userID and Changing group and GID | deal732 | Shell Programming and Scripting | 2 | 04-18-2007 10:09 AM |
| Changing the order using sed | venu_nbk | UNIX for Dummies Questions & Answers | 9 | 07-29-2006 05:03 PM |
| Changing Users | Bab00shka | Shell Programming and Scripting | 2 | 08-01-2003 04:44 AM |
| Changing IP's | Hordak | UNIX for Dummies Questions & Answers | 2 | 08-03-2002 12:42 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Hello there,
Here is (a part) of the file I want to change. Note the three spaces before the "3" (could be "2" or "0" also). Code:
3 621530 1.1935E-02 631530 1.1293E+01 641530 1.1117E-02 571540 4.4419E-14 3 581540 2.6670E-10 591540 3.8610E-09 601540 1.1016E-06 611540 3.2618E-06 3 611541 6.5572E-07 621540 9.8307E+00 631540 3.1177E+00 641540 1.4615E+00 3 571550 0.0000E+00 581550 8.8139E-12 591550 1.0739E-09 601550 2.5639E-07 3 611550 1.1011E-06 621550 3.6787E-05 631550 3.4821E+00 641550 3.1855E+00 3 641551 0.0000E+00 581560 9.4585E-13 591560 7.5358E-11 601560 1.2023E-07 3 611560 1.7968E-07 621560 5.4237E-04 631560 3.0054E-02 641560 6.5687E+00 3 581570 2.3926E-14 591570 9.9322E-12 601570 4.3929E-09 611570 4.6793E-07 3 621570 5.5911E-06 631570 6.3564E-04 641570 3.2355E+00 591580 2.8608E-13 I am using this script: Code:
VALUE=".........."
awk -v var="$VALUE" 'BEGIN{}
NR==FNR{a[$0]=$0; next}
{for(i=2;i<9;i+=2)if($i in a){$(i+1)=var}}
#{$1=" "$1}
{print}' ID_to_be_changed my_file
However I do not know how to keep the same number of spaces between the rows: 2 spaces between the ID and the following value, and 1 space between a value and the ID after that (the script above turns everything into a single space). Can someone help me? |
|
||||
|
For now, the only solution I have is:
Code:
VALUE=".........."
awk -v var="$VALUE" 'BEGIN{FS=OFS=" "}
NR==FNR{a[$0]=$0; next}
{for(i=4;i<9;i+=2)if($i in a){$(i+1)=var}}
{for(i=4;i<9;i+=2)if($i in a){print " ",$1,$2," "$3,$4," "$5,$6," "$7,$8," "$9;next}}
{print}' ID_to_be_changed my_file
Code:
2 982520 1.6134E-14 982530 1.7241E-17 982540 3.4164E-20 982550 1.2285E-24 2 992530 1.8708E-17 992541 6.2076E-22 992540 7.5646E-20 992550 7.0917E-22 2 162500 7.6697E-08 0 0.0000E+00 0 0.0000E+00 0 0.0000E+00 3 10030 2.0892E-01 30060 6.0786E-04 30070 1.3046E-05 40090 1.9438E-05 3 40100 1.1748E-04 60140 1.6964E-05 280660 0.0000E+00 290660 1.4066E-14 3 300660 5.0403E-09 290670 7.6644E-19 300670 2.4718E-10 300680 1.1002E-12 Also, using all the argument in print is not convenient... How could I solve these two problems? |
|
||||
|
You can use printf to print the line with a fixed format, something like:
Code:
VALUE=".........."
awk -v var="$VALUE" 'BEGIN{FS=OFS=" ";fmt="%5s%7s%12s%7s%12s%7s%12s%7s%12s\n"}
NR==FNR{a[$0]=$0; next}
{for(i=4;i<9;i+=2)if($i in a){$(i+1)=var}}
{for(i=4;i<9;i+=2)if($i in a){printf(fmt,$1,$2,$3,$4,$5,$6,$7,$8,$9);next}}
{print}' ID_to_be_changed my_file
|
| Sponsored Links | ||
|
|