Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-05-2012
Registered User
 
Join Date: Jul 2011
Posts: 37
Thanks: 9
Thanked 0 Times in 0 Posts
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 03:18 AM.. Reason: Adding code tags for data sample
Sponsored Links
    #2  
Old 09-05-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
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.
Sponsored Links
    #3  
Old 09-05-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
Hi,

Try this out,


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

Cheers,
Ranga
    #4  
Old 09-06-2012
Registered User
 
Join Date: Jul 2011
Posts: 37
Thanks: 9
Thanked 0 Times in 0 Posts
@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 03:18 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #5  
Old 09-06-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,488
Thanks: 56
Thanked 426 Times in 422 Posts
Try this..


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

Sponsored Links
    #6  
Old 09-06-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts

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

Sponsored Links
    #7  
Old 09-06-2012
Registered User
 
Join Date: Jul 2011
Posts: 37
Thanks: 9
Thanked 0 Times in 0 Posts
@

Both the above does not work, since i need the formatting of the csv file to be intact without any change to other fileds
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
output - tab formatted - awk Fredrick Shell Programming and Scripting 3 11-27-2009 07:32 AM
Help please...output problems with printf. pwanda UNIX for Advanced & Expert Users 3 10-19-2008 07:30 PM
Formatted Output dhanamurthy Shell Programming and Scripting 6 05-13-2008 02:30 AM
Formatted output - awk dhanamurthy Shell Programming and Scripting 3 05-11-2008 11:25 PM
Formatted output in KSH psynaps3 Shell Programming and Scripting 1 07-05-2006 08:03 AM



All times are GMT -4. The time now is 07:32 PM.