getting problem in awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting problem in awk command
# 8  
Old 04-25-2011
From what I know of your requirements so far:

Code:
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.
# 9  
Old 04-25-2011
Quote:
Originally Posted by Corona688
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:
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 ----------

Quote:
Originally Posted by Corona688
From what I know of your requirements so far:

Code:
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.

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
Code:
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

Last edited by Scott; 04-25-2011 at 04:02 PM.. Reason: Code tags
# 10  
Old 04-25-2011
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!
# 11  
Old 04-25-2011
Quote:
Originally Posted by Corona688
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.

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.
Code:
 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


Last edited by Scott; 04-25-2011 at 04:02 PM.. Reason: ...and again
# 12  
Old 04-25-2011
We crossposted. Read again.
# 13  
Old 04-25-2011
Quote:
Originally Posted by Corona688
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.
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 [code] before your code and [/code] after it;

Or, highlight your code and click the Image button;

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

Last edited by Scott; 04-25-2011 at 04:16 PM..
# 14  
Old 04-25-2011
Quote:
Originally Posted by ravi_agarwalla
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.
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!

Code:
# 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution Problem with awk command

Hi All, I am trying to find a word from a file in loop. while read i; do DB_Name=$i awk '{for(i=1;i<=NF;i++)if($i~/$DB_Name/)print $(i)}' $BTEQ_NAME > $DB_Name_TableList.txt done <Param.txt here Param.txt contents data as ODS_TARGT_RECV FIN_TARGT... (7 Replies)
Discussion started by: Shilpi Gupta
7 Replies

2. Shell Programming and Scripting

Problem with Variable and AWK command

Okay, so I am trying to use a count variable to reference the column of output sent from an echo statement. So I am trying to do this #!/bin/bash CURRENT=$PWD VAR=3 CHANGE=`echo $CURRENT | awk -F "/" '{ print \$$VAR }'` This instead of giving me the third instance after the "/" gives... (4 Replies)
Discussion started by: mkjp2011
4 Replies

3. Shell Programming and Scripting

Alias problem with awk command

Hi to all, I'm facing some problems when adding an alias like: #alias list="ls -al | awk '{ print $1, $2, $3, $4, (($5/1048576))"\t", $6, $7, $8, $9 }'" and when I enter: #list I get: Syntax Error The source line is 1. The error context is { print >>> , <<< awk:... (3 Replies)
Discussion started by: enux
3 Replies

4. UNIX for Dummies Questions & Answers

problem with awk command in script.

#!/bin/sh date=`date +%y%m%d -d"1 day ago"` inbound_dir=/vis/logfiles/to_solmis cp `grep -il ST~856~ $inbound_dir/*$date*` /vis/sumit/in_ASN/ echo 'SENDER,RECEIVER,DATE,TIME,ASNUMBER' > a.csv for i in /vis/sumit/in_ASN/* do ... (8 Replies)
Discussion started by: shrima.pratima
8 Replies

5. Shell Programming and Scripting

problem with awk command

I have written some code in a KSH script to find the index of the character in a string like this #!usr/bin/ksh string="Hi How are you" awk 'BEGIN {print index ($string, "are")}' My expected output should be : 8 but it is giving the out put as : 0 I wanted to store that index value in... (8 Replies)
Discussion started by: hemanth424
8 Replies

6. Shell Programming and Scripting

problem with awk command

I am having problem running an awk commad on a file Before applying awk command on the file After applying I don't expect more than 409743 records in the file. Why do I have 1 record more after applying awk command? Please let me know (4 Replies)
Discussion started by: dsravan
4 Replies

7. Shell Programming and Scripting

Problem in awk command

Hello, I am getting problem in awk command during matching (using if in awk) when there is special character "" I have tried by loosing the special meaning, still its not working Below is my code: set pinname_watch = "DCORRECT" set pinname = "DCORRECT\" echo 'defineGateSize... (2 Replies)
Discussion started by: nehashine
2 Replies

8. Shell Programming and Scripting

Problem with sub command (awk) and numbers

Hi, I am trying to perform a simple soustraction between two floating numbers and cannot get it done for some reason due to the use of the sub command. The following is the straight-forward result of the soustraction: $ echo | gawk '{a=968;b=967.99;c=a-b;print c}' ... (2 Replies)
Discussion started by: Indalecio
2 Replies

9. Post Here to Contact Site Administrators and Moderators

AWK command problem

How can I write an AWK so it could print some fixed strings and concatenate that with other words from an input file. Also having a string like "go" in the next line in the output file as below: Example: A fixed string like: "Hello my" and the input file is: friend mother father... (4 Replies)
Discussion started by: sybase08
4 Replies

10. Shell Programming and Scripting

problem in awk command

Hello all, i am new one to this forum. : i have file with these contents.. internal://project/squid-internal-static/icons/anthony-xpm.gif http://widget.blogrush.com/img/br.png http://www.wingware.com/css/print http://publib.boulder.ibm.com/infocenter/systems/advanced/filterwarning.css... (3 Replies)
Discussion started by: viveksnv
3 Replies
Login or Register to Ask a Question