Hi All,
I am reading a line from a file and writing it to other file.
Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is
U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7 pin27F connector8 pin26F connector9 pin25F connector10 pin24F connector11 pin23F connector0 pin21E connector12 pin22F connector1 pin22E connector13 pin21F connector2 pin23E
I want above line to be splitted into four line as below.
1.Every line will start from first name U_ABC.
2.First line will have connection0 and its subsequent pin number,connection1 and its subsequent pin number, connection2 and its subsequent pin.
3. Same for other three line in seq.
4. Connection7 and connection13 along with their pin number should be not be included.
I can split it into four line as
awk '{for (i = 1; i <= NF; i += 3) printf "%d %d %d\n", $i, $(i+1), $(i+2)}' file
but dont no how to write it in sequence as above. Do I need to save it in some form of dictionary first?
Thanks
Assuming that you really want to translate "connectorX" from your input to "connectionX" in your output, and that you might want to feed multiple lines into this script, the following seems to do what you want:
awk '
{ mc = 0
for(i = 2; i < NF; i += 2) {
c = substr($i, 10) + 0
if(c != 7 && c != 13) {
p[c] = $(i + 1)
if(c > mc) mc = c
}
}
lc = 0
for(i = 0; i <= mc; i++)
if(i in p) {
if(lc++ == 0) o = $1
o = o " connection" i " " p
if(lc == 3) {
print o
lc = 0
}
delete p
}
if(lc) print o
}' file
This could be simplified slightly if you will always only process one line at a time.
If each line to be processed always has connector0 through connector12 (except for connector7) and connector7 and connector13 might also appear, but no others, it could be simplified some more. I will leave those simplifications as an exercise for the reader.
With your sample input, this script produces:
U_ABC connection0 pin21E connection1 pin22E connection2 pin23E
U_ABC connection3 pin24E connection4 pin25E connection5 pin26E
U_ABC connection6 pin27E connection8 pin26F connection9 pin25F
U_ABC connection10 pin24F connection11 pin23F connection12 pin22F
1 Like
Thanks a lot!!!!!!!
That will solve my problem.