Hi all;

I'm stuck with this simple awk script,i need to group the lines which the position of 28 length 3 (if the line not starts with 0)that contains "688" into 1 group and other than "688" into another group. My problem is the script only read other than "688" and ignores the lines which contains "688".

The file look like this:
040171011140820070000000009650244002933170003000000075272
1F921338300506 01082007000688jkjddiwe02499834fg
1F921338300506 010820070000198676767675645shdjd
1H912279980109 0108200700009965787889890090uyk
1H912279980109 01082007000688djjdfieuireurireiro

I used this script to group the lines:
sort -t" " -rk2.12,2.14 FileLoad.txt |awk '
/^1/ { sub(/1/, "2") }
/^3/ { saved = $0 }
/^0/ { $NF = ($NF FS saved) }
{ x[FNR] = $0 }
END{
if (substr(x[FNR],1,1)=="0")
print x[FNR]


if(substr(x[j],1,1)!="0" && substr(x[j],28,3)=="688")
{
print 1
for (j=1; j<=FNR; j++)
if(substr(x[j],1,1)!="0" && substr(x[j],28,3)=="688")
print x[j]
}


if(substr(x[j],1,1)!="0" && substr(x[j],28,3)!="688")
{
print 1
for (j=1; j<=FNR; j++)
if(substr(x[j],1,1)!="0" && substr(x[j],28,3)!="688")
print x[j]
}
}'

After execute the script,the output shown:
040171011140820070000000009650244002933170003000000075272
1
2H912279980109 0108200700009965787889890090uyk
2F921338300506 010820070000198676767675645shdjd


can anyone help me to figure out?tq in advance

Hey there,

The simplest thing to do, if you're getting the opposite results of what you were expecting, would be to reverse your comparisons

if(substr(x[j],1,1)!="0"
change to:
if(substr(x[j],1,1)=="0"

and so on,
Mike

Hi all;

I'm stuck with this simple awk script,i need to group the lines which the position of 28 length 3 (if the line not starts with 0)that contains "688" into 1 group and other than "688" into another group. My problem is the script only read other than "688" and ignores the lines which contains "688".

The file look like this:

040171011140820070000000009650244002933170003000000075272
1F921338300506 01082007000688jkjddiwe02499834fg
1F921338300506 010820070000198676767675645shdjd
1H912279980109 0108200700009965787889890090uyk
1H912279980109 01082007000688djjdfieuireurireiro

I used this script to group the lines:

sort -t" " -rk2.12,2.14 FileLoad.txt |awk '
/^1/ { sub(/1/, "2") }
/^3/ { saved = $0 }
/^0/ { $NF = ($NF FS saved) }
{ x[FNR] = $0 }
END{
if (substr(x[FNR],1,1)=="0")
print x[FNR]


if(substr(x[j],1,1)!="0" && substr(x[j],28,3)=="688")
{
print 1
for (j=1; j<=FNR; j++)
if(substr(x[j],1,1)!="0" && substr(x[j],28,3)=="688")
print x[j]
}


if(substr(x[j],1,1)!="0" && substr(x[j],28,3)!="688")
{
print 1
for (j=1; j<=FNR; j++)
if(substr(x[j],1,1)!="0" && substr(x[j],28,3)!="688")
print x[j]
}
}'

After execute the script,the output shown:

040171011140820070000000009650244002933170003000000075272
1
2H912279980109 0108200700009965787889890090uyk
2F921338300506 010820070000198676767675645shdjd

can anyone help me to figure out?tq in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.