How do I split a single-line input into five lines?

Example input:

John:Shepherd:770-767-4040:U.S.A:New York
Mo Jo:Jo Jo: 666-666-6666:U.S.A:Townsville

Expected Output:

First Name: John
Last Name: Shepherd
Phone Number: 770-767-4040
Country: U.S.A
State: New York

First Name: Mo Jo
Last Name: Jo Jo
Phone Number: 666-666-6666
Country: U.S.A
State: Townsville
awk '
  BEGIN{ fmt="%20s%20s\n" }
  function prt()
  {
    printf(fmt, "First Name:", data[1])
    printf(fmt, "Last Name:", data[2])
    printf(fmt, "Phone Number:", data[3])
    printf(fmt, "Country:", data[4])
    printf(fmt, "State:", data[5])
    printf("\n")
  }
  { split($0, data, ":"); prt() }' infile

Thanks for the help, but I still have a minor problem.The output is displayed on a single line.

First Name: John Last Name: Shepherd Phone Number: 770-767-4040 Country: U.S.A State: New York First Name: Mo Jo Last Name: Jo Jo Phone Number: 666-666-6666 Country: U.S.A State: Townsville

Well, it works for me. Did you omit the new line in the code? Are you using the exact code I have given? which is your OS?

root@maximus:/tmp# awk '
   BEGIN{ fmt="%20s%20s\n" }
   function prt()
   {
     printf(fmt, "First Name:", data[1])
     printf(fmt, "Country:", data[4])
     printf(fmt, "Last Name:", data[2])
     printf(fmt, "Phone Number:", data[3])
     printf(fmt, "Country:", data[4])
     printf(fmt, "State:", data[5])
     printf("\n")
   }
   { split($0, data, ":"); prt() }'  infile

         First Name:                John
          Last Name:            Shepherd
       Phone Number:        770-767-4040
            Country:               U.S.A
              State:            New York

         First Name:               Mo Jo
          Last Name:               Jo Jo
       Phone Number:        666-666-6666
            Country:               U.S.A
              State:          Townsville

I'm using ElementaryOS. Here's my code:

#!/bin/bash
echo -n "Please enter a name:"
read input
echo `grep -w "$input" list.txt | awk `

    BEGIN{ fmt="%20s%20s\n" }
    function prt()
    {
         printf(fmt, "First Name:", data[1])
         printf(fmt, "Country:", data[4])
         printf(fmt, "Last Name:", data[2])
         printf(fmt, "Phone Number:", data[3])
         printf(fmt, "Country:", data[4])
         printf(fmt, "State:", data[5])
         printf("\n")
   }
   { split($0, data, ":"); prt() }'  list.txt`

This is not right. Did you try the code which I gave to see if it works for you without integrating it to your script?
I think what you are trying to do here is, get a input and display the details of that particular user, is that so?

Please answer both the questions.
Thank You

1 Like

Yes. It works perfectly.

Yes. I'm trying to search for all users who have a first name matching the input and display the details of the matching users.

The single line output is because of field splitting (there are no double quotes around the backquotes)..
@OP: try using Ahamed's suggestion like this:

output=$(grep -w "$input" list.txt | awk '

    BEGIN{ fmt="%20s%20s\n" }
    function prt()
    {
         printf(fmt, "First Name:", data[1])
         printf(fmt, "Country:", data[4])
         printf(fmt, "Last Name:", data[2])
         printf(fmt, "Phone Number:", data[3])
         printf(fmt, "Country:", data[4])
         printf(fmt, "State:", data[5])
         printf("\n")
   }
   { split($0, data, ":"); prt() }'  )

echo "$output"

And there seems to be a wrong quote indicated in red above.. And list.txt was used twice..

1 Like

Use -e with the echo in the above code as in echo -e "$output"

Or another approach

#!/bin/bash

read -p "Please enter a name : " name
awk -F":" '
  BEGIN{ fmt="%20s%20s\n" }
  function prt()
  {
    printf(fmt, "First Name:", $1)
    printf(fmt, "Last Name:", $2)
    printf(fmt, "Phone Number:", $3)
    printf(fmt, "Country:", $4)
    printf(fmt, "State:", $5)
    printf("\n")
    exit
  }
  $1==name{ prt() }' name=$name list.txt
1 Like

@ahamed101 and Scrutinizer

Thanks for the help guys. They both worked. Sorry for the silly mistakes, bash scripting and programming in general is rather new to me.

awk 'BEGIN{FS = ":"; OFS = "\n"; ORS = "\n\n"}
  $1 == /patt/ {print "First Name: " $1, "Last Name: " $2, "Phone Number: " $3, "Country: " $4, "State: " $5}' patt='match_fname' file