0

I'm looking for a way to generate an awk file using sed. The problem at hand is, I have a text file with about 1000 rows of words and I want to be able to extract each line from the text file and use the word extracted as a regex in an awk file.

For example the text file will read:

James    Class1
Toby     Class3
Callum   Class4
Sarah    Class1
Jeremiah Class2

Output to awk using sed should look like

/ reg exp for James/ {action matching word has Class1}
/ reg exp for Toby / {action matching word has Class3}
/ reg exp for Callum / {action matching word has Class4}
/ reg exp for Sarah / {action matching word has Class1}

I've tried extracting all the words using sed but can't seem to find a way to write that to an awk file!

9
  • 1
    Welcome to SO, not clear, why do you need a sed for creating an awk program? Please be more clear with more details in your post and let us know then. Commented Aug 19, 2018 at 7:40
  • Also please confirm do you really want to print text action matching word has Class1 etc or you want it actual code some conditions to be printed in a script or etc which you want to execute it later? Commented Aug 19, 2018 at 7:46
  • thanks for your help Ravinder, basically the task is to create a sed file that generates many lines of awk code that count the number of students in a certains. the end output should be able to count how many students in class1, class2 and class3. Commented Aug 19, 2018 at 7:49
  • 3
    The XY problem is asking about your attempted solution rather than your actual problem. Commented Aug 19, 2018 at 7:57
  • 2
    I'm looking for a way to generate an awk file using sed. - that makes zero sense. If you can parse the input file with sed then you can certainly parse it with awk and if you can produce an awk script to do something from sed then you can just write the awk script to do whatever that something is. Tell your teacher to come up with a more useful/realistic exercise. Commented Aug 19, 2018 at 13:00

1 Answer 1

1

Take a look at this GNU awk (works only in GNU awk due to the multi dimensional array syntax, as noted by @EdMorton):

awk '{ a[$2][$1]++ }
END {
    for(i in a) {
        sum=0
        printf "=== %s ===\n", i
        for(j in a[i]) {
            sum+=a[i][j]
            printf "%s: %d\n", j, a[i][j]
        }
        printf "> %d\n", sum
    }  
}' data

Example:

$ cat data
Sarah    Class1
James    Class2
Sarah    Class1
Sarah    Class2
Peter    Class1
$ ./script
=== Class1 ===
Sarah: 2
Peter: 1
> 3
=== Class2 ===
James: 1
Sarah: 1
> 2
Sign up to request clarification or add additional context in comments.

1 Comment

@EdMorton Good point, will edit it into my answer. Any other suggestions are welcome :)

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.