getting problem in awk command

Hi,

I have one file with tab delimited values in it. i want to increase the value of 6th field by 2 if value of 3rd field is greater than 2 . The command is working fine but space between the field is getting removed after adding.

below is the file and the command

Filename: test1.txt

1|2345|23|34|98|6 |2
2|4356|54|12|76|5 |5
3|2342|65|76|4 |3 |7
4|4556|76|34|54|2 |8
5|2352|87|12|23|7 |9

I want the output like below

1|2345|23|34|98|8 |2
2|4356|54|12|76|7 |5
3|2342|65|76|4 |5 |7
4|4556|76|34|54|4 |8
5|2352|87|12|23|9 |9

but i am getting output with space removed like below

1|2345|23|34|98|8|2
2|4356|54|12|76|7|5
3|2342|65|76|4 |5|7
4|4556|76|34|54|4|8
5|2352|87|12|23|9|9

Anybody can help me out

All your output looks the same because you didn't post in code tags. Let me try it and see what you mean.

---------- Post updated at 12:29 PM ---------- Previous update was at 12:27 PM ----------

I'm not sure you posted quite what you meant. Are you saying the numbers in certain columns have to be padded with spaces to at least 2 length? Or do you have potentially random spacing in your file you wish to preserve?

The whitespace is of course stripped when you do numeric options, you have to put it back.

If columns have to be specific widths:

awk 'BEGIN { FS="|"; OFS="|"; split("1|4|2|2|2|2|2", W, "|"); }
{ if($6 > 2) $3 += 2;
   for(N=1; W[N]; N++) while(length($N) < W[$N]) $N=" "$N;
   print; }'

Makes data like

1|2345|25|34|98|6 | 2
2|4356|56|12|76|5 | 5
3|2342|67|76|4 |3 | 7
4|4556|76|34|54|2 | 8
5|2352|89|12|23|7 | 9

my original file is below one

1|2345|23|34|98|6  |2
2|4356|54|12|76|5  |5
3|2342|65|76|4 |3  |7
4|4556|76|34|54|2  |8
5|2352|87|12|23|7  |9

and i want output of 6th field to get increase by 2 without space in 6th field being removed.
like output below

1|2345|23|34|98|8  |2
2|4356|54|12|76|7  |5
3|2342|65|76|4 |5  |7
4|4556|76|34|54|4  |8
5|2352|87|12|23|9  |9

which i am not getting as the space in 6th value is getting removed.

---------- Post updated at 01:35 PM ---------- Previous update was at 01:32 PM ----------

1|2345|23|34|98|8|2
2|4356|54|12|76|7|5
3|2342|65|76|4  |5|7
4|4556|76|34|54|4|8
5|2352|87|12|23|9|9

u can see the output above where the space is not present in 6th field after the value which was there in original file

Yes, but that doesn't answer my question.

If you just want all columns to remain the same width, the solution I posted should work.

ok let me check. but can you explain how it works.. what the split will do.. and if my file has more than 50 columns and i want to increase value of 40th field then will it work?

It creates an array where W[1]=1, W[2]=4, etc.

If you want me to write something that works for you, it would really really help to know what your data is and to know what you want!

For the third time:

ok let me check. but can you explain how it works.. what the split will do.. and if my file has more than 50 columns and i want to increase value of 40th field then will it work?

below is what i am getting.. its not the desired result

ravi@andLinux:~/RAVI$ awk 'BEGIN { FS="|"; OFS="|"; split("1|4|2|2|2|2|2", W, "|
"); }
{ if($1 > 1) $6 += 2;
   for(N=1; W[N]; N++) while(length($N) < W[$N]) $N=" "$N;
   print; }' test1.txt
1|2345|23|34|98|6  | 2
 2|4356|54|12|76| 7| 5
 3|2342|65|76|4 | 5| 7
 4|4556|76|34|54| 4|8
 5|2352|87|12|23|9|9
ravi@andLinux:~/RAVI$

Its still not in correct format

From what I know of your requirements so far:

