2023-01-24T03:33:47+00:00https://www.redditstatic.com/icon.png//r/awk.rssAWK - programming language by Aho, Weinberger and KernighanAWK/u/mmkodalihttps://www.reddit.com/user/mmkodali<!-- SC_OFF --><div class="md"><p>i am new to using awk.</p> <p>i have written a simple one liner script to calculate memory consumed by a program as below.</p> <p>```</p> <h1>!/usr/bin/env bash</h1> <p>read -p &quot;enter process name: &quot; item;</p> <p>ps aux | awk -vi=$item &#39;/i/ {sum += $6} END { printf &quot;Memory Usage: %d MB\n&quot;, sum/1024 }&#39;</p> <p>```</p> <p>in the above example, variable &#39;i&#39; is not substituted when executing script.</p> <p>where i am going wrong?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/mmkodali"> /u/mmkodali </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/10jb47y/substituting_awk_variable_in_a_bash_script/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/10jb47y/substituting_awk_variable_in_a_bash_script/">[comments]</a></span>t3_10jb47y2023-01-23T12:16:56+00:002023-01-23T12:16:56+00:00substituting awk variable in a bash script/u/exquisitesunshinehttps://www.reddit.com/user/exquisitesunshine<!-- SC_OFF --><div class="md"><p>I have an <a href="https://0x0.st/ohns.txt">xml file</a> that I would like transform to this <a href="https://0x0.st/ohnz.txt">xml file</a> in a shell script using awk. The resulting <code>diff</code> is:</p> <pre><code>2,3c2 &lt; &lt;name&gt;debian-11-test&lt;/name&gt; &lt; &lt;uuid&gt;4ade684e-ce3e-4746-8292-528a84b98445&lt;/uuid&gt; --- &gt; &lt;name&gt;debian-11-test-1&lt;/name&gt; 38c37 &lt; &lt;source file=&#39;/tmp/vm/debian-11-test.qcow2&#39;/&gt; --- &gt; &lt;source file=&#39;/tmp/vm/debian-11-test-1.qcow2&#39;/&gt; 88d86 &lt; &lt;mac address=&#39;52:54:00:14:fa:09&#39;/&gt; 89a88 &gt; &lt;ip address=&#39;192.168.122.11&#39; prefix=&#39;24&#39;/&gt; </code></pre> <p>Looking for a mini awk script or command that can do this summary of the changes:</p> <ul> <li><p>In the line containing <code>&lt;name&gt;debian-11-test&lt;/name&gt;</code>, replace with <code>&lt;name&gt;${host}&lt;/name&gt;</code> where <code>$host</code> is a shell variable with the resulting string to be placed in the xml file.</p></li> <li><p>Delete the line with <code>&lt;uuid&gt;</code> and <code>&lt;/uuid&gt;</code>, ideally only deleting the first matching beginning from the above <code>&lt;name&gt;&lt;/name&gt;</code> line or at least deleting the first match found in the file only.</p></li> <li><p>Same as the first change: want find line containing <code>&lt;source file=&#39;/tmp/vm/debian-11-test.qcow2&#39;/&gt;</code> and replace with <code>&lt;source file=&#39;/tmp/vm/${host}.qcow2&#39;/&gt;</code>.</p></li> <li><p>Same as second change: delete the line with <code>&lt;mac address=&#39;52:54:00:14:fa:09&#39;/&gt;</code>, ideally only deleting the first match beginning with the line containing <code>&lt;interface type=&#39;network&#39;&gt;</code> or at least deleting the first match found in the file only.</p></li> <li><p>Finally, add a new line <code>&lt;ip address=&#39;192.168.122.$cnt&#39; prefix=&#39;24&#39;/&gt;</code> after the line matching <code>&lt;interface type=&#39;network&#39;&gt;</code> and then exiting immediately.</p></li> </ul> <p>Much appreciated. I should be able to learn from suggestions and tweak if they don&#39;t do exactly the above.</p> <p>P.S. I&#39;m aware of tools like <code>virt-sysprep</code> to prepare the VM image but they are for prepping a base image whereas I want to do bake these changes into the VM image so they are generated fresh every time without requiring a clean base image that needs to be maintained.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/exquisitesunshine"> /u/exquisitesunshine </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/10j4cgc/append_to_first_matched_line_only_delete_first/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/10j4cgc/append_to_first_matched_line_only_delete_first/">[comments]</a></span>t3_10j4cgc2023-01-23T05:00:20+00:002023-01-23T05:00:20+00:00Append to first matched line only, delete first matched line beginning from a matched line/u/Pure_Needleworker536https://www.reddit.com/user/Pure_Needleworker536<!-- SC_OFF --><div class="md"><p>The relevant piece of awk code:</p> <pre><code>comm = n ? substr($0, m+1, n-m-1) : substr($0, m+1) jump = n ? substr($0, n+1) : 0 print comm printf(&quot;comm %s; jump %s;\n&quot;, comm, jump) </code></pre> <p>yields the output</p> <pre><code>A ; jump 0 D+A ; jump 0 D jump 0 </code></pre> <p>with both gawk and mawk. Why is the value of comm disappearing in between the print and printf statement? Why isn&#39;t even the string literal &quot;comm&quot; within the printf argument being printed?</p> <p>Entire code: <a href="https://pastebin.com/hD6PGFrP">https://pastebin.com/hD6PGFrP</a></p> <p>Input file:</p> <pre><code>// This file is part of www.nand2tetris.org // and the book &quot;The Elements of Computing Systems&quot; // by Nisan and Schocken, MIT Press. // File name: projects/06/add/Add.asm // Computes R0 = 2 + 3 (R0 refers to RAM[0]) @2 D=A @3 D=D+A @0 M=D </code></pre> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Pure_Needleworker536"> /u/Pure_Needleworker536 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/10ifqea/cant_figure_this_behavior_out/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/10ifqea/cant_figure_this_behavior_out/">[comments]</a></span>t3_10ifqea2023-01-22T09:27:03+00:002023-01-22T09:27:03+00:00Can't figure this behavior out/u/tspierhttps://www.reddit.com/user/tspier<!-- SC_OFF --><div class="md"><p>Hi, y&#39;all! I have a file where answers to questions were recorded and are preceded by a number and a right parenthesis, e.g. <em>1)</em> and <em>9)</em>. What I&#39;m trying to do is extract the number, the parenthesis, and the relevant information, i.e. any type of character that appears after the number and parenthesis BUT <em>before</em> the next number and parenthesis. For instance, if I have a file with the following content and then run the subsequent AWK script, it shows everything between 1) and 3). What I want to do is show everything between 1) and 2). Thank you in advance for your help!</p> <pre><code>test.txt 1) good 2) bad 3) ok </code></pre> <p>&#x200B;</p> <pre><code>script.awk awk &#39;/1\)/,/2\)/ { if ($0 ~ /1\)/) { p=1 } if (p) { print } if ($0 ~ /2\)/) { exit } }&#39; test.txt </code></pre> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/tspier"> /u/tspier </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/10hdvyt/splitting_a_file_and_extracting_text_between_two/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/10hdvyt/splitting_a_file_and_extracting_text_between_two/">[comments]</a></span>t3_10hdvyt2023-01-21T00:26:30+00:002023-01-21T00:26:30+00:00Splitting a File and Extracting Text Between Two Strings/u/rocket_186https://www.reddit.com/user/rocket_186<!-- SC_OFF --><div class="md"><p>So the other day at work I was trying to extract data formatted like this:</p> <p>{“5_1”; “3_1”; “2_1”;} (there was a lot more data than this spanning numerous lines, but this is all I cba typing out)</p> <p>The output I wanted was: 532 </p> <p>I managed to get awk to match but it would only match the first instance in every line. I tried Googling solutions but couldn’t find anything anywhere.</p> <p>Is this not what AWK was built for? Am I missing something fundamental and simple? Please help as it now keeps me up at night. </p> <p>Thanks in advance :)</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/rocket_186"> /u/rocket_186 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/zwdfmb/getting_multiple_nearidentical_matches_on_each/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/zwdfmb/getting_multiple_nearidentical_matches_on_each/">[comments]</a></span>t3_zwdfmb2022-12-27T11:21:32+00:002022-12-27T11:21:32+00:00Getting multiple near-identical matches on each line/u/AppleTreeBloomhttps://www.reddit.com/user/AppleTreeBloom<!-- SC_OFF --><div class="md"><p>Tapping the hive mind here. I need to sort letters in a string. I’ve put the letters into an array using Split, sorted the areay using Asort, now I need the elements of the array put back into a string. This seems to be unreasonably difficult in gawk due to it’s undefined concatenation. Is there a method or function that solves this problem? Thanks!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/AppleTreeBloom"> /u/AppleTreeBloom </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/zrxpqp/any_way_to_reliably_concatenate_the_elements_of/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/zrxpqp/any_way_to_reliably_concatenate_the_elements_of/">[comments]</a></span>t3_zrxpqp2022-12-21T19:07:18+00:002022-12-21T19:07:18+00:00Any way to reliably concatenate the elements of an array back into a string using gawk?/u/JMP800https://www.reddit.com/user/JMP800<!-- SC_OFF --><div class="md"><p>Looking to find options on how to split a &quot;.tsv&quot; file&#39;s field variable to an array.</p> <p>I would like to keep it POSIX compliant if possible. (Using mawk 1.3.4 20200120)</p> <p>Would prefer to use the built-in split() function since it gives incremental integer indexes.</p> <p>These are the methods I tried. Only the last one works in an example script below.</p> <ul> <li><p>array[$2];</p></li> <li><p>split($2, array. &quot;\n&quot;);</p></li> <li><p>string = sprintf(&quot;%s &quot;, $2); WITH split(string, array, &quot; &quot;);</p></li> <li><p>printf(%s &quot;, $2) &gt; &quot;field.tmp&quot;; WITH split(file, array, &quot; &quot;);</p></li> </ul> <p>*</p> <pre><code>#!/usr/bin/awk -f NR &gt; 1 { #SAVE FIELD AS 1-LINE FILE printf(&quot;%s &quot;, $2) &gt; &quot;/tmp/field.tmp&quot;; } END { #ASSIGN PATH VARIABLE path = &quot;/tmp/field.tmp&quot;; fflush(path); #SPLIT 1-LINE FILE INTO ARRAY while ((getline file &lt; path) &gt; 0) { split(file, array, &quot; &quot;); }; close(path); #PRINT TO STDOUT TO CONFIRM for (i in array) { printf(&quot;Index %d is %s\n&quot;, i, array[i]); } } </code></pre> <p>Any help would be awesome :) I did notice that POSIX awk supports regex in the split function but no luck.</p> <p>POSIX: (<a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html">https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html</a>)</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/JMP800"> /u/JMP800 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/zrwz1f/how_to_split_a_field_variable_ex_2_to_an_array/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/zrwz1f/how_to_split_a_field_variable_ex_2_to_an_array/">[comments]</a></span>t3_zrwz1f2022-12-21T18:40:03+00:002022-12-21T18:40:03+00:00How to split() a field variable (ex. $2) to an array?/u/International_Pea500https://www.reddit.com/user/International_Pea500<!-- SC_OFF --><div class="md"><p>Hi all. Can&#39;t get my brain around how to convert some netflow data into influxdb format via awk. </p> <p>Have data, looks like</p> <p>csv , columns , for , useful , data , 2022-12-15 12:24:15.410 </p> <p>I&#39;m currently breaking this data up with a while loop and IFS delimiter *but* there are so many lines of data that this ends up being a very slow process. </p> <p>I&#39;m pretty sure an inline awk would do this much faster, but I need a little help in execution. </p> <p>unixtimestamp=`date -d &quot;2022-12-15 12:24:15.410&quot; +&quot;%s&quot;` + 000 is what I need for influxdb. </p> <p>And advice on how to take that data column in the csv and replace it with the computed unix timestamp plus 3 zeros? All other columns we go untouched.</p> <p>&#x200B;</p> <p>Thanks.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/International_Pea500"> /u/International_Pea500 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/zp9f01/awk_to_convert_a_txt_date_to_unix_timestamp/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/zp9f01/awk_to_convert_a_txt_date_to_unix_timestamp/">[comments]</a></span>t3_zp9f012022-12-18T21:50:01+00:002022-12-18T21:50:01+00:00awk to convert a txt date to unix timestamp?/u/Automatic-Ad-9530https://www.reddit.com/user/Automatic-Ad-9530<!-- SC_OFF --><div class="md"><p>&#x200B;</p> <p><a href="https://preview.redd.it/jba6yf9mak6a1.png?width=546&amp;format=png&amp;auto=webp&amp;s=ed206487fecb222bf52eab16f17fbca45fcf2f06">data</a></p> <p>&#x200B;</p> <p><a href="https://preview.redd.it/p2yu2pxnak6a1.png?width=798&amp;format=png&amp;auto=webp&amp;s=11a29a440fe1ab90cf5f262d0a8e66c175df68bf">the script</a></p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Automatic-Ad-9530"> /u/Automatic-Ad-9530 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/zoms6x/chatgpt_playing_awk_giving_the_correct_result_in/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/zoms6x/chatgpt_playing_awk_giving_the_correct_result_in/">[comments]</a></span>t3_zoms6x2022-12-18T01:46:43+00:002022-12-18T01:46:43+00:00ChatGPT playing awk. Giving the correct result in first attempt./u/ctenolophonhttps://www.reddit.com/user/ctenolophon<!-- SC_OFF --><div class="md"><p>I&#39;m learning about using logical operators in short-circuit statements, outside of logical tests. (Been using Awk for years, but these are new to me.) I noticed that parenthesizing sub-statements is vital if you want the whole statement to work as expected. But I&#39;m not sure why, or how this works. For example:</p> <pre><code>BEGIN{ i = 0 ; i+=1 &amp;&amp; i+=10 &amp;&amp; i+=100 ; print &quot;___ &quot; i # 102 i = 0 ; i+=1 &amp;&amp; i+=10 &amp;&amp; (i+=100) ; print &quot;__p &quot; i # 102 i = 0 ; i+=1 &amp;&amp; (i+=10) &amp;&amp; i+=100 ; print &quot;_p_ &quot; i # 111 i = 0 ; i+=1 &amp;&amp; (i+=10) &amp;&amp; (i+=100) ; print &quot;_pp &quot; i # 111 i = 0 ; (i+=1) &amp;&amp; i+=10 &amp;&amp; i+=100 ; print &quot;p__ &quot; i # 102 i = 0 ; (i+=1) &amp;&amp; i+=10 &amp;&amp; (i+=100) ; print &quot;p_p &quot; i # 102 i = 0 ; (i+=1) &amp;&amp; (i+=10) &amp;&amp; i+=100 ; print &quot;pp_ &quot; i # 111 i = 0 ; (i+=1) &amp;&amp; (i+=10) &amp;&amp; (i+=100) ; print &quot;ppp &quot; i # 111 } </code></pre> <p>Only when the middle sub-statement is parenthesized do you get the expected result of 111. If it is not parenthesized you get 102, suggesting the first statement gets evaluated twice, and the last once. Anyone know why? Something to do with the Awk parser?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/ctenolophon"> /u/ctenolophon </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/zl5ofa/parentheses_in_shortcircuit_statements/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/zl5ofa/parentheses_in_shortcircuit_statements/">[comments]</a></span>t3_zl5ofa2022-12-13T20:16:10+00:002022-12-13T20:16:10+00:00Parentheses in short-circuit statements/u/Usually-Mistakenhttps://www.reddit.com/user/Usually-Mistaken<!-- SC_OFF --><div class="md"><p>awk &#39;/VM_pool|&lt;name&gt;/ { gsub(/&lt;|&gt;|\047/,&quot; &quot;); print $(NF-1) }&#39; $path</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Usually-Mistaken"> /u/Usually-Mistaken </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/za5o2e/newb_here_is_this_messy/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/za5o2e/newb_here_is_this_messy/">[comments]</a></span>t3_za5o2e2022-12-02T00:39:26+00:002022-12-02T00:39:26+00:00Newb here, is this messy?/u/Mark_1802https://www.reddit.com/user/Mark_1802<!-- SC_OFF --><div class="md"><p>I am using awk to look for a pattern inside a line and change the way it&#39;s shown. It aims those lines which have at least three occurrences of <code>nvl(</code> or one occurrence of four trailing open parenthesis <code>(</code> </p> <pre><code>awk &#39; { if (tolower($0) ~ /(.*nvl\(){3}/ || $0 ~ /\({4}/) { print &quot;/*&quot; , $0 , &quot;*/&quot; } else { print $0 } }&#39; teste.txt </code></pre> <p>If matched, this line is surrounded with <code>/**/</code>. It works fine. However, I&#39;d like to make the changes in the file itself, just like the <code>sed -i</code> option makes.</p> <p>Does <code>awk</code> have a mechanism to save the changes made in the same file?</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Mark_1802"> /u/Mark_1802 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/z2qywt/save_the_changes_in_the_same_file/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/z2qywt/save_the_changes_in_the_same_file/">[comments]</a></span>t3_z2qywt2022-11-23T14:46:30+00:002022-11-23T14:46:30+00:00Save the changes in the same file/u/elnanomlhttps://www.reddit.com/user/elnanoml&#32; submitted by &#32; <a href="https://www.reddit.com/user/elnanoml"> /u/elnanoml </a> <br/> <span><a href="/r/regex/comments/z0c6gu/find_2_words_before_a_certain_character_appears/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/z0cay9/is_there_a_way_to_do_this_with_awk_using_regex_or/">[comments]</a></span>t3_z0cay92022-11-20T19:05:58+00:002022-11-20T19:05:58+00:00Is there a way to do this with awk (using regex or with another alternative)/u/RyzenRaiderhttps://www.reddit.com/user/RyzenRaider<!-- SC_OFF --><div class="md"><p>Hi everyone. Newly discovered awk and enjoying the learning process and getting stuck on an attempt to Capitalize Every First Letter. I have seen a variety of solutions using a for loop to step through each character in a string, but I can&#39;t help but feel gsub() should be able to do this. However, I&#39;m struggling to find the appropriate escapes.</p> <p>Below is a pattern that works in sed for my use case. I don&#39;t want to use sed for this task because it&#39;s in the middle of the awk script and would rather not pipe out then back in. And I also want to learn proper escaping from this example (for me, I&#39;m usually randomly trying until I get the result I want).</p> <pre><code>echo &quot;hi. [hello,world]who be ye&quot; | sed &#39;s/[^a-z][a-z]/\U&amp;/g&#39; Hi. [Hello,World]Who Be Ye </code></pre> <p>Pattern is to upper case any letter that is not preceded by a letter, and it works as I want. So how does one go about implementing this substitution <code>s/[^a-z][a-z]/\U&amp;/g</code> in awk? Below is the current setup, but fighting the esxape slashes. Below correctly identifies the letters I want to capitalize, it&#39;s just working out the replacement pattern.</p> <pre><code>gsub(/[^a-z][a-z]/,&quot; X&quot;,string) </code></pre> <p>Any guidance would be appreciated :) Thanks.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/RyzenRaider"> /u/RyzenRaider </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/yzeui1/capitalizing_words_in_awk/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/yzeui1/capitalizing_words_in_awk/">[comments]</a></span>t3_yzeui12022-11-19T15:47:26+00:002022-11-19T15:47:26+00:00Capitalizing words in awk/u/AppleTreeBloomhttps://www.reddit.com/user/AppleTreeBloom<!-- SC_OFF --><div class="md"><p>Does anyone have an online awk simulator to recommend? Trying to teach myself awk for a project. Thank you!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/AppleTreeBloom"> /u/AppleTreeBloom </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/yynhaq/newbie_question_online_awk_simulator/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/yynhaq/newbie_question_online_awk_simulator/">[comments]</a></span>t3_yynhaq2022-11-18T15:59:28+00:002022-11-18T15:59:28+00:00Newbie question: online awk simulator?/u/breckhttps://www.reddit.com/user/breck&#32; submitted by &#32; <a href="https://www.reddit.com/user/breck"> /u/breck </a> <br/> <span><a href="https://pldb.com/posts/brianKernighan.html">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/ywfneh/a_brief_interview_with_awk_creator_dr_brian/">[comments]</a></span>t3_ywfneh2022-11-16T01:22:21+00:002022-11-16T01:22:21+00:00A brief interview with AWK creator Dr. Brian Kernighan/u/tje210https://www.reddit.com/user/tje210<!-- SC_OFF --><div class="md"><p>I have a text file, formatted</p> <p>A [tab] 1,2,3... (Varying number of fields) [tab] 1,2,3... (Varying number of fields)</p> <p>B [tab] 1,2,3... (Varying number of fields) [tab] 1,2,3... (Varying number of fields</p> <p>C [tab] 1,2,3... (Varying number of fields) [tab] 1,2,3... (Varying number of fields ... [20k lines]</p> <p>The first field is an IP address, the second column is a varying number of IPs, the third column is the same number of different IPs.</p> <p>I want to separate everything out so I get</p> <p>A 1 1</p> <p>A 2 2</p> <p>A 3 3</p> <p>...</p> <p>B 1 1</p> <p>B 2 2</p> <p>...</p> <p>basically turning 20k lines into 200k+. The second and third columns have 1 - 20 comma-separated fields.</p> <p>Thinking about constructing this, I&#39;d go</p> <p>while read p; do</p> <p>fields=(Count number of fields in second column)</p> <p>for i in 1..$fields; do</p> <pre><code> IP=$(cat $p | awk &#39;{print $1}&#39;) Srcaddr=$(cat $p | [awk to get $i&#39;th value in second column]) Dstaddr=$(cat $p | [awk to get $i&#39;th value in third column]) echo $IP $Srcaddr $Dstaddr &gt;&gt; outfile done </code></pre> <p>done</p> <p>That actually doesn&#39;t look too bad for a first pass. The term in lines 5 and 6 will take a little work, figure I&#39;ll get the second and third fields respectively, then do another awk using $i and FS=, to get the appropriate fields from those columns.</p> <p>Any tips for doing this better? I feel like what I wrote out above will get me there but it feels pretty graceless, and I&#39;d love to learn some new things.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/tje210"> /u/tje210 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/yscst6/reshape_existing_data/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/yscst6/reshape_existing_data/">[comments]</a></span>t3_yscst62022-11-11T14:55:24+00:002022-11-11T14:55:24+00:00Reshape existing data/u/batman_9326https://www.reddit.com/user/batman_9326<!-- SC_OFF --><div class="md"><p>I am trying to convert us-east-1 into ue1. I spent over some time but couldn’t figure out the right way to do it.</p> <p>Can someone please help me out?</p> <p>Edit: Thanks everyone for the input. I am going with the below one-liner. echo us-east-1 | awk -vRS=- &#39;{printf substr($0,1,1)}&#39;</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/batman_9326"> /u/batman_9326 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/ykf0yr/split_a_string_with_a_delimiter_and_print_first/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/ykf0yr/split_a_string_with_a_delimiter_and_print_first/">[comments]</a></span>t3_ykf0yr2022-11-02T19:45:52+00:002022-11-02T19:45:52+00:00Split a string with a delimiter and print first character/u/Hectic-Skeptichttps://www.reddit.com/user/Hectic-Skeptic<!-- SC_OFF --><div class="md"><p>I have a small Budgeting program going, and want to categorize items on a bank statement. I learned the absolute basics of AWK to combine multiple statement CSVs into one big CSV for the month or quarter. Since I am often getting groceries ect, I would like to knock off a good percentage of the categorizing with the use of matching against a lookup file. </p> <p>Is there a straight forward way in AWK for every field on a record in a csv, run through an entire lookup table matching the keyword in the lookup table to the field in the CSV?</p> <h1>Dummy Tables</h1> <h2>statement.csv:</h2> <table><thead> <tr> <th align="left">Date</th> <th align="left">Description</th> <th align="left">Amount</th> </tr> </thead><tbody> <tr> <td align="left">10/20/2022</td> <td align="left">TRADER JOE&#39;S CHICAGO IL</td> <td align="left">24.85</td> </tr> <tr> <td align="left">10/21/2022</td> <td align="left">SHELL GAS #1234</td> <td align="left">50.35</td> </tr> <tr> <td align="left">10/21/2022</td> <td align="left">Goldies Pub</td> <td align="left">10.15</td> </tr> <tr> <td align="left">10/22/2022</td> <td align="left">Dunkin Donuts</td> <td align="left">5.00</td> </tr> </tbody></table> <h2>KeywordToCategory:</h2> <table><thead> <tr> <th align="left">Keyword</th> <th align="left">Category</th> <th align="left"></th> </tr> </thead><tbody> <tr> <td align="left">Shell</td> <td align="left">Automotive</td> <td align="left"></td> </tr> <tr> <td align="left">Trader Joe</td> <td align="left">Grocery</td> <td align="left"></td> </tr> <tr> <td align="left">Goldie</td> <td align="left">Entertainment</td> <td align="left"></td> </tr> </tbody></table> <p>Thanks and I really appreciate the help!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Hectic-Skeptic"> /u/Hectic-Skeptic </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/yhzq1p/newbie_question_matching_substring_to_field/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/yhzq1p/newbie_question_matching_substring_to_field/">[comments]</a></span>t3_yhzq1p2022-10-31T03:39:11+00:002022-10-31T03:39:11+00:00Newbie Question: Matching sub-string to field?/u/LeadershipComplex958https://www.reddit.com/user/LeadershipComplex958<!-- SC_OFF --><div class="md"><p>awk &#39;($6&gt;max) &amp;&amp; ($4!=&quot;AZ&quot;) {max = $6; line = $0}END{print line}&#39; foo.txt</p> <p>Whenever field #6 contains negative numbers, it doesn&#39;t correctly return the line that has the highest number in field 6.</p> <p>The goal is to for example in the following file contents:</p> <p>///////////////////////////////</p> <p>Shellstrop Eleanor Phoenix AZ 85023 -2920765</p> <p>Shellstrop Donna Tarantula_Springs NV 89047 -5920765</p> <p>Mendoza Jason Jacksonville FL 32205 -4123794</p> <p>Mendoza Douglas Jacksonville FL 32209 -3193274 (Donkey Doug)</p> <p>Peleaz Steven Jacksonville FL 32203 -3123794 (Pillboi)</p> <p>/////////////////////////////////// </p> <p>goal is to return the line containing Peleaz. (It wouldn&#39;t be Shellstrop Eleanor as she lives in AZ.)</p> <p>This works as required for positive numbers but not negative. Or there could be some completely different bug Im missing. Im very new to awk.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/LeadershipComplex958"> /u/LeadershipComplex958 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/yfjlzb/why_is_this_codecommand_not_working_for_negative/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/yfjlzb/why_is_this_codecommand_not_working_for_negative/">[comments]</a></span>t3_yfjlzb2022-10-28T09:38:14+00:002022-10-28T09:38:14+00:00Why is this code/command not working for negative numbers?/u/LeadershipComplex958https://www.reddit.com/user/LeadershipComplex958<!-- SC_OFF --><div class="md"><p>&#x200B;</p> <p><a href="https://preview.redd.it/l0cnu6v7mgv91.png?width=333&amp;format=png&amp;auto=webp&amp;s=9db4aed991dd62f1eb0e7eefd37ccc34a2dc0f7f">https://preview.redd.it/l0cnu6v7mgv91.png?width=333&amp;format=png&amp;auto=webp&amp;s=9db4aed991dd62f1eb0e7eefd37ccc34a2dc0f7f</a></p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/LeadershipComplex958"> /u/LeadershipComplex958 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/yb4p5f/does_each_statement_inside_the_brackets_always/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/yb4p5f/does_each_statement_inside_the_brackets_always/">[comments]</a></span>t3_yb4p5f2022-10-23T01:40:51+00:002022-10-23T01:40:51+00:00Does each statement inside the brackets always execute repeatedly? Even though the average only needs to be set once. How do you know that it does it repeatedly? Super noob question.<!-- SC_OFF --><div class="md"><p>Im developing a script in awk to convert a tex document into html, according to my preferences.</p> <p>```</p> <h1>!/bin/awk -f</h1> <p>BEGIN { FS=&quot;\n&quot;; print &quot;&lt;html&gt;&lt;body&gt;&quot; }</p> <h1>Function to print a row with one argument to handle either a &#39;th&#39; tag or &#39;td&#39; tag</h1> <p>function printRow(tag) { for(i=1; i&lt;=NF; i++) print &quot;&lt;&quot;tag&quot;&gt;&quot;$i&quot;&lt;/&quot;tag&quot;&gt;&quot;; }</p> <p>NR&gt;1 { [conditions] printRow(&quot;p&quot;) }</p> <p>END { print &quot;&lt;/body&gt;&lt;/html&gt;&quot; } ```</p> <p>Its in a very young stage of development, as seen. </p> <p>``` \documentclass[a4paper, 11pt, titlepage]{article} \usepackage{fancyhdr} \usepackage{graphicx} \usepackage{imakeidx} [...]</p> <p>\begin{document}</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla placerat lectus sit amet augue facilisis, eget viverra sem pellentesque. Nulla vehicula metus risus, vel condimentum nunc dignissim eget. Vivamus quis sagittis tellus, eget ullamcorper libero. Nulla vitae fringilla nunc. Vivamus id suscipit mi. Phasellus porta lacinia dolor, at congue eros rhoncus vitae. Donec vel condimentum sapien. Curabitur est massa, finibus vel iaculis id, dignissim nec nisl. Sed non justo orci. Morbi quis orci efficitur sem porttitor pulvinar. Duis consectetur rhoncus posuere. Duis cursus neque semper lectus fermentum rhoncus.</p> <p>\end{document} ```</p> <p>What I want, is that the script only interprets the lines that are between <code>\begin{document}</code> and <code>\end{document}</code>, since before they are imports of libraries, variables, etc; which at the moment do not interest me.</p> <p>How do I make it so that it only processes the text within that pattern?</p> </div><!-- SC_ON --> <br/> <span><a href="https://www.reddit.com/r/awk/comments/y9qs93/processing_a_specific_part_of_a_text_according_to/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/y9qs93/processing_a_specific_part_of_a_text_according_to/">[comments]</a></span>t3_y9qs932022-10-21T11:04:19+00:002022-10-21T11:04:19+00:00Processing a specific part of a text according to pattern from AWK script/u/Gold-Ad-5257https://www.reddit.com/user/Gold-Ad-5257<!-- SC_OFF --><div class="md"><p>I&#39;ve seen some people say AWk don&#39;t really use ranges. </p> <p>I have an input plain text file that I would like to convert to CSV using awk. </p> <p>the problem is my last part of the record, where I want to preserve the input fields and not separate them with a delimiter, and This is pretty much a free format field(description) which can therefore contain say up to N random nr of words, which I would like to output as a single field.</p> <p>given the input as an example </p> <p>DATE TIME USERID NAME SURNAME SU-ID DESCRIPTION</p> <pre><code>10SEP22 17:26 UID01 John Wick root TEST 10SEP22 17:30 UID110 Bat Man DBusr Rerun Backup. 10SEP22 23:02 UID02 Peter Parker admin COPY FILE &amp; EDIT DATE </code></pre> <p>As can be seen after the 6th field I would like to specify the rest as a single field and there can be N words present until the end of the line. </p> <p>So currently I have this, </p> <pre><code>$awk &#39;{print $1 &quot;,&quot; $2 &quot;,&quot; $3 &quot;,&quot; $4 &quot; &quot; $5 &quot;,&quot; $6 &quot;,&quot; $7}&#39; </code></pre> <p>and the output is this : </p> <pre><code>10SEP22,17:26,UID01,John Wick,root,TEST 10SEP22,17:30,UID110,Bat Man,DBusr,Rerun 10SEP22,23:02,UID02,Peter Parker,admin,COPY </code></pre> <p>It obviously cuts off after field 7 and only works if there is a single word in the description. Note I am also trying to keep the name and surname as a single field, hence separated by a space, not a comma. </p> <p>I would like to get something like this to work in place of $7 above, while everything else($1 - 6) as per above still remains(on its own this works fine for my requirement) :</p> <pre><code>awk {&#39;{i = 14} {while (i &lt;= NF) {print $i ; i++}}&#39;} </code></pre> <p>that way the output should be :</p> <pre><code>10SEP22,17:26,UID01,John Wick,root,TEST 10SEP22,17:30,UID110,Bat Man,DBusr,Rerun Backup. 10SEP22,23:02,UID02,Peter Parker,admin,COPY FILE &amp; EDIT DATE </code></pre> <p>Any help is much appreciated.</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Gold-Ad-5257"> /u/Gold-Ad-5257 </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/y1f5s2/help_newbie_how_to_use_awk_to_specify_from_a/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/y1f5s2/help_newbie_how_to_use_awk_to_specify_from_a/">[comments]</a></span>t3_y1f5s22022-10-11T17:36:41+00:002022-10-11T17:36:41+00:00help : newbie : How to use awk to specify from a field X to end of line/u/morihackyhttps://www.reddit.com/user/morihacky&#32; submitted by &#32; <a href="https://www.reddit.com/user/morihacky"> /u/morihacky </a> <br/> <span><a href="https://kau.sh/blog/awk-1-oneliner-dollar-explanation/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/xn9t13/what_does_02_in_awk_do_learn_awk/">[comments]</a></span>t3_xn9t132022-09-25T01:24:20+00:002022-09-25T01:24:20+00:00What does $0=$2 in awk do? learn awk/u/shrchemhttps://www.reddit.com/user/shrchem<!-- SC_OFF --><div class="md"><p>I have a file which looks something like this:</p> <pre><code>ATOM 3667 CD1 ILE 237 12.306 -11.934 16.545 1.00 0.00 ATOM 3668 HD11 ILE 237 12.949 -12.488 16.075 1.00 0.00 ATOM 3669 HD12 ILE 237 11.408 -12.181 16.274 1.00 0.00 ATOM 3670 HD13 ILE 237 12.463 -11.002 16.328 1.00 0.00 ATOM 3671 C ILE 237 9.292 -11.489 20.242 1.00 0.00 ATOM 3672 O ILE 237 8.722 -10.388 20.078 1.00 0.00 ATOM 3673 OXT ILE 237 9.145 -12.132 21.279 1.00 0.00 TER ATOM 3674 N1 LIG 238 -1.541 3.935 2.126 1.00 0.00 ATOM 3675 C2 LIG 238 -0.418 6.199 2.597 1.00 0.00 ATOM 3676 N3 LIG 238 -3.604 3.076 2.842 1.00 0.00 ATOM 3677 C4 LIG 238 1.091 5.162 4.121 1.00 0.00 ATOM 3678 C5 LIG 238 0.498 4.906 5.503 1.00 0.00 </code></pre> <p>After TER in $1 you can see that from next record the $4 field is LIG, and the $5 is 238, I want to change $5 to 1 for the first time LIG is matched then 2 for the next and so on.</p> <p>This is how I want it to be:</p> <pre><code>ATOM 3667 CD1 ILE 237 12.306 -11.934 16.545 0.00 0.00 ATOM 3668 HD11 ILE 237 12.949 -12.488 16.075 0.00 0.00 ATOM 3669 HD12 ILE 237 11.408 -12.181 16.274 0.00 0.00 ATOM 3670 HD13 ILE 237 12.463 -11.002 16.328 0.00 0.00 ATOM 3671 C ILE 237 9.292 -11.489 20.242 1.00 0.00 ATOM 3672 O ILE 237 8.722 -10.388 20.078 1.00 0.00 ATOM 3673 OXT ILE 237 9.145 -12.132 21.279 0.00 0.00 TER ATOM 3674 N1 LIG 1 -1.541 3.935 2.126 0.00 0.00 ATOM 3675 C2 LIG 2 -2.491 3.845 3.151 0.00 0.00 ATOM 3676 N3 LIG 3 -3.604 3.076 2.842 0.00 0.00 ATOM 3677 C4 LIG 4 -3.852 2.404 1.633 0.00 0.00 ATOM 3678 C5 LIG 5 -2.826 2.559 0.663 0.00 0.00 </code></pre> <p>I have banged my head around google, I need a quick fix. I could get till <code>awk &#39;{ print $0 &quot;\t&quot; ++count[$1] }&#39;</code> which adds the counter as an extra column. Thanks for the help!!!</p> </div><!-- SC_ON --> &#32; submitted by &#32; <a href="https://www.reddit.com/user/shrchem"> /u/shrchem </a> <br/> <span><a href="https://www.reddit.com/r/awk/comments/x5un6p/match_a_pattern_start_counter_and_replace_the_5th/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/awk/comments/x5un6p/match_a_pattern_start_counter_and_replace_the_5th/">[comments]</a></span>t3_x5un6p2022-09-04T18:55:29+00:002022-09-04T18:55:29+00:00Match a pattern, start counter and replace the 5th field with the counter. Help Needed.