1
# Nexae Linux servers
##
subparent Nexas Nexae_NIX Linux
title Nexae Linux servers

group-eorted NFe zluetere
10.48.37.67 zintxzpanae01       # teetip NOzOLUMNe:info,trende
10.59.31.67 etrtxzpanae01       # teetip NOzOLUMNe:info,trende

group-sorted ZBTS

10.48.37.11 zinpl001z3a01       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.12 zinpl001z3a02       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.13 zinpl001z3a03       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.14 zinpl001z3a04       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.41 zinpl001z3b01       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.42 zinpl001z3b02       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.31 zinvl201a3001       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.32 zinvl201a3002       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.33 zinvl201a3003       #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.150 zinvl201a3004      #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.151 zinvl201a3005      #"TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.152    zinvl201a3006   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.20 zinvl201a3007       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.39 zinvl201a3008       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.65 zinvl201a3009       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.66 zinvl201a3010       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.70 zinvl201a3011       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.158    zinvl201a3012   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.48.37.34     zinvl201a3013   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"

group-sorted NTTA

10.59.31.11 etrpl001z1a01       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.12 etrpl001z1a02       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.13 etrpl001z1a03       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.14 etrpl001z1a04       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.31 etrvl201a1001       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.32 etrvl201a1002       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.33 etrvl201a1003       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.150    etrvl201a1004   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.151    etrvl201a1005   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.152    etrvl201a1006   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.44 etrvl201a1008       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.47 etrvl201a1009       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.39 etrvl201a1010       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.65 etrvl201a1011       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.66 etrvl201a1012       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
10.59.31.70 etrvl201a1013       # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"
170.252.208.216 etrvl201a1007   # "TRENDe:*,netetat:netetat|netetat1|netetat2|netetat3"

#######################################################
##

Above is the example of HO file. I'm using below sed code to match the keyword group-sorted NTTA in file ho and then insert all the entries from file t4 into ho.

sed -i.bak '/^group-sorted NTTA$/!b;N;N;r t4' ho

But the keyword group-sorted NTTA appears in file 15 times, so if I want to insert the entries after middle keyword. Can I do that with sed?

  • Thanks Inian for nice editing , now it looks like a question. – Varun Dogra Jan 8 '18 at 8:07
  • what is the content of t4 file? Also, elaborate your middle keyword relatively to 15 times – RomanPerekhrest Jan 8 '18 at 8:35
0

To insert the contents of file t4 two lines after the n-th occurrence of ^group-sorted NTTA$ in file H0, try:

awk -v n=2 'FNR==NR{t=t"\n"$0;next} /^group-sorted NTTA$/ && n==++cnt {x=NR+3} NR==x{print substr(t,2)} 1' t4 H0

While there undoubtedly is a sed solution to this problem, awk is a more natural choice here because awk understands arithmetic. This, for example, makes it easier to program awk to do something after the n-th occurrence of something where n is a variable.

How it works

  • -v n=2

    This sets the value of n.

  • FNR==NR{t=t"\n"$0;next}

    When reading the first-named file, t4, this saves its contents in variable t.

  • /^group-sorted NTTA$/ && n==++cnt {x=NR+3}

    While reading the second-named file, H0, the number of occurrences of ^group-sorted NTTA$ is saved in variable cnt. If cnt is equal to n, then we assign variable x to the value of the current line number, NR, plus 3.

  • NR==x{print substr(t,2)}

    When we have reached line number x, we print the contents of variable t. (The substr command removes a superfluous leading newline character.)

  • 1

    This is awk's shorthand for print-the-current-line.

Example

Consider these input files:

$ cat H0
blah, blah
group-sorted ZBTS

10.48.37.11 zinpl001z3a01 #"TRENDe
10.48.37.12 zinpl001z3a02 #"TRENDe

group-sorted NTTA

10.59.31.11 etrpl001z1a01       # "TRENDe
10.59.31.12 etrpl001z1a02       # "TRENDe
10.59.31.13 etrpl001z1a03       # "TRENDe

group-sorted NTTA

10.59.31.14 etrpl001z1a04       # "TRENDe
10.59.31.31 etrvl201a1001       # "TRENDe
10.59.31.32 etrvl201a1002       # "TRENDe

