0

I have a CSV file which contains the data in the following format

web_url.csv

drive,http://www.drive.google.com/
wikipedia,https://en.wikipedia.org/wiki/%s
wolframalpha,http://www.wolframalpha.com/input/?i=%s

I would like to create a directory for each item in the first column and inside that directory create a text file with the same name as the directory. ie: it should create a file structure as shown.

├───drive
│       - drive.txt
│
├───wikipedia
│       - wikipedia.txt
│
└───wolframalpha
        - wolframalpha.txt

The file contents of each of the file should be as shown below

#!/bin/bash
echo [$@]
echo "command=xdg-open \" $url $@\""
echo "icon="
echo "subtext=Search on $keyword for $@"

where the $url is replaced by the item in the second column and the $keyword is being replaced by the item in the first column.

Edited:

awk -F "," '{print $1,$2}' web_url.csv | awk '{system("mkdir "$1)}'
awk -F "," '{print $1,$2}' web_url.csv | awk '{print $1 > $1".txt"}'

test.sh

key=`awk -F "," '{print $1}' web_url.csv`
search_url=`awk -F "," '{print $2}' web_url.csv`

for word in $key; do
echo 'echo "command=xdg-open \"$url$@\""'
echo 'echo "icon="'
echo "echo "\"subtext=Search on $word for \$@""\"
done

Output

echo "command=xdg-open \"$url$@\""
echo "icon="
echo "subtext=Search on wikipedia for $@"
echo "command=xdg-open \"$url$@\""
echo "icon="
echo "subtext=Search on wolframalpha for $@"
echo "command=xdg-open \"$url$@\""
echo "icon="
echo "subtext=Search on drive for $@"

I am not sure how to create the file inside the folder and how to create the file contents for each file and not have the contents in a single file as shown in the output above.

Also in the output I am not getting the url there.

  • 2
    What is the question? Where is the script that is doing this, where does it go wrong? What error does it give or what output does it generate that differs from your example output? – Anthon Jun 9 '15 at 10:21
  • 1
    Please show us what you have so far and what is giving you trouble. We're here to help you do your work, we're not a free script-writing service. – terdon Jun 9 '15 at 10:36
0

First make a script doit.sh that takes two parameters: the keyword and the url:

#!/bin/bash
mkdir -p $1
cat > $1/$1.txt << EOF
#!/bin/bash
echo [\$@]
echo "command=xdg-open \\" $2 \$@\\""
echo "icon="
echo "subtext=Search on $1 for \$@"
EOF

make it executable (chmod +x doit.sh) and test it until calling it with two parameters gets you the right result. (mkdir's -p option prevents complaining that the directory already exists). Especially focus on escaping the $ and \ to be preserved in the output.

Once you have that, call the script for every line with two parameters:

awk -F "," '{print "./doit.sh " $1 " " $2}' web_url.csv  | sh

The way you have been doing it, you walk over all keyword (for word in $key) but you don't have the matching URL to do the proper thing.

I am not sure how to fix that, but if you need to do multiple things, always try to do one of those things correctly, then focus on how to use that solution multiple times. Being unfamiliar with awk I had to experiment without piping the output into sh until I could see the doit.sh would be called correctly:

awk -F "," '{print "./doit.sh " $1 " " $2}' web_url.csv  

gives as output:

./doit.sh drive http://www.drive.google.com/
./doit.sh wikipedia https://en.wikipedia.org/wiki/%s
./doit.sh wolframalpha http://www.wolframalpha.com/input/?i=%s
  • Cheers. I does exactly what I want. Thank you. – Ishan Jun 10 '15 at 5:58

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.