awk 'BEGIN { FS="|"; OFS="|"; }
{
        # save lengths for later
        for(N=1; N<=NF; N++)    W[N]=length($N);

        # alter column
        if($6 > 2) $3 += 2;

        # make sure all columns are the right width
        for(N=1; N<NF; N++) while(length($N) < W[N]) $N=" "$N;
        print
}'

This will preserve random lengths.

for your question

Are you saying the numbers in certain columns have to be padded with spaces to at least 2 length? Or do you have potentially random spacing in your file you wish to preserve?

length of each field is fixed. Suppose length of 6th field is 5 but the data in rows of 6th field is of single value like 2,3,4 etc.but when i m writing command to increase the value of 6th field by 2 to make output like 4,5,6 etc then the space gets removed

---------- Post updated at 01:58 PM ---------- Previous update was at 01:52 PM ----------

Thanks for quick reply but length is getting fixed with this command but the value are getting padded to right which i dont want.first value is left justified in 6th field and rest are right justified which is incorrect. i want just to increase the value

ravi@andLinux:~/RAVI$ awk 'BEGIN { FS="|"; OFS="|"; }
{
        # save lengths for later
        for(N=1; N<=NF; N++)    W[N]=length($N);

        # alter column
        if($1 > 1) $6 += 2;

        # make sure all columns are the right width
        for(N=1; N<NF; N++) while(length($N) < W[N]) $N=" "$N;
        print
}' test1.txt
1|2345|23|34|98|6  |2
2|4356|54|12|76|  7|5
3|2342|65|76|4 |  5|7
4|4556|76|34|54|  4|8
5|2352|87|12|23|  9|9
ravi@andLinux:~/RAVI$

Your quick response will be appreciated

The last solution I posted should work no matter what you do to the columns, then.

I could make it more efficient by only updating the columns that were changed but this way you can substitute in whatever you want and it will still work.

---------- Post updated at 12:59 PM ---------- Previous update was at 12:58 PM ----------

That's a trivial change. $N=" "$N; becomes $N=$N" ";

You still haven't figured out how to post in code tags? Quote one of my posts! It's easy!

values are getting right justified after increasing the 6th field value by 2 which will stop my process.look at the output nicely in 6th field. it should be like this only.

 1|2345|23|34|98|6  |2
 2|4356|54|12|76|7  |5
 3|2342|65|76|4  |5  |7
 4|4556|76|34|54|4  |8
 5|2352|87|12|23|9  |9

We crossposted. Read again.

Hi thanks for the reply.. i am getting the desired result. Thank you very very very much

Can you explain how it works broadly so that i can gain little more knowledge abt unix.i am not able to understand anything abt this code.

awk 'BEGIN { FS="|"; OFS="|"; }
{
        # save lengths for later
        for(N=1; N<=NF; N++)    W[N]=length($N);

        # alter column
        if($1 > 1) $6 += 2;

        # make sure all columns are the right width
        for(N=1; N<NF; N++) while(length($N) < W[N]) $N=$N" ";
        print
}' test1.txt

edit by scottn: As Corona688 says, adding Code tags is really easy!

Either type in

```text
 before your code and 
```

after it;

Or, highlight your code and click the button;

Or, click Quote on Conona688's post to see how he has done it.

You forgot to post in code tags, again. Do you realize there's moderators having to follow you around and clean up after you? Quote one of my posts to find out how to use code tags! It's easy!

# Set input and output field separators so awk knows to split on | and print |
awk 'BEGIN { FS="|"; OFS="|"; }
{
        # save lengths for later
        # Loops through all fields 1-NF, saving their lengths into W[N]
        for(N=1; N<=NF; N++)    W[N]=length($N);

        # alter column
        if($1 > 1) $6 += 2;

        # make sure all columns are the right width
        # Loops through each column 1-NF, then checks their
        # current length against the length stored in W.
        # If it's shorter, add spaces until they're identical.
        for(N=1; N<NF; N++) while(length($N) < W[N]) $N=$N" ";
        # print all columns separated by |
        print
}' test1.txt