The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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 11:09 AM
Changing the order using sed venu_nbk UNIX for Dummies Questions & Answers 9 07-29-2006 06:03 PM
Changing Users Bab00shka Shell Programming and Scripting 2 08-01-2003 05:44 AM
Changing IP's Hordak UNIX for Dummies Questions & Answers 2 08-03-2002 01:42 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2008
jolecanard jolecanard is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 36
Unhappy awk is changing my FS !!

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 want to find some ID (eg. 621530) and change the value following it.

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

The problem is that it removes the three spaces in front of the row if it changes something in the row...removing the commented line solves the problem.
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?
  #2 (permalink)  
Old 10-04-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,418
set OFS=" " any no of spaces you want
  #3 (permalink)  
Old 10-04-2008
jolecanard jolecanard is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 36
I already tried that...
The problem then is that all my columns are spaced with only one space!

I want to keep the structure:

Code:
 
"   "3" "xxxxxx"  "x.xxxxExx" "xxxxxx"  "x.xxxxExx"...

  #4 (permalink)  
Old 10-04-2008
jolecanard jolecanard is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 36
Thumbs up

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

In fact for few lines, some values do not have the format "x.xxxxExxx", but less characters (eg "0.0"). Then, the missing characters are "spaces" in order to keep the alignement:


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

My solution works as long as I am not changing values located on the rows with "reduced" characters...(as long as nothing is changed in the row, awk does not re-concatenate the row, thus conserving the right spacing).

Also, using all the argument in print is not convenient...

How could I solve these two problems?
  #5 (permalink)  
Old 10-04-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
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

Regards
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0