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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I split a single-line input into five lines?
# 1  
Old 03-23-2014
Question How do I split a single-line input into five lines?

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

Expected Output:
Code:
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

# 2  
Old 03-23-2014
Code:
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

# 3  
Old 03-23-2014
Question

Quote:
Originally Posted by ahamed101
Code:
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.
Code:
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

# 4  
Old 03-23-2014
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?

Code:
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

# 5  
Old 03-23-2014
Quote:
Originally Posted by ahamed101
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?

Code:
root@maximus:/tmp# 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

         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:
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`

# 6  
Old 03-23-2014
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
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 03-23-2014
Quote:
Originally Posted by ahamed101
Did you try the code which I gave to see if it works for you without integrating it to your script?
Yes. It works perfectly.
Quote:
I think what you are trying to do here is, get a input and display the details of that particular user, is that so?
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split: File into multiple and keeping the same 3 lines from input into all output files

The following code will split the infile into multiple files. However, I need it to insert the same first 3 lines from the original input file into each splitted file. How do I modify my script below to do so: print -n "Enter file name to split? " ; read infile if then echo "Invalid file... (4 Replies)
Discussion started by: mrn6430
4 Replies

2. Shell Programming and Scripting

Input two variable on single line

Can we input two variable on single line that separate by space example user input "list jpg" it will list all jpg files in current directory (3 Replies)
Discussion started by: guidely
3 Replies

3. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

4. Shell Programming and Scripting

Split the a single line into two

Hi all, I wanted to split a single line into two line. For example: A/B/C 10 i want this output var1 = A/B/C var2 = 10 How do i get this. (2 Replies)
Discussion started by: ch33ry
2 Replies

5. Shell Programming and Scripting

Two Input Lines Into Single Output Line (CSV)

Hi all, My search karate must be weak because I'm about certain something very like this has been asked and answered here many times. I'll give you the exact scenario I've wasted a few hours of my Saturday on: :wall: I'm trying to read through a very large number (~200) of router and... (28 Replies)
Discussion started by: svermill
28 Replies

6. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

7. Shell Programming and Scripting

Split the single file lines into multiple files

Let's assume that I have a file name called ‘A' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow. INPUT: Input file name A 1 2 3 4 5 6 7 8 9 ........100 Output: 4 output files (x,y,z,w) File x should contains (Skip 4 lines)... (15 Replies)
Discussion started by: subbarao25
15 Replies

8. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

9. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

10. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies
Login or Register to Ask a Question