group-sorted NTTA

10.59.31.33 etrvl201a1003       # "TRENDe
10.59.31.150    etrvl201a1004       # "TRENDe
10.59.31.151    etrvl201a1005       # "TRENDe

And:

$ cat t4
line 1 of t4
line 2 of t4

The output from our command is:

$ awk -v n=2 'FNR==NR{t=t"\n"$0;next} /^group-sorted NTTA$/ && n==++cnt {x=NR+3} NR==x{print substr(t,2)} 1' t4 H0
blah, blah
group-sorted ZBTS

10.48.37.11 zinpl001z3a01 #"TRENDe
10.48.37.12 zinpl001z3a02 #"TRENDe

group-sorted NTTA

10.59.31.11 etrpl001z1a01       # "TRENDe
10.59.31.12 etrpl001z1a02       # "TRENDe
10.59.31.13 etrpl001z1a03       # "TRENDe

group-sorted NTTA

10.59.31.14 etrpl001z1a04       # "TRENDe
line 1 of t4
line 2 of t4
10.59.31.31 etrvl201a1001       # "TRENDe
10.59.31.32 etrvl201a1002       # "TRENDe

group-sorted NTTA

10.59.31.33 etrvl201a1003       # "TRENDe
10.59.31.150    etrvl201a1004       # "TRENDe
10.59.31.151    etrvl201a1005       # "TRENDe
  • 1
    Tht's really a nice explanation thanks, let me check this in my script..give me sometime. – Varun Dogra Jan 8 '18 at 9:16
0

Input file1

cat H0

blah, blah
group-sorted ZBTS

10.48.37.11 zinpl001z3a01 #"TRENDe
10.48.37.12 zinpl001z3a02 #"TRENDe

group-sorted NTTA

10.59.31.11 etrpl001z1a01       # "TRENDe
10.59.31.12 etrpl001z1a02       # "TRENDe
10.59.31.13 etrpl001z1a03       # "TRENDe

group-sorted NTTA

10.59.31.14 etrpl001z1a04       # "TRENDe
10.59.31.31 etrvl201a1001       # "TRENDe
10.59.31.32 etrvl201a1002       # "TRENDe

group-sorted NTTA

10.59.31.33 etrvl201a1003       # "TRENDe
10.59.31.150    etrvl201a1004       # "TRENDe
10.59.31.151    etrvl201a1005       # "TRENDe

group-sorted NTTA

10.59.31.33 etrvl201a1003       # "TRENDe
10.59.31.150    etrvl201a1004       # "TRENDe
10.59.31.151    etrvl201a1005       # "TRENDe

Input file2

cat t4

10
20

Command

 c=`sed -n -e '/group-sorted NTTA/{=;p;}' H0| sed "N;s/\n/ /g" | awk '{print $1}' | sed -n ''$y'p' | awk '{print 2 + $1}'`;sed ''$c'r t4' H0

Output

blah, blah
group-sorted ZBTS

10.48.37.11 zinpl001z3a01 #"TRENDe
10.48.37.12 zinpl001z3a02 #"TRENDe

group-sorted NTTA

10.59.31.11 etrpl001z1a01       # "TRENDe
10.59.31.12 etrpl001z1a02       # "TRENDe
10.59.31.13 etrpl001z1a03       # "TRENDe

group-sorted NTTA

10.59.31.14 etrpl001z1a04       # "TRENDe
10
20
10.59.31.31 etrvl201a1001       # "TRENDe
10.59.31.32 etrvl201a1002       # "TRENDe

group-sorted NTTA

10.59.31.33 etrvl201a1003       # "TRENDe
10.59.31.150    etrvl201a1004       # "TRENDe
10.59.31.151    etrvl201a1005       # "TRENDe

group-sorted NTTA

10.59.31.33 etrvl201a1003       # "TRENDe
10.59.31.150    etrvl201a1004       # "TRENDe
10.59.31.151    etrvl201a1005       # "TRENDe
  • Thanks for replying, but i'll go with the awk one, it's working. Though it is editing the same file. – Varun Dogra Jan 11 '18 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.