|
121 | 121 |
|
122 | 122 | $ echo "$s" | awk '{patsplit($0, a, /"[^"]*"|[^,]*/); print a[2]}'
|
123 | 123 | "fox,42"
|
124 |
| -</code></pre><h2 id=substr><a class=header href=#substr>substr</a></h2><p>The <code>substr</code> function helps to extract a specified number of characters from an input string based on indexing. The argument order is:<ul><li>First argument is the input string<li>Second argument is the starting position<li>Third argument is the number of characters to extract</ul><p>The index starts from <code>1</code>. If the third argument is not specified, by default all characters until the end of the string is extracted. If the second argument is greater than the length of the string or if the third argument is less than or equal to <code>0</code>, then an empty string is returned. The second argument will be converted <code>1</code> if a number less than one is specified.<pre><code class=language-bash>$ echo 'abcdefghij' | awk '{print substr($0, 1, 5)}' |
| 124 | +</code></pre><h2 id=substr><a class=header href=#substr>substr</a></h2><p>The <code>substr</code> function helps to extract a specified number of characters from an input string based on indexing. The argument order is:<ul><li>First argument is the input string<li>Second argument is the starting position<li>Third argument is the number of characters to extract</ul><p>The index starts from <code>1</code>. If the third argument is not specified, by default all characters until the end of the string is extracted. If the second argument is greater than the length of the string or if the third argument is less than or equal to <code>0</code>, then an empty string is returned. The second argument will be converted to <code>1</code> if a number less than one is specified.<pre><code class=language-bash>$ echo 'abcdefghij' | awk '{print substr($0, 1, 5)}' |
125 | 125 | abcde
|
126 | 126 | $ echo 'abcdefghij' | awk '{print substr($0, 4, 3)}'
|
127 | 127 | def
|
|
182 | 182 | # end of string
|
183 | 183 | $ awk -v s="a+b" 'index($0, s)==length()-length(s)+1' eqns.txt
|
184 | 184 | i*(t+9-g)/8,4-a+b
|
185 |
| -</code></pre><p>Recall that the <code>-v</code> option gets parsed by <code>awk</code>'s string processing rules. So, if you need to pass a literal string without falling in backslash hell, use <code>ENVIRON</code> instead.<pre><code class=language-bash>$ echo 'a\b\c\d' | awk -v s='a\b' 'index($0, s)' |
186 |
| -$ echo 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)' |
| 185 | +</code></pre><p>Recall that the <code>-v</code> option gets parsed by <code>awk</code>'s string processing rules. So, if you need to pass a literal string without falling in backslash hell, use <code>ENVIRON</code> instead.<pre><code class=language-bash>$ printf '%s\n' 'a\b\c\d' | awk -v s='a\b' 'index($0, s)' |
| 186 | +$ printf '%s\n' 'a\b\c\d' | awk -v s='a\\b' 'index($0, s)' |
187 | 187 | a\b\c\d
|
188 |
| -$ echo 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])' |
| 188 | +$ printf '%s\n' 'a\b\c\d' | s='a\b' awk 'index($0, ENVIRON["s"])' |
189 | 189 | a\b\c\d
|
190 | 190 | </code></pre><h2 id=system><a class=header href=#system>system</a></h2><p>External commands can be issued using the <code>system</code> function. Any output generated by the external command would be as usual on <code>stdout</code> unless redirected while calling the command.<pre><code class=language-bash>$ awk 'BEGIN{system("echo Hello World")}'
|
191 | 191 | Hello World
|
|
318 | 318 | Er,56,79,92
|
319 | 319 | Ort,68,72,66
|
320 | 320 | Blue,67,46,99
|
321 |
| -</code></pre><p><strong>2)</strong> For the input file <code>nums3.txt</code>, calculate the square root of numbers and display the results in two different formats as shown below. First, with four digits after the fractional point and then in the scientific notation, again with four digits after the fractional point. Assume that the input has only a single column of positive numbers.<pre><code class=language-bash>$ cat nums3.txt |
| 321 | +</code></pre><p><strong>2)</strong> For the input file <code>nums3.txt</code>, calculate the square root of numbers and display the results in two different formats as shown below. First, with four digits after the fractional point and then in the scientific notation, again with four digits after the fractional point. Assume that the input has only a single column of positive numbers.<pre><code class=language-bash>$ cat nums3.txt |
322 | 322 | 3.14
|
323 | 323 | 4201
|
324 | 324 | 777
|
|
359 | 359 |
|
360 | 360 | $ s='\&/'
|
361 | 361 | # should be no output for this input
|
362 |
| -$ echo 'f\&z\&2.14' | ##### add your solution here |
| 362 | +$ printf '%s\n' 'f\&z\&2.14' | ##### add your solution here |
363 | 363 | # but this one has a match
|
364 |
| -$ echo 'f\&z\&/2.14' | ##### add your solution here |
| 364 | +$ printf '%s\n' 'f\&z\&/2.14' | ##### add your solution here |
365 | 365 | \&/2.14
|
366 | 366 | </code></pre><p><strong>6)</strong> Extract all positive integers preceded by <code>-</code> and followed by <code>:</code> or <code>;</code>. Display the matching portions separated by a newline character.<pre><code class=language-bash>$ s='42 apple-5; fig3; x-83, y-20:-34; f12'
|
367 | 367 | $ echo "$s" | awk ##### add your solution here
|
|
0 commit comments