Shell script

Hi Lucas_0418,
Thank you so much for your quick reply.If z is always a string what's the use of if block and can u explain what this line mean p=p?RD A [i]: A [i]and what's the use of found.

Am New to shell script.But trying really hard to understand this.

One more thing actually in gsub we will replace search pattern,but we are replacing with x. It means bla-bla-bla-01 will become bla-bla-bla-x and stores it into s.where will num of substitutions value get stored i.e 2 as in your eg

Thanks in advance,
Preethy

---------- Post updated at 12:06 PM ---------- Previous update was at 12:02 PM ----------

Hi Lucas_0418,
Can u explain me about this line
P=P? P FS A [i]: A

[i]Thanks in advance,
preethy

For first question:
z is a string, the use of if block if(!(z in S)) is to check if z is an index of an element in Array S, just like what I've told above. If S has an element S["aa"], and z="aa", we say (z in S) is true,
so if(!(z in S)) condition is false.
p=p?RD A [i]: A[i]: this is a three meta compute.
Separate it to p= "p" ? "RD A[i]" : "A[i]"
the part between "equal to" and "question mark" is a expression, it can be true or false.
If it's true, p's value will be set to the part between "question mark" and "colon" in example "RD A[i]".
And if it is false, p's value will be set to the last part: "A[i]"
And, hah, found is also a array's name, not some special thing. In the code you'll find "found[++m]=z"

For second question:
When use gsub, use it like this to store the value in another variable.
len=gsub(......)
If you don't store it manually, it losts.

I'm so sleepy, going to sleep now, bye!!

Hi Lucas_0418,
I think u can't get me.if s[5] is an array we can say that z takes values 0-4 but here how the array s is formed without any declaration and initializationand what are the values it's index takes.I know about working of conditional operator but what I wanted to know is way does it mean by p fieldspecifier A

[i]Thanks,
preethy

Sorry guy. I did get you.
array in awk doesn't need to initial, it is unadorn

if(!(z in S)){
 S[z] = $0

Why if, it make sure S[z]'s value would only be set once.

Why

s= "s" ? "s FS A" : "A"

You know FS is field separator.
When s is not set, first time this code be executing.
s only need the content of A[i].
But after the first time, for separate pre value and a new A[i].
It use FS, that is why s=sFSA [i]for all times except the very first time

And, I am sorry for my mistake recently.

Hi Lucas_0418,
can u just say a word what the if else block is doing and what for the end block is used.while analyzing this code I got to know if else is used for summing the fields of all lines which have got the same scope name where as end block will occur only at the end to print all the lines

Thanks,
preethy

---------- Post updated at 09:24 PM ---------- Previous update was at 08:48 PM ----------

Hi Lucas_0418,
finally what { print ; next }
!/Total/
it does is prints first line and recursively deletes all the lines containing total.But which line it will point aftet that.Plz provide me a clarification regarding replacing with x here in gsub

Thanks,
preethy

Hi preethy,
I am so sorry for my a lot of explanations that is not your expected.

And you have already got it ( For the use of if else block and the END block, your understanding totally right).
Now for
finally what {

 print ; next } 
!/Total/

The two sentences above are not relative.

Let us look at them in the total code.

NR == 1 { print ; next } 
                 !/Total/{
					  z = $3
					  gsub(/-[0-9]+$/,x,z)
			  if(!(z in S)){
					    ...
				       }
				  else {
						... 
				       }				
			  }

{ print ; next } this would be executed only one time.(When NR==1, you also can say it the first line in 'file')
( print: print this line, it's equal to print $0 )
( next: do not execute the command after 'next' for current line, read next line, this is what you are asking, after this , it points to the second line. NR=2,
then back to beginning of the total code)

!/Total/{
...
}
If current line does not have a patter /Total/, step in the '{}' after the !/Total/.

After all, the meaning of gsub.
Assuming this line is

20131203170000,171.99.192.0/23,SRT-PKT-ActiveSubCPE-13,87,509,0,66,399,44,0,0,0,0,0,0,"

z = $3(z is set to "SRT-PKT-ActiveSubCPE-13")
gsub(/-[0-9]+$/,x,z) (Do you remember if a variable in awk has not been set,
the value of it is "", in this command, variable x is "",
"-13" is a /-[0-9]+$/ pattern, it would be substituted by ""(value of x )
so, after substitute, z is set to "SRT-PKT-ActiveSubCPE")

Hi lucas_0418,
for(k in S)
{
if(k == found[q])
{
}
}
k is "" here found[] consists all the values of z,how can if block gets executed if we are comparing null value to some string in found[].
Hope you dont feel like spoon feeding.
Thanks,
preethy

preethy and Lucas_0418 kindly use [CODE] tags and [ICODE] when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the code button in the editing menu. don't just discuss like chat.

kindly note this is a professional forum dedicated to the open and universal exchange of information. Please help keep the quality of the posts very high for the entire world community.

Thanks,

Regards,
Akshay

See if this clarifies your doubt

#!/bin/bash

awk '
  BEGIN{

   S["a"]="apple"
   S["b"]="bat"
   S["c"]="cat"

   for(k in S){
     print "Index: ", k
     print "Value: ", S[k]
   }
  }'

Output

root@maximus:/tmp# ./run
Index:  a
Value:  apple
Index:  b
Value:  bat
Index:  c
Value:  cat

k serves as an index here.

--ahamed

1 Like

Hi preethy,
It doesn't matter, my English is not very good, and I am on working now, if my reply came slowly, please don't mind.

ahamed's reply may answer your question too, thanks then, ahamed.

You have already known that found[] consists all the values of z.
But why you think k's value can be null ?

For every element value of z in array found.
We also push element index of z in array S.

found[++m] = z
S[z] = $0
for(q = 1; q <= m; q++)
for ( k in S )

k is index of array S.
So every k could make condition ( if(k==found[q]) ) be true once, right ?
k's value may not be null, and of cause if it is, ( if(k==found[q]) ) would be false.

Hi lucas_0418,
Thanks a lot for letting me understand this even though your on work.
Regards,
preethy

---deleted---

Hi Lucas_0418,
!/Total/ this is skipping even the line containing string "total-abcd-01" and "acc-Total-09".But i want to skip only the lines containing the string "Total".
Thanks & Regards,
preethy.