skip to Main Content

Existing names in the "names" file is in form of lastname1,firstname1 ; lastname2,firstname2.

In the new file it should be like down below.

Create a script that outputs a list of existing users (from the "names" file) in the form:

firstname1.lastname1
firstname2.lastname2
etc.

And saves a file called "cat list"

2

Answers


  1. This kind of command line should be a solution for you :

     awk -F '.' '{print $2","$1}' source_file >> "cat list"
    
    • First awk revers the order of the field and put the char ‘,’ under
    • ">>" Second step redirect full output to a file called "cat list" as requested
    Login or Signup to reply.
  2. I don’t think I have the most efficient solution here but it works and outputs the different stages of translation to help illustrate the process:

    #!/bin/sh
    
    echo "lastname1,firstname1 ; lastname2,firstname2" >testfile
    echo "original file:"
    cat testfile
    echo "n"
    
    # first replace semi-colon with newline
    tr ';' 'n' <testfile >testfile_n
    echo "after first translation:"
    cat testfile_n
    echo "n"
    
    # also remove extra spaces
    tr -d '[:blank:]' <testfile_n >testfile_n_s
    echo "after second translation:"
    cat testfile_n_s
    echo "n"
    
    # now swap name order using sed and use periods instead of commas
    sed -E 's/([a-zA-Z0-9]*),([a-zA-Z0-9]*)/2.1/g' testfile_n_s >"cat list"
    echo "after third iteration:"
    cat "cat list"
    echo "n"
    

    The script above will save a file called ‘cat list’ and output something similar to:

    original file:
    lastname1,firstname1 ; lastname2,firstname2
    
    
    after first translation:
    lastname1,firstname1
     lastname2,firstname2
    
    
    after second translation:
    lastname1,firstname1
    lastname2,firstname2
    
    
    after third iteration:
    firstname1.lastname1
    firstname2.lastname2
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search