Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>awk is definitely your friend.</p> <p>awk reads a datafile, line-by-line. You don't need/want to have any kind of loop structure (unless you're getting very advanced). </p> <pre><code>awk '{print $0}' inFile </code></pre> <p>Is all you need to read each line of a file and print it out (it will go to your screen, so don't do a big file)</p> <p>note that I used <code>$0</code> to indicate 'the whole line of data'.</p> <p>Awk also has variables to refer to each field of data, you use values like <code>$2</code> to print the 2nd field in the file.</p> <blockquote> <p>I want to replace the digits by letters according to column 5th &amp; 6th. So what I want is to replace 0 with T T (if the 5th column is T) and 2 by C C (if the 6th column is C) and 1 with T C (if the 5th and 6th columns are T and C respectively) and I want to change -1 to ? ? or ! !</p> </blockquote> <p>So for your problem, you want to test each line, test certain fields and set new values.</p> <pre><code>awk 'NR&gt;1{ # replace 0 with T T (if the 5th column is T) if ($5 == 0) $5="TT" # and 2 by C C (if the 6th column is C) if ($6 == 2) $6="CC" # and 1 with T C (if the 5th and 6th columns are T and C respectively) if ($5 == "T" &amp;&amp; $6 == "C") $1="1" }' inputFile | sed 's/TT/T T/; s/CC/C C/' </code></pre> <p>To change all fields after a certain field, incorporate this code as needed,</p> <pre><code>awk 'NR&gt;1{ # replace 0 with T T (if the 5th column is T) if ($5 == 0) { for (i=5; i&lt;=NF;i++) { printf("T ") } printf("\n") } ...... </code></pre> <p>}' inputfile ...</p> <p>The <code>NR&gt;1</code> means only processes line numbers greater than 1.</p> <p>Note that we're using simple logic to implment your tests. it is easy to add more and more. Recall that many times it makes sense to use 'layered' logic <code>if ($5==0) { ... } else if ($5 == 1) { ....}</code></p> <p>The one problem is your requirement to output 'C C', for example. When you do something like `$5="C C" in awk, awk will recalibrate its field numbers, so $5 will be C and $6 will be C, not the value that was there before. </p> <p>I have taken the short-cut of printing 'CC', and then using sed at the the end to create the 'C C' values that your specfication indicates.</p> <p>I'm not sure how to deal with </p> <blockquote> <p>and I want to change -1 to ? ? or ! !</p> </blockquote> <p>as it has to be one or the other, and I'm not sure what field you want to operate on. Use the above code as a guide. If you get stuck, post a new question with sample input data, expected output, current output and the code you are using.</p> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload