|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
doneOUTPUT 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
|
||||
|
||||
|
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
|
||||
|
||||
|
Hi, Try this out, Code:
printf "%c,%4c,%-10d" ,$1,$2,$3; Cheers, Ranga
|
|
#4
|
|||
|
|||
|
@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
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
awk -F, '$1=="3"&&$5=="229"&&$8=="990"{OFS=FS;$9="8J8"}1' file |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
@
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|