Issue
I am working with some bash script and would need your help :) The result that I want: V04.01, V04.02
- one string, no new lines, unique values, commas. Only name prefixes (all before __), only for sql files.
The original diff command and its results:
git diff --name-only 123132132213 origin/dev
gives:
.abc.yml
abc/test.xml
aaa/V04.01__SampleInsert.sql
bbb/V04.01__SampleInsert.sql
abc/efg/V04.02__SampleUpdate.sql
The result that I want: V04.01, V04.02
My script which is not working:
git diff --name-only 12313123123132 origin/dev | grep '.sql' | rev | cut -d"/" -f1 | rev | awk '{sub(/__.*/,x)}1'
The result is:
V04.01
V04.01
V04.02
Thanks in advance if anyone has some idea how to do that inline.
Solution
Using cat file
in place of your git
command which I don't have:
$ cat file | awk -F'/' '/\.sql$/{ sub(/__.*/,"",$NF); if ( !seen[$NF]++ ) printf "%s%s", sep, $NF; sep=", " }'
V04.01, V04.02
I know you said you don't want any newlines but that makes the output not a valid text file so if you do want a terminating newline just add END{print ""}
to the end of the awk script.
Answered By - Ed Morton Answer Checked By - Mildred Charles (WPSolving Admin)