Find the answer to your Linux question:
Results 1 to 2 of 2
Hello everybody, what I'm trying to do is a little tricky, and frankly I have no ideea how to start. The problem looks like this: 1.) I have 2 files, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    5

    Question Generate files


    Hello everybody, what I'm trying to do is a little tricky, and frankly I have no ideea how to start. The problem looks like this:
    1.) I have 2 files, 1 witch contain usernames and passwords and 1 which contains some text
    the user/pass looks like this:
    user1
    pass1
    user2
    pass2
    user3
    pass3
    etc...
    the text file looks like this
    title1
    text1
    title2
    text2
    title3
    text3
    ...
    title19
    text19
    title20
    text20

    What I'm trying to do is generate some files with date in a custom format in order to import into my program, the files must look like this

    so I must have a file called

    05.nov.2012.ext

    that sould contain 10 titles and have with the first username, after that the next 10 titles must be with the second username and than the 1st file must be ended

    and the second file
    06.nov.2012.ext must be created and have the first 10 titles with the 3rd username and the next 10 titles with the 4th username and so on

    so 20 titles in a file with the first 2 users
    the same 20 titles with the 3rd and 4th user
    the 20 titles with the 5th and 6th user
    etc... until 30 files are maded.

    Until now I tryed with a while statement but i get nowhere, could somebody give me a hint on where, and how to start the script?
    Thank you

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Hi,

    That is quite an odd requirement, I agree. It could be done many ways. Awk and Perl leap to mind. Here is a sample script to get you started written in Bash. It doesn't do everything you want, but it should get you going.

    What it doesn't do:

    1) does not write to "date-specific" output files. You can figure that out for yourself. besides, you did not specify how that should be determined.

    2) does not stop after writing 30 output files (will happily keep on going provided there are enough users and titles in the two files).

    What you have to do:

    1) Specify the proper names/locations of your two files in the two variables located at the top of the script (named userFile and titlesFile).

    Here's the script:

    Code:
    #!/bin/bash
    
    # the file containing username/password
    userFile='./gen-users.txt'
    
    # the file containing title/text
    titlesFile='./gen-titles.txt'
    
    # make sure the files exist
    ! [ -f $userFile ] && echo $userFile: No such file && exit 1
    ! [ -f $titlesFile ] && echo $titlesFile: No such file && exit 1
    
    # declare some arrays and variables
    declare -a user
    declare -a pass
    declare -i n=0
    declare -i x
    
    # put users and passwords from file into array
    while read line; do
      if (( $n % 2 == 0 )) ; then
        x=${#user[*]}
        user[$x]=$line
      else
        x=${#pass[*]}
        pass[$x]=$line
      fi
      n+=1
    done < <(cat $userFile)
    
    # reset some variables and declare some arrays
    n=0
    unset x
    declare -a title
    declare -a text
    
    # put titles and text from file into array
    while read line; do
      if (( $n % 2 == 0 )) ; then
        x=${#title[*]}
        title[$x]=$line
      else
        x=${#text[*]}
        text[$x]=$line
      fi
      n+=1
    done < <(cat $titlesFile)
    
    # declare some more...
    n=0
    declare -i wrap=1
    declare -i base=0
    declare -i end=10
    declare -i c
    
    # now loop thru the list of users, one at a time
    for ((u=0;u<${#user[*]};u++)); do
    
      # starting with first user at index 0, check if index is odd or even
      c=$[ $u % 2 ]
      if [ $c == 0 ]; then
    
        # every two users, one-up the file counter
        let n+=1
    
        start=$(( $base + 1 ))
        end=$(( $base + 10 ))
      else
        start=$(( $base + 11 ))
        end=$(( $base + 20 ))
      fi
    
      # the output file
      out="output_${n}.txt"
    
      # print to terminal for debugging purposes (can comment out)
      echo -n "User $u: ${user[$u]}, File: $out, Wrap: $wrap, Base: $base, Titles: "
      echo "$start - $end"
    
      # append the username to the file
      echo ${user[$u]} >> $out
    
      # loop thru the range of titles determined for the user
      for i in `seq $start $end`; do
    
        # the index number of the title is one less than the number in the range
        ttl=${title[$(($i-1))]}
        echo "title for $i is $ttl"
        if [ -z "$ttl" ]; then
          echo "Ran out of titles to process"
          exit 1
        fi
    
        # append the title to the output file
        echo $ttl >> $out
      done
    
      # increment the titles by 20, if we've done four users
      if [ $wrap == 4 ]; then
        let base+=20
        wrap=1
      else
        let wrap+=1
      fi
     
    done
    Let me know if there are things in your user/title text files that break it (there are bound to be some, i guess).

    Note: this code assumes that the users file is always in the form of a single user on one line, followed by that user's password on the next line, followed by the next username, followed by that users' password, etc. That is implied based upon your example file you posted, but I'm just clarifying.

    Same goes for titles and associated text in the titles file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •