Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

explain the unix script

Status
Not open for further replies.

plumz

Programmer
Jul 30, 2003
3
US

cat $1|awk '{print substr($0,2,1) substr($0,4)}'|
awk '{
line=$0;
first_time1=0;
first_time=0;
do
{
if (match(line, /^100/) != 0)
{
cat $1|awk '{print substr($0,2,1) substr($0,4)}'|
awk '{
line=$0;
first_time1=0;
first_time=0;
do
{
if (match(line, /^100/) != 0)
{
cat $1|awk '{print substr($0,2,1) substr($0,4)}'|
awk '{
line=$0;
first_time1=0;
first_time=0;
do
{
if (match(line, /^100/) != 0)
{
cat $1|awk '{print substr($0,2,1) substr($0,4)}'|
awk '{
line=$0;
first_time1=0;
first_time=0;
do
{
if (match(line, /^100/) != 0)
{
if (first_time != 0)
{
printf ("*500\n");
printf ("%s\n",line);
}
else
{
first_time=1;
printf ("%s\n",line);
}
}
else
{
if( first_time1 != 0 )
{
printf ("%s\n",line);
}
first_time1 = 1;
}
}
while (getline line !=0)
printf("*500\n");
}'> $2
 
i'm assuming that your script really is:
Code:
cat $1|awk '{print substr($0,2,1) substr($0,4)}'|
    awk '{                                        
        line=$0;                                  
        first_time1=0;                            
        first_time=0;                             
        do                                        
        {                                         
            if (match(line, /^100/) != 0)         
           {                                      
            if (first_time != 0)                  
            {                                     
              printf ("*500\n");                  
              printf ("%s\n",line);               
            }                                     
            else                                  
            {                                     
              first_time=1;                       
              printf ("%s\n",line);               
            }                                     
           }                                      
           else                                   
           {                                      
             if( first_time1 != 0 )               
             {                                    
               printf ("%s\n",line);              
             }                                    
             first_time1 = 1;                     
           }                                      
        }                                         
        while (getline line !=0)                  
       printf("*500\n");                          
    }'>  $2

and it's goddamn awful code ...

Code:
cat $1 | awk '
BEGIN
{
    first_time1=0;
    first_time=0;
}
{
    line=substr($0,2,1) substr($0,4);
    if (index(line, "100") == 1)
    {
        if (first_time == 0)
        {
            first_time=1;
        } else {
            print "*500";
        }
        print line;
    } else {
        if( first_time1 == 1 )
        {
            print line;
        }
        first_time1 = 1;
    }
}
END
{
    print "*500";
}
'> $2

is slightly better ... but not much.

what it appears to be doing is:
getting the second character, and everything else after (and including) the fourth character.

so '1234567890' would become '24567890'

it then checks that the first 3 characters match '100' so that means the original file would have to be 41900 or similar at the beginning.

if those 3 characters exist and it is not the first time those 3 characters have existed at the beginning of the line then output "*500" after that; if those 3 characters exist at the beginning then output the line.

if those 3 characters don't exist at the beginning and this is not the first time that these characters don't exist at the beginning then output the line.

finally when all lines are processed output "*500" at the end of the file.

$1 is the file you read in.
$2 is the file you output to.

example file in:

Code:
1234567890 hi mum
1422973423 squiggle meep meep
9100072322 arparp
*1v00qwzzy arqy
985343jjhj ppparp
997322hhhh hhhhhhhhhh hhhhhh hhhhhh hhhh
01h0000000 aaaa aaa aaa
9121517bet arg
example file out:

Code:
42973423 squiggle meep meep
10072322 arparp
*500
100qwzzy arqy
8343jjhj ppparp
9322hhhh hhhhhhhhhh hhhhhh hhhhhh hhhh
*500
10000000 aaaa aaa aaa
11517bet arg
*500

hope this makes sense to you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top