<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: Perl one-liner for beginners !]]></title>
	<link>https://bioinformaticsonline.com/blog/view/42003/perl-one-liner-for-beginners?</link>
	<atom:link href="https://bioinformaticsonline.com/blog/view/42003/perl-one-liner-for-beginners?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/blog/view/42003/perl-one-liner-for-beginners</guid>
	<pubDate>Fri, 24 Jul 2020 05:58:28 -0500</pubDate>
	<link>https://bioinformaticsonline.com/blog/view/42003/perl-one-liner-for-beginners</link>
	<title><![CDATA[Perl one-liner for beginners !]]></title>
	<description><![CDATA[<p>I often use the following arguments to perl:</p><ul>
<li>-e Makes the line of code be executed instead of a script</li>
<li>-n Forces your line to be called in a loop. Allows you to take lines from the diamond operator (or stdin)</li>
<li>-p Forces your line to be called in a loop. Prints $_ at the end</li>
</ul><p>&nbsp;</p><ul>
<li>This counts the number of quotation marks in each line and prints it
<div>
<blockquote>
<div>perl -ne&nbsp;'$cnt = tr/"//;print "$cnt\n"'&nbsp;inputFileName.txt</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>Adds string to each line, followed by tab
<div>
<blockquote>
<div>perl -pe&nbsp;'s/(.*)/string\t$1/'&nbsp;inFile &gt; outFile</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>Append a new line to each line
<div>
<blockquote>
<div>perl -pe&nbsp;'s//\n/'&nbsp;all.sent.classOnly &gt; all.sent.classOnly.sep</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>Replace all occurrences of pattern1 (e.g. [0-9]) with pattern2
<div>
<blockquote>
<div>perl -p -i.bak -w -e&nbsp;'s/pattern1/pattern2/g'&nbsp;inputFile</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>Go through file and only print words that do not have any uppercase letters.
<div>
<blockquote>
<div>perl -ne&nbsp;'print unless m/[A-Z]/'&nbsp;allWords.txt &gt; allWordsOnlyLowercase.txt</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>Go through file, split line at each space and print words one per line.
<div>
<blockquote>
<div>perl -ne&nbsp;'print join("\n", split(/ /,$_));print("\n")'&nbsp;someText.txt &gt; wordsPerLine.txt</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>or in other words, delete every character that is not a letter, white space or line end (replace with nothing)
<div>
<blockquote>
<div>perl -pne&nbsp;'s/[^a-zA-Z\s]*//g'&nbsp;text_withSpecial.txt &gt; text_lettersOnly.txt</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>
<div>
<div>perl -pne&nbsp;'tr/[A-Z]/[a-z]/'&nbsp;textWithUpperCase.txt &gt; textwithoutuppercase.txt;</div>
</div>
</li>
</ul><ul>
<li>Print only the second column of the data when using tabular as a separator
<div>
<blockquote>
<div>perl -ne&nbsp;'@F = split("\t", $_); print "$F[1]";'&nbsp;columnFileWithTabs.txt &gt; justSecondColumn.txt</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>
<div>One-Liner: Sort lines by their length
<blockquote>
<div>perl -e&nbsp;'print sort {length $a &lt;=&gt; length $b} &lt;&gt;'&nbsp;textFile</div>
</blockquote>
</div>
</li>
</ul><ul>
<li>One-Liner: Print second column, unless it contains a number
<blockquote>
<div>perl"&gt;perl -lane&nbsp;'print $F[1] unless $F[1] =~ m/[0-9]/'&nbsp;wordCounts.txt</div>
</blockquote>
</li>
</ul>]]></description>
	<dc:creator>BioStar</dc:creator>
</item>

</channel>
</rss>