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.
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.
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?
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
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
}'
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$
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.
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