4

I am trying to remove all characters after particular match pattern i.e nfin to the end of line.

sample_text

xxsac00mi126 vddai:f1042 tkeept_iph xsac00.f1040 vdda lvtpfet m=1 nf=1 nfin=2 pdej=1.6e-07 asej=3.2e-16 lrsd=4e-08 pre_layout_local=0 

Command

sed -i "s/nfin\=[0-9].*//g" sample_text

Desired Output

xxsac00mi126 vddai:f1042 tkeept_iph xsac00.f1040 vdda lvtpfet m=1 nf=1 nfin=2  

3 Answers 3

5

Try this sed instead:

sed -E 's/ pdej.*$//'

Example:

sed -E 's/ pdej.*$//' file.txt 
xxsac00mi126 vddai:f1042 tkeept_iph xsac00.f1040 vdda lvtpfet m=1 nf=1 nfin=2

Where file.txt contains:

cat file.txt 
xxsac00mi126 vddai:f1042 tkeept_iph xsac00.f1040 vdda lvtpfet m=1 nf=1 nfin=2 pdej=1.6e-07 asej=3.2e-16 lrsd=4e-08 pre_layout_local=0


OR:
If you want (need) to use nfin as an anchor of sorts, use:

sed -E 's/(.*nfin=[0-9]).*/\1/' file.txt 
xxsac00mi126 vddai:f1042 tkeept_iph xsac00.f1040 vdda lvtpfet m=1 nf=1 nfin=2
3
  • I was looking for nfin as anchor mainly. The above command works fine ; can I use inline argument ? Feb 15, 2017 at 8:19
  • If you want to match the last "nfin" use (.*nfin=...) -- if you want to match the first "nfin", omit the leading .* -> (nfin=...) Feb 16, 2017 at 14:43
  • Answer can be improved by making more self-documenting by replacing -E with or adding --regexp-extended in examples. Apr 22, 2020 at 8:40
2
sed -i "/nfin=[0-9]/{s//&\n/;P;d;}" sample_text

will remove the rest of line after nfin=[0-9]
For line(s) with nfin=[0-9]:

  • put \newline after the pattern (&)
  • Print line before \newline
  • delete line (print any more)
1
  • 2
    Could you please explain a bit, why your answer solves the problem? This is a knowledge base, so other users might benefit from it. Feb 15, 2017 at 9:40
1
sed -ne 's/[ ]nfin=[^ ]*/&\n/;P' < your_spice_netlist

But you have to keep in mind that spice netlists can have instances('x') or for that matter any element cross line boundaries and in such a scenario the above sed code might not work as maybe the nfin parameter might lie at the beginning of a line, like as, +nfin=23. Also, sometimes 'nfin' might appear parameterized, e.g., nfin='width*0.5 + 1./nfac*len', whereby the nfin=[0-9] will not match.

However if you can assure that there are no multilines (at least for instances) then the above should work reliably. Also there be no TABs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .