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?
# 8  
Old 03-23-2014
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:
Code:
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..

Last edited by Scrutinizer; 03-23-2014 at 07:14 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 03-23-2014
Use -e with the echo in the above code as in echo -e "$output"

Or another approach

Code:
#!/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

This User Gave Thanks to ahamed101 For This Post:
# 10  
Old 03-23-2014
@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.
# 11  
Old 03-23-2014
Code:
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

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