0

Exist many files with this structure within diferent directories, the files are named equals idea.js:

get.idea("tagIdea1","Some idea1 tag abc");
get.idea("idea1","Some idea1 description1 abc");
get.idea("tagIdea2","Some idea2 tag abc");
get.idea("idea2","Some idea2 description2 abc");
...
...

...So like this... Many ideas with its correspondent idea description.

It is necessary to get the second parameter from each get.idea function and create its correspondent .csv file in the same directory that was found de idea.js file with that seconds parameters for each file found like this:

Idea, descripcion
"Some idea1 tag abc","Some idea1 description1 abc"
"Some idea2 tag abc","Some idea2 description2 abc"

I think with grep command and a regular expression storing the words in an array and within a cycle increasing the number for each idea can be done but I don't know how create the regular expression that match the second parameter for each idea and how storage in an array to later be exported to a new .csv file with the mentioned estructure in the same directory that was found the idea.js files. Can you help me.

  • Could the strings contain commas? – Kusalananda May 4 '18 at 17:00
  • @Kusalananda yes, the second parameter can contain commas and even scaped characters like this: get.idea("idea2","Some idea2 description2 \"some quoted text\" abc"); – Carlos May 4 '18 at 17:05
0
$ sed -n '/^get\.idea/s/^.*(\(.*\)).*/\1/;p' input | awk -F, 'BEGIN {print "Idea, Description";OFS=","} (NR % 2 ) == 1 {save=$2} (NR % 2) ==0 {print save, $2}'
Idea, Description
"Some idea1 tag abc","Some idea1 description1 abc"
"Some idea2 tag abc","Some idea2 description2 abc"

We use sed to strip off all the irrelevant cruft, and then use awk to make your CSV file by parsing every other line.

  • Good! In some point of the output shows the correct and desired result but after print the line: Idea, Description ...many commas are printed and after the correct result many part of code is printed too because the idea.js file has much code and some of them match with the regex I think. Even in the file idea.js exists the get.idea function some times that is not necesary to get the secod parameter, for example get.idea("activity","01") in this case I don't need the second parameter, only when the first parameter is "tagIdea1 2 3... Etc." And "idea1 2 3... Etc" Almost done @DopeGhoti. – Carlos May 4 '18 at 18:45
  • This is the output: pastebin.com/G0TRXYfC – Carlos May 4 '18 at 19:02
  • I have adjusted the sed portion to only feed lines starting with get.idea to awk for processing. – DopeGhoti May 4 '18 at 19:44
  • The final output with the sed portion modified show the same result as before, I don't know how to set a correct regex and even I don't know if the error is just in that part. The command and all options are near to the correct result, only missing filtering the other code as mentioned :/ – Carlos May 4 '18 at 23:01
  • Works for me like this: sed -n '/^get\.idea/s/^.*((.*)).*/\1/ p' ... ... this is removing the semicolon from the regex How can implement it using find to go through an entire directoy structure? – Carlos May 5 '18 at 21:42
0

With single awk command:

awk 'BEGIN{ print "Idea, Description"; FS = OFS = "," }
     { desc = substr($2, 1, length($2) - 2) }
     { if ($1 ~ /"tag/){ tag = desc } else { print tag, desc } }' idea.js

The output:

Idea, Description
"Some idea1 tag abc","Some idea1 description1 abc"
"Some idea2 tag abc","Some idea2 description2 abc"

Extended approach to cover the requirement "create its correspondent .csv file in the same directory that was found de idea.js file":

Create idea_to_csv.awk script:

#!/bin/awk -f

BEGIN{ print "Idea, Description"; FS=OFS="," }
{ desc = substr($2, 1, length($2)-2) }
{ if ($1 ~ /"tag/){ tag = desc } else { print tag, desc } }

find . -type f -name "idea.js" -execdir sh -c 'awk -f ~/idea_to_csv.awk "$1" > idea.csv' _ {} \;

Change path ~/idea_to_csv.awk to your actual location where idea_to_csv.awk was created

  • Works but is happening as I have described in the comment for @DopeGhoti unix.stackexchange.com/questions/441816/… Additional in the correct output to print a final parenthesis and a break line, otherwise well, can you polish this? – Carlos May 4 '18 at 18:52
  • @Carlos, you could update your question with final expected output so I could adjust my answer – RomanPerekhrest May 4 '18 at 19:18
  • Right, just as is written now: Idea, descripcion "Some idea1 tag abc","Some idea1 description1 abc" "Some idea2 tag abc","Some idea2 description2 abc" I need too as mentioned go through an entire directory structure finding for all the idea.js files and generate in that same directory where they are located the corresponding .cvs for each idea.js file found but what you have done is an excellent start. – Carlos May 4 '18 at 19:40
  • @Carlos, done. See my update – RomanPerekhrest May 4 '18 at 20:11
  • I get this error in console: awk: cannot open idea_to_csv.awk (No such file or directory) and idea_to_csv.awk is put in the same location I'm executing the find command: find . -type f -name "idea.js" -execdir sh -c 'awk -f idea_to_csv.awk "$1" > idea.csv' _ {} \; The error is printed many times, the times the script found the idea.js file. The script continues generating all additional code mentioned in this comment – Carlos May 4 '18 at 23:13

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.