2

I have a txt file with two columns which in each column I have function Ids which indicate the first function calls the second function such as below:

1,4

12,5

4,8

8,1

5,23

Now, I am going to use awk commands to find a flow of my function calls. For example, based on the above content of my file, I wanna to extract a flow of 1,4,8 and 12,5,23

For each flow, I will continue to add the function ids until I reach to a circle or I reach to end of the file. My files are very huge and I don't wanna to use python.

4
  • Please share your input and expected output file along with criteria to extract the output. Commented Mar 11, 2017 at 13:31
  • @EdMorton - "until I reach to a circle" is not clear to me. Commented Mar 11, 2017 at 13:37
  • @Ed Morton, My file has only two columns and I wanna to read the rows from top to below. For each line, I will not look at the before lines. In your example, 5,23 12,5 7,5 I will have F1: 5,23 and F2:12,5 and F3:7,5 just these three flows(F). But in my mentioned example in the question's content, I will have F1:1,4,8 F2:12,5,23 just that. is it clear for you? Commented Mar 11, 2017 at 13:42
  • @EdMorton No, it will NOT appear. Because we have mentioned it in our previous flow(s). Commented Mar 11, 2017 at 13:55

1 Answer 1

3

You'll want a recursive-descent program, like this:

$ cat recurse.awk
BEGIN { FS=OFS="," }
{
    roots[NR] = $1
    map[$1] = $2
}
END {
    for (rootNr=1; rootNr<=NR; rootNr++) {
        root = roots[rootNr]
        if ( !(seen[root]++) ) {
            tree = root
            descend(root)
            print tree
        }
    }
}

function descend(root,  branch) {
    if (root in map) {
        branch = map[root]
        if ( !(seen[branch]++) ) {
            tree = tree OFS branch
            descend(branch)
        }
    }
}

$ awk -f recurse.awk file
1,4,8
12,5,23
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.