Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash: Converting string with new lines to string with unique values with commas, inline

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading