-1

Here is my data file called:

bblack|Black|Brian|P|5|sun
lbanas|Banas|Kerry|S|5|mercury
dholder|Holder|David|S|5|venus
dives|Ives|David|W|5|earth
slambert|Lambert|Scott|S|5|mars
rtaylor|Taylor|Rich|S|5|jupiter 
dtempleman|Templeman|Daniel|W|5|saturn
vtrimboli|Trimboli|Vincent|S|5|uranus
swalker|Walker|Steve|W|5|neptune
szemanek|Zemanek|Steven|W|5|pluto

And here is my script

#!/bin/bash
echo "USER_ID      LAST_NAME      FIRST_NAME       JOB       OLD_NICE     PREFERRED_PASSWORD"
while read IN_RECORD
do
     USER_ID=`echo $IN_RECORD | cut -d'|' -f1`
     LAST_NAME=`echo $IN_RECORD | cut -d'|' -f2`
     FIRST_NAME=`echo $IN_RECORD | cut -d'|' -f3`
     JOB=`echo $IN_RECORD | cut -d'|' -f4`
     OLD_NICE=`echo $IN_RECORD | cut -d'|' -f5`
     PREFERRED_PASSWORD=`echo $IN_RECORD | cut -d'|' -f6`
     FULL_NAME="$FIRST_NAME $MIDDLE. $LAST_NAME"

     echo "$USER_ID           $LAST_NAME         $FIRST_NAME           $JOB         $OLD_NICE         $PREFERRED_PASSWORD"

     if [ "$JOB" = "P" ]
     then
         sudo nice -n3 [ $USER_ID,$LAST_NAME,$FIRST_NAME,$JOB,$OLD_NICE,$PREFERRED_PASSWORD ]
         echo "NEW_NICE = 3"
     elif [ "$JOB" = "S" ]
     then
         sudo nice -n6 [ $USER_ID,$LAST_NAME,$FIRST_NAME,$JOB,$OLD_NICE,$PREFERRED_PASSWORD ]
         echo "NEW_NICE = 6" 
     else
         sudo nice -n7  [ $USER_ID,$LAST_NAME,$FIRST_NAME,$JOB,$OLD_NICE,$PREFERRED_PASSWORD ]
         echo "NEW_NICE = 7"
     fi

     if [ -z "$(getent passwd $USER_ID)" ]; 
     then
         sudo useradd -m -c "$FULL_NAME" -p $(echo $P4ssw0rd | openssl passwd -1 -stdin) $USER_ID
         MESSAGE="$USER_ID created"
         echo "$USER_ID created"
     else
         MESSAGE="$USER_ID already setup "
         echo $MESSAGE
     fi

done < test3_data.txt > hamd.txt

Here is the output desired. I have tried using awk in different ways but i am lost.

Here is the output desired. I have tried using awk in different ways but i am lost . Please help.

  • Can you elaborate on the kind of problems you are having? Did you consider @[John Kerl]'s answer and try breaking up the script? – qneill Oct 12 '17 at 16:54
1

No one has answered, although the question was downvoted -- without any stated reason why.

But I can guess: While you've given your inputs and your desired outputs (plain text better than screenshot, but that's a relatively minor detail), you haven't given any specific indication of what you've tried. While you say "I have tried using awk in different ways but i am lost", it would be useful to see some detail.

So please let the rest of us know what you've tried and what you think is wrong with it.

That said, I'll move the conversation forward a bit by saying this: the pattern you're following here is one I see quite often, namely, input-parsing, processing (nicing, account-creation), and output-formatting appear mixed within the same question.

This is a dangerous paradigm to follow. If you've got a single awk script doing all of this, then -- for example -- you'll need to re-do the account operations every time you want to test the pretty-printed output formatting.

So while I encourage you to show us some of your code, I also encourage you to have three scripts: one which parses the input and decides what actions to take; one which does the actions; one which, given the results, pretty-prints the results. Each can take some input data and write to standard output. For production you can pipe the three one to the next. For testing you can invoke each one separately. If you do this then I think you'll find the problem much more tractable.

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.