Problems with awk printf, formatted output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with awk printf, formatted output
# 1  
Old 09-05-2012
Data Problems with awk printf, formatted output

Hi,

i have a script, which is incomplete, am on my way developing it.

Input
Code:
1,12,2012,IF_TB001
2,12,2012,3K3
3,Z56,00000,25,229,K900,00, ,3G3, ,USD, ,0000000000,000, , , , 550000000
3,Z56,00000,53,411,W225,00,000, , ,USD,OM170,0000000000,000, , , , -550000000
4,Z56,COUNT, 4,SUM LOC, 552215541
5,COUNT, 80

Code:
 
#!/usr/bin/ksh
set  -x
FILENAME=/home/glcomp.int
OUT=/tmp/out_res
cat $FILENAME | while  read line
do
if   [[ $(echo $line|cut -c1-1) = "1" ]];
then  echo $line | nawk 'BEGIN{FS=OFS=","}  {print}' >> $OUT
elif  [[ $(echo $line|cut -c1-1) = "2" ]];
then   echo $line | nawk 'BEGIN {FS=OFS=","}  {print}'  >> $OUT
elif  [[ $(echo $line|cut-c1-1) = "3" ]];
then  echo $line >> $OUT
elif  [[ $(echo $line|cut-c1-1) = "4" ]];
then  echo $line  >> $OUT
else  [[ $(echo $line|cut-c1-1) = "5" ]];
 echo $line | nawk  ' BEGIN {FS=OFS="," ;}  {printf  "%c%4c%-10d" ,$1,$2,$3 }  '>> $OUT
fi
done

OUTPUT should be same as input, puzzled yes, i am going to make changes to few columns in records starting with 3. so currently i want to output to be same as input same csv format and with space padding etc.,

I am unable to get space padding and "," when i use printf "%c%4c%-10d" ,$1,$2,$3 the last print staement

please suggest me some ideas

Last edited by Franklin52; 09-06-2012 at 04:18 AM.. Reason: Adding code tags for data sample
# 2  
Old 09-05-2012
And how should the output look?
Your script is very inefficient. With awk, you can operate on each line of the file without a loop.
# 3  
Old 09-05-2012
Hi,

Try this out,

Code:
printf  "%c,%4c,%-10d" ,$1,$2,$3;

Cheers,
Ranga Smilie
# 4  
Old 09-06-2012
@elixir_sinari

OUT should be similar to input. excpet that the values in 5th and 8th column of records starting with 3 will have to be compared to a constant then replace the 9th column with a constant

my requirement
for records starting with 3 { if $5 eq 229 && $8 eq 990 then $9=8J8}

all other records should be print as it is

INPUT
Code:
1,12,2012,IF_TB001
2,12,2012,3K3
3,Z56,00000,25,229,K900,00,990,3G3, ,USD, ,0000000000,000, , , , 550000000
3,Z56,00000,53,411,W225,00,000, , ,USD,OM170,0000000000,000, , , , -550000000
4,Z56,COUNT, 4,SUM LOC, 552215541
5,COUNT, 80

OUTPUT
Code:
1,12,2012,IF_TB001
2,12,2012,3K3
3,Z56,00000,25,229,K900,00,990,8J8, ,USD, ,0000000000,000, , , , 550000000
3,Z56,00000,53,411,W225,00,000, , ,USD,OM170,0000000000,000, , , , -550000000
4,Z56,COUNT, 4,SUM LOC, 552215541
5,COUNT, 80


Last edited by Franklin52; 09-06-2012 at 04:18 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 09-06-2012
Try this..

Code:
awk -F "," '{ if ($1 == 3) { if ( $5 == 229 && $8 == 990) { $9 = "8J8"; print} else {print} } else { print}}' OFS=\, file

# 6  
Old 09-06-2012
Code:
awk -F, '$1=="3"&&$5=="229"&&$8=="990"{OFS=FS;$9="8J8"}1' file

# 7  
Old 09-06-2012
@

Both the above does not work, since i need the formatting of the csv file to be intact without any change to other fileds
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Formatted output in PERL

Hi Techies, I'm a newbie to PERL, Please help me with following problem. I have an input text file like below cat Input.txt 418673132,P 492538858,P 384535478,P 521522357,I 529435679,I 183617024,P 184414408,I 735510689,P 736238343,I 411642045,I 412690979,I 104232783,I (2 Replies)
Discussion started by: mahi_mayu069
2 Replies

2. Shell Programming and Scripting

Shell Script Problems, Lose formatting when copy pasting from formatted file.

Hello, I'm having trouble with formatting some text via the terminal. I can get it perfectly formatted, but when I try and copy paste the text from the output file it loses it's formatting. Very frustrating! Basically I have 7 files (data data2 data3 data4 data5 data6 data7) containing a... (13 Replies)
Discussion started by: facetoe
13 Replies

3. UNIX for Dummies Questions & Answers

Request for Formatted Output

Can you please tell me how to just get only the output of dealers I & V information along with their subtotals in the next line of the file and create a new file, The dealer position along with corresponding totals may change everyday to any position above or below in the file, please help Thanks (2 Replies)
Discussion started by: Ariean
2 Replies

4. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

5. Shell Programming and Scripting

How to format the output using float in awk{printf}

Hi I'm using awk to manipulate the data in the 6th field of the file xxx_yyy.hrv. The sample data that is available in this field is given below 220731.7100000000000000 When i tried using this command cat xxx_yyy.hrv | awk '{printf("%23.16f\n",$6*-1)}' I get the output as... (4 Replies)
Discussion started by: angelarosh
4 Replies

6. UNIX for Advanced & Expert Users

Help please...output problems with printf.

#include <stdio.h> #include <math.h> // this function calculates the volume of a Cylinder int main(void) { int r; // radius int h; // height double M_PI; // pi int pOne = pow (r, 2); // get user input of radius and height printf ("Enter your... (3 Replies)
Discussion started by: pwanda
3 Replies

7. Shell Programming and Scripting

Formatted Output

Hi I have the following lines in a file SWPRC000001NOT STATED 1344 SWPRC000001NOT STATED 1362 SWPRC000001NOT STATED 1418 SWPRC000001NOT STATED 1436 SWPRC000001NOT STATED ... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

8. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

9. Shell Programming and Scripting

Formatted output in KSH

Hi, Is there some way to get formatted output in ksh? Something like a properly alligned tabular format. I tried adding '\t' to echo statements, but it doesn't come properly alligned 'hello' A simple Hello 'helloworld' A helloworld statement I need the second coloumn to... (1 Reply)
Discussion started by: psynaps3
1 Replies

10. Shell Programming and Scripting

find: problems escaping printf-command string

Hi Folks! Can you help me with this find -printf command. I seem to be unable to execute the printf-command from my shell script. I'm confused: :confused: My shell script snippet looks like this: #!/bin/sh .. COMMAND="find ./* -printf '%p %m %s %u %g \n'" echo "Command: ${COMMAND}"... (1 Reply)
Discussion started by: grahamb
1 Replies
Login or Register to Ask a Question