2

I have a text file that is organized as follows:

Group 1
asdsdsdsdf.html  jeff
xcvxcvxcvx.html  bob
vrgeiuvhif.html  sue

Group 2
iwdowijdoi.html  mary
pokpompojm.html  doug
ndkjfsjfbs.html  lisa

I need a bash script that creates a directory named after each group. And then a text file inside named for each person in that group with their corresponding link on the first line.

I have managed to be able create the directories, the named text files, and a file with only the links. But I don't know how to echo each link into each name file or how to sort them by group.

I'm sure there are better ways to accomplish the small amount that I have.

#!/bin/bash

grep Group list.txt > groups
grep -v Group list.txt > links_names
while read file; do mkdir "${file}"; done < groups
while read line; do export name=`echo $line | awk '{print $2}'`
touch "$name"; done < links_names
while read line; do export link=`echo $line | awk '{print $1}'`
echo "$link" >> links ; done < links_names

rm {groups,links_names,links}
3

With awk:

awk '/^Group/{g=$0; system("mkdir \""g"\""); next} g&&$0{print $1 >g"/"$2}' file
  • /^Group/ if the line starts with the string Group...
    • g=$0 set the variable g to the group name, for example Group 1.
    • system(...) call the command mkdir with the system function to create that directory (notice the additional quotes to deal with spaces in group names).
  • g&&$0 if the variable g is defined and the line is not empty...
    • print $1 >g"/"$2 write the value in the first field $1 (the link) to a file in the directory g name by the value of the second field $2 (the name).

The test:

$ cat Group\ 2/doug
pokpompojm.html
1

Requirements

I need a bash script that creates a directory named after each group. And then a text file inside named for each person in that group with their corresponding link on the first line.

#!/bin/bash
while read a b
do
    # Skip blank lines
    test -z "$a" && continue

    if [[ "$a" == "Group" ]]
    then
        # Create the group directory
        group_dir="$a $b"
        mkdir "$group_dir"
    else
        # Write the link into the named file
        echo "$a" > "$group_dir/$b"
    fi
done <list.txt

For the purists, this assumes a number of restrictions:

  • no line in your text file begins with significant whitespace
  • no link contains whitespace
  • there are either precisely two words per line separated by whitespace, or that lines are empty
  • that the first non-blank line is a Group header

All of these assumptions can be overcome at the expense of additional code (and complexity).

  • You missed a quote in the echo line. But after I added one it worked perfectly. It was also much easier for me to understand than the "awk" answer. Thank you – WiseGuy Oct 14 '15 at 22:31
0

If your textfile is strict, as in your group will always be called "Group" followed by a space and a word and will not have any spaces in the group name, and similarly for the names after .html, this should work.

#!/bin/bash

parseNdostuff(){
    case $1 in
        Group)
            shift
            mkdir $1
            cd $1
            ;;
        *.html)
            link=$1
            shift
            echo $link > $1
            ;;
        *)
            cd ..
            ;;
    esac

}

while read line
do
   parseNdostuff $line
done <  mytext.txt

Just do a while read line and your text file input at the end < mytext.txt, and feed it to a function i wrote. The reason I chose to do it in a function was for simplicity of dealing with each line, plus being able to easily use case and shift.

Some details:

$1 is the first argument of the function (the $ variables are special, look them up)

shift pushes down the arguments ($1 gets dropped, $2 becomes $1, $3 -> $2)

when it creates a directory it cd into that directory so when the statement runs again and sees the .html lines it makes the files and inputs the link all in the proper directory.

*) is all other cases, so if you have a blank line between groups, it will just cd .. and go down a directory, and you'll be back where you started your script. Note, all these cd's are relative to your script placement.

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.