I am a newbie to shell programming and maybe somebody can help me out a little. Here's my problem:
I got a PIPE delimited file with header record. I need to add a new column name as RECORDKEY. I would like to use a counter to generate this new value for each record. I plan to do a while loop and adding the key (e.g. 1-100). To complicate the logic a bit, I need to check column4 and compare it to the value of previous column4. If they match then I don't need to do add +1(cnt=cnt+1) and still use the current value of "cnt".
I appreciate all help! I tried looking at different samples but I still cannot build a solid foundation to the problem I have.
INPUT FILE:
"SA ID"|"PER ID"|"SP ID"|"ACCT ID"|"PREM ID"|
"4809192005"|"8548667904"|"8579918510"|"4809192892"|"8579918555"|
"3242844005"|"8149108497"|"8138547810"|"3242844114"|"8138547802"|
"1039653968"|"8060515953"|"8006519921"|"1039653537"|"8006519920"|
"8864439005"|"3291253901"|"3406088605"|"8864439315"|"3406088600"|
"9746328005"|"8006660653"|"8006564710"|"9746328430"|"8006564769"|
"7655489005"|"2113888404"|"2165831410"|"9746328430"|"2165831429"|
"2833100005"|"7770432791"|"7770421305"|"2833100003"|"7770421334"|
"3842956005"|"2509734222"|"2551230310"|"3842956375"|"2551230340"|
DESIRED OUTPUT FILE:
"KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT ID"|"PREM ID"|
1|"4809192005"|"8548667904"|"8579918510"|"4809192892"|"8579918555"|
2|"3242844005"|"8149108497"|"8138547810"|"3242844114"|"8138547802"|
3|"1039653968"|"8060515953"|"8006519921"|"1039653537"|"8006519920"|
4|"8864439005"|"3291253901"|"3406088605"|"8864439315"|"3406088600"|
5|"9746328005"|"8006660653"|"8006564710"|"9746328430"|"8006564769"|
5|"7655489005"|"2113888404"|"2165831410"|"9746328430"|"2165831429"|
6|"2833100005"|"7770432791"|"7770421305"|"2833100003"|"7770421334"|
7|"3842956005"|"2509734222"|"2551230310"|"3842956375"|"2551230340"|
As you can see, line 5 and 6 has the same $4 value. So I don't want to change the KEYRECORD value.
agama
January 5, 2012, 7:49pm
2
I think this might work:
awk -F "|" '
NR == 1 { print "\"KEYRECORD\"|" $0; next; }
{
if( !last || last != $4 )
n++;
print n "|" $0;
last = $4;
}
' input-file
Thanks for the reply! Everything worked!
This time I have to check first if $4 is blank or not. Then create a new record key if $5 does not match the previous value.
My problem right now is my 2nd IF statement. For some reason it is not working. My desired output is to create a new key record if $4 is not blank.
awk -F \| '
NR == 1 { print "\"KEYRECORD\"|" $0; next; }
{
if( $4 -eq 0 || !last || last != $4 || last5 != $5) {
echo "\nProcess black account id....\n";
n++;
}
else
if( !last || last != $4 ) {
echo "\nProcess nonblank account id....\n";
n++;
}
print n "|" $0;
last = $4;
last5 = $5;
}
' $CUSTFINAL > $CUSTFINAL2
agama
January 9, 2012, 5:04pm
4
The expression syntax in awk is like C and not like that of the test function. Try this change:
awk -F \| '
NR == 1 { print "\"KEYRECORD\"|" $0; next; }
{
if( $4 == "\"0\"" || !last || last != $4 || last5 != $5) {
echo "\nProcess black account id....\n";
n++;
}
else
if( !last || last != $4 ) {
echo "\nProcess nonblank account id....\n";
n++;
}
print n "|" $0;
last = $4;
last5 = $5;
}
' $CUSTFINAL > $CUSTFINAL2
Because your data is enclosed in quotes, you must include the quotes in the comparison.
This is my desired output and I cannot get it to work.
"KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT ID"|"PREM ID"|"PHONE"|"NA
1|" "|" "|"3197691605"|" "|"3197691657"|"
2|" "|" "|"3246309010"|" "|"3246309029"|"
3|" "|" "|"3350909705"|" "|"3311776290"|"
3|" "|" "|"3311776205"|" "|"3311776290"|"
4|" "|" "|"3329099805"|" "|"3329099858"|"
5|" "|" "|"3621605305"|" "|"3621605356"|"
6|"0001344005"|"3157734331"|"3209921605"|"0001344441"|"3209921631"|"
6|"7189943005"|"3231403121"|"3210736405"|"0001344441"|"3210736492"|"
7|"0006746005"|"3287909287"|"3340056905"|"0006746763"|"3340056937"|"
8|"0007922990"|"5053661026"|"3519545205"|"0007922859"|"3519545208"|"
9|"0008717005"|"3508935326"|"3519329105"|"0008717685"|"3519329168"|
10|"0008969005"|"7117287080"|"3613118705"|"0008969048"|"3613118796"|
11|"0009160005"|"5094212566"|"3467460805"|"0009160056"|"3467460835"|
---------- Post updated at 06:18 PM ---------- Previous update was at 05:52 PM ----------
You are the best! I cannot figure how to compare the enclosed quotes and this fixed my code!! Thanks again!!