{"id":413,"date":"2019-03-10T23:45:51","date_gmt":"2019-03-11T06:45:51","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=413"},"modified":"2021-11-12T13:31:12","modified_gmt":"2021-11-12T21:31:12","slug":"perl-script-to-write-line-containing-specific-word-and-next-second-and-third-lines","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/03\/perl-script-to-write-line-containing-specific-word-and-next-second-and-third-lines\/","title":{"rendered":"#62 Perl script to write line containing specific word and next second and third lines"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!usr\/bin\/perl\r\nmy $newfile = 'cleaned.csv';\r\nopen(my $newfh, '&gt;', $newfile) or die &quot;Can't read file '$newfile' &#x5B;$!]\\n&quot;;\r\nmy $file = 'original.csv';\r\nmy @data;\r\nopen(my $fh, '&lt;', $file) or die &quot;Can't read file '$file' &#x5B;$!]\\n&quot;;\r\nwhile (my $line = &lt;$fh&gt;) {\r\n    chomp $line;\r\n    my @fields = split(\/,\/, $line);\r\n\tif ($fields&#x5B;4] eq &quot;Control Line&quot;){\r\n\t\t$line2=&lt;$fh&gt;; #second line\r\n\t\t$line3=&lt;$fh&gt;; #third line\r\n\t\tprint $newfh &quot;$line\\n&quot;;\r\n\t\tprint $newfh &quot;$line2&quot;;\r\n\t\tprint $newfh &quot;$line3&quot;;\r\n\t}else{\r\n\t\tprint &quot;$line\\n&quot;;\r\n\t}\r\n}\r\nclose ($fh);\r\nclose ($newfh);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!usr\/bin\/perl my $newfile = &#8216;cleaned.csv&#8217;; open(my $newfh, &#8216;&gt;&#8217;, $newfile) or die &quot;Can&#8217;t read file &#8216;$newfile&#8217; &#x5B;$!]\\n&quot;; my $file = &#8216;original.csv&#8217;; my @data; open(my $fh, &#8216;&lt;&#8216;, $file) or die &quot;Can&#8217;t read file &#8216;$file&#8217; &#x5B;$!]\\n&quot;; while (my $line = &lt;$fh&gt;) { chomp $line; my @fields = split(\/,\/, $line); if ($fields&#x5B;4] eq &quot;Control Line&quot;){ $line2=&lt;$fh&gt;; #second line $line3=&lt;$fh&gt;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[31],"tags":[],"class_list":["post-413","post","type-post","status-publish","format-standard","hentry","category-perl"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-6F","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1512,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/06\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":413,"position":0},"title":"#286 Creates a fillet between two lines using HyperMesh Tcl","author":"gantovnik","date":"2022-06-24","format":false,"excerpt":"#285 Find min and max lengths of lines in HyperMesh database [code language=\"python\"] *createfillet line1 id line2 id radius trim quadselected [\/code] line1 id = The ID of the first line to which the fillet line is to be created. line2 id = The ID of the second line to\u2026","rel":"","context":"In &quot;HyperMesh&quot;","block_context":{"text":"HyperMesh","link":"https:\/\/gantovnik.com\/bio-tips\/category\/hypermesh\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":432,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/07\/windows-batch-script-to-print-nth-lines-after-line-containing-matched-string\/","url_meta":{"origin":413,"position":1},"title":"#63 Batch script to print n-th lines after line containing matched string","author":"gantovnik","date":"2019-07-26","format":false,"excerpt":"[code language=\"python\"] @echo off setlocal enableDelayedExpansion set \"word=VON MISES\" set \"keep=3\" set \"file=model.f06\" find \" X \" model.f06 > extract_weight.txt del extract_stress.txt set n=%keep% for \/f \"usebackq delims=\" %%A in (\"%file%\") do ( set \"ln=%%A\" if \"!ln:%word%=!\" neq \"!ln!\" ( echo( set n=0 ) if !n! lss !keep! ( echo\u2026","rel":"","context":"In &quot;batch&quot;","block_context":{"text":"batch","link":"https:\/\/gantovnik.com\/bio-tips\/category\/batch\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":783,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/11\/135-global-namespace-in-tcl\/","url_meta":{"origin":413,"position":2},"title":"#135 Global namespace in tcl","author":"gantovnik","date":"2020-11-12","format":false,"excerpt":"#135 Global namespace in tcl It is possible to reference global variables inside procedures using global namespace indicator: [code language=\"python\"] set ::element_list \"1 2 3 4 5\"; proc ::element_info {} { puts $::element_list; set ::node_list \"10 20 30 40\"; puts $::node_list; } element_info; 1 2 3 4 5 10 20\u2026","rel":"","context":"In &quot;HyperMesh&quot;","block_context":{"text":"HyperMesh","link":"https:\/\/gantovnik.com\/bio-tips\/category\/hypermesh\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":671,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/114-python-script-for-remove-offsets-from-cbar-elements\/","url_meta":{"origin":413,"position":3},"title":"#114: Python script for remove offsets from CBAR elements","author":"gantovnik","date":"2020-09-28","format":false,"excerpt":"#114: Python script for remove offsets from CBAR elements SOL 106 does not allow offsets in CBAR elements. [code language=\"python\"] print \"-------------------------------------\" print \"remove_cbush_offsets_v1.0\" print \"Vladimir Gantovnik\" print \"March 15 2016\" print \"-------------------------------------\" #input file inName='test.bdf' inFile=open(inName) #output file outName='stripped.bdf' outFile=open(outName,'w') import string while 1 : line=inFile.readline() if not line\u2026","rel":"","context":"In &quot;nastran&quot;","block_context":{"text":"nastran","link":"https:\/\/gantovnik.com\/bio-tips\/category\/nastran\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":867,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/156-filtering-content-from-files-using-awk\/","url_meta":{"origin":413,"position":4},"title":"#156 Filtering content from files using awk","author":"gantovnik","date":"2021-02-09","format":false,"excerpt":"#156 Displaying and filtering the content of files with awk Using the following command, we can print all lines from the file: [code language=\"python\"] awk ' { print } ' \/etc\/passwd [\/code] This is equivalent to using the $0 variable. The $0 variables refers to the complete line. [code language=\"python\"]\u2026","rel":"","context":"In &quot;awk&quot;","block_context":{"text":"awk","link":"https:\/\/gantovnik.com\/bio-tips\/category\/awk\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1540,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/09\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3\/","url_meta":{"origin":413,"position":5},"title":"#296 y-y plot in python","author":"gantovnik","date":"2022-09-02","format":false,"excerpt":"y-y plots are a type of line plot where one line corresponds to one y-axis, and another line on the same plot corresponds to a different y-axis. y-y plots typically have one vertical y-axis on the left edge of the plot and one vertical y-axis on the right edge of\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/09\/ex295.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=413"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}