awk is changing my FS !!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk is changing my FS !!
# 1  
Old 10-04-2008
Data 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  
Old 10-04-2008
set OFS=" " any no of spaces you want
# 3  
Old 10-04-2008
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  
Old 10-04-2008
MySQL

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing format of file with awk

Hi all, I have a file that looks like this: Closest words to: manifesto >>>> Closest words to: passport >>>> and I want to reformat this with awk with the following desired result: manifesto 0.99999999999999978, 'manifesto' 0.72008211381623111, 'communiqu\xe9'... (5 Replies)
Discussion started by: owwow14
5 Replies

2. Shell Programming and Scripting

Changing the variable using awk?

Dear all, I have kind of used both the awk/sed command and found them really useful. But at the necessity I am having right now, I need help. Actually, I would like to do the following in file script.sh PATH535="/eos/uscms/store/user/pooja04//analysis2012/535/mc/summer12/002/tt/" ... (2 Replies)
Discussion started by: emily
2 Replies

3. Shell Programming and Scripting

help with awk with changing parameters: TY

hi, i got several pieces of code that look like: for ff in `seq 3 $nlpN`; do npc1=`awk 'NR=='$ff' {print $1}' p_walls.raw`; echo ${npc1}; npc2=`awk 'NR=='$ff' {print $2}' p_walls.raw`; npc3=`awk 'NR=='$ff' {print $3}' p_walls.raw`; npRs=`awk 'NR=='$ff' {print $4}'... (10 Replies)
Discussion started by: calim_cfd
10 Replies

4. Shell Programming and Scripting

sed/awk changing values

Can somebody help me out and provide me with a SED or AWK solution that converts TO_DATE CLAUSE -> TIMESTAMP I need to keep the PARTION value (HISTORY_20110417) and DATE/TIME value (2011-04-18 00:00:00) the same for every line PARTITION HISTORY_20110417 VALUES LESS THAN (TO_DATE('... (3 Replies)
Discussion started by: BeefStu
3 Replies

5. UNIX for Dummies Questions & Answers

Changing Text with sed or awk

I'm changing some html code on multiple web pages and I need to match particular phrases but keep some text within each phrase. E.G. I need to change this line: <DIV id="heading">Description:</DIV> into <span class="hlred">Description:</span><br /> The text "Description:" may... (2 Replies)
Discussion started by: hal8000
2 Replies

6. Shell Programming and Scripting

Changing part of column value with AWK

Hi All , Here is my req I am extracting a column value and I want to change part of its value according to user input Say the column I extracted is of 15 digits and I want to change the 11th to 14th position with user input 4 digit value Example column value 111111111111111 user input... (8 Replies)
Discussion started by: Pratik4891
8 Replies

7. Shell Programming and Scripting

Changing one number in all files using awk

Hi I want to change the number 70 mentioned in my file to 76 by using awk. I know how to change all same digits but not one particular number. I have 29 files almost similar to this. One of my files looks like #Input file for 200K NPT molecular dynamics of final 70%XL made from 58.5%... (3 Replies)
Discussion started by: ananyob
3 Replies

8. Shell Programming and Scripting

Help changing date output with AWK

Hello all, I have an issue where I'm trying to change a date in a csv text file. The file contains lines that have several fields. For example "John", "Smith","some_address","some_city","555-555-5555","11/11/1972" "Joan","User","some_address","some_city","444-444-4444","12/02/1963" The date... (6 Replies)
Discussion started by: commobox
6 Replies

9. Shell Programming and Scripting

Changing file names with AWK

Dear All, I have some thousands of files in a folder and i need to change those file names without opening the file (no need to change anything in the file content, need to change the file name only). The filenames are as follows: Myfile_name.1_parameter Myfile_name.2_parameter... (6 Replies)
Discussion started by: Fredrick
6 Replies

10. Shell Programming and Scripting

Help with awk script, changing the FS for a single variable

Hi all, im new to awk and would apreciate if you could tell me how to do this, i have a file with several entries like this: 2008-09-09 21:57:45 44 403 CUSTOM_EVENT Upgrade - end1 2008-09-09 21:57:46 45 403 CUSTOM_EVENT Component Check - start... (3 Replies)
Discussion started by: sx3v1l_1n51de
3 Replies
Login or Register to Ask a Question