Saturday, July 23, 2022

[SOLVED] Find the second word delimited by space or comma then insert strings before and after

Issue

I have a file containing TABLE schema.table and want to put strings around it to make a command like MARK string REJECT

the file contains many lines TABLE SCHEMA.MYTAB, etc. etc....

or

TABLE SCHEMA.MYTAB , etc. etc....

The result is

MARK SCHEMA.MYTAB REJECT ..etc

I have

grep TABLE dirx/myfile.txt | awk -F, '{print $1}' | awk '{print $2}' | sed -e 's/^/MARK /' |sed -e 's/$/ REJECT/'

It works, but can this be tidier? I think I can combine the awk and sed into single commands but not sure how.


Solution

Maybe:

awk '/^TABLE/ {gsub(/,.*$/, ""); print "MARK " $2 " REJECT"}' dirx/myfile.txt


Answered By - Arnaud Valmary
Answer Checked By - David Goodson (WPSolving Volunteer)