getting problem in awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting problem in awk command
# 1  
Old 04-25-2011
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

Last edited by ravi_agarwalla; 04-25-2011 at 03:28 PM.. Reason: format incorrect
# 2  
Old 04-25-2011
All your output looks the same because you didn't post in code tags. Let me try it and see what you mean.
Quote:
below is the file and the command

Filename: test1.txt

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

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

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


Last edited by Corona688; 04-25-2011 at 03:38 PM..
# 3  
Old 04-25-2011
my original file is below one
Code:
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
Code:
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 ----------
Code:
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

Last edited by Scott; 04-25-2011 at 03:38 PM.. Reason: Use code tags, please...
# 4  
Old 04-25-2011
Yes, but that doesn't answer my question.
Quote:
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?
If you just want all columns to remain the same width, the solution I posted should work.
# 5  
Old 04-25-2011
Quote:
Originally Posted by Corona688
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?
# 6  
Old 04-25-2011
Quote:
Originally Posted by ravi_agarwalla
ok let me check. but can you explain how it works.. what the split will do..
It creates an array where W[1]=1, W[2]=4, etc.
Quote:
and if my file has more than 50 columns and i want to increase value of 40th field then will it work?
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:
Quote:
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?
# 7  
Old 04-25-2011
Quote:
Originally Posted by Corona688
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?


below is what i am getting.. its not the desired result
Code:
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

Moderator's Comments:
Mod Comment Code tags, please...

Last edited by Scott; 04-25-2011 at 03:54 PM.. Reason: Code tags
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