0

I'm trying to write a command to capture every license in the linux OS (kali) and associate it with the correct package. The basic idea is that you search /usr/share/doc recursively for "copyright" file, then you cat that and search for "^License" the leading license. There can be multiple per package.

I'm trying to create a csv that takes all of the package names and puts them in the first field of each row, and then follows it with each license comma delimited.

My basic flow: make a list of every package (done). Make a list where each line is csv of the licenses found. Then just paste file 1 and file 2, boom.

Problem, I have to swap the newlines in the output of the command with comma's, but I need to then re-insert a newline at the end for each iteration of xargs so my csv licenses will line up with the package list. I've tried this dozens of ways. When I do command substitution it breaks translate or echo (doesn't read \n as a newline anymore. I've tried \\n, no luck).

Here is what I have that just needs a way to put a newline per xargs cycle.

find . -name copyright |xargs -l cat |grep "^License" |tr '\n' ',' 

So if I could just pipe to something that inserts \n after every xarg cycle I'd be done. I know I can write a for loop for this with variables.

1

If you're okay with awk, try this solution:

find $PWD -name copyright | xargs awk 'BEGIN{ORS=","} FNR==1{print "\n"FILENAME}; $0 ~ /^License/ {print $0}' | sed 's/,$//g'  |awk -F',' 'NF>1'
  • That worked. Thank you so much. I'll read up on awk, looks like I need more tools in my tool box. Thanks again. – bashCypher Sep 18 '18 at 17:09
  • @bashCypher glad it worked. – Rakesh.N Sep 19 '18 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.