Read 2 input and produce it in single ouput?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read 2 input and produce it in single ouput?
# 1  
Old 03-02-2015
Read 2 input and produce it in single ouput?

Hi all.
I’ve 2 inputs here and would like to produce it in single ouput. I’ve drafted simple shell script but not sure how to put all this together. The final output should be “GROUP-XYZ” instead of “TEST”
Please advise.

INPUT1
Code:
GROUP-XYZ

INPUT2
Code:
type8code0@box:~/dbedit$ cat port.txt
80
443
type8code0@box:~/dbedit$


Expected output
Code:
type8code0@box:~/dbedit$ ./dbedit-script-service-group
   create service_group GROUP-XYZ
   create service_group GROUP-XYZ '' services:tcp80
   create service_group GROUP-XYZ '' services:tcp443
   type8code0@box:~/dbedit$

DRAFT SHELL SCRIPT
Code:
#!/bin/bash
   
   awk '{print "tcp"$1}' port.txt > temp-port.txt
   
   awk '{
   print "create service_group TEST";
   print "create service_group TEST \x27\x27 services:"$1;
   }' temp-port.txt

Unfortunately, this is not the expected output. Need to fix something on the draft code so that expected output can be produced. Please advise.
Code:
type8code0@box:~/dbedit$   ./dbedit-script-service-group
   create   service_group TEST
   create   service_group TEST '' services:tcp80
   create   service_group TEST
   create service_group   TEST '' services:tcp443
   type8code0@box:~/dbedit$


Last edited by Scrutinizer; 03-02-2015 at 07:11 AM.. Reason: Removed spurious formatting; Additional code tags for input samples
# 2  
Old 03-02-2015
Hi, maybe this could work for you.

Code:
awk '
BEGIN { print "create service_group TEST"; }
{ print "create service_group TEST \x27\x27 services:tcp"$1 }' port.txt

This User Gave Thanks to Chirel For This Post:
# 3  
Old 03-02-2015
Quote:
Originally Posted by Chirel
Hi, maybe this could work for you.

Code:
awk '
BEGIN { print "create service_group TEST"; }
{ print "create service_group TEST \x27\x27 services:tcp"$1 }' port.txt

This is definitely better than my initial draft code Smilie
Is it possible to replace “TEST" with any variable?

E.g.
Code:
Please type your new service group name: GROUP-XYZ

Final Output:
Code:
create service_group GROUP-XYZ
  create service_group GROUP-XYZ '' services:tcp22
  create service_group GROUP-XYZ '' services:tcp80
  create service_group GROUP-XYZ '' services:tcp443

# 4  
Old 03-02-2015
With awk you could try something like this:
Code:
awk 'NR==FNR{A[$1]; next} {print s, $1; for(i in A) print s, $1, t i}' s="create service_group" t="'' services:tcp" file2 file

But perhaps shell script would do just fine here?
Code:
while read service_group
do
  printf "create service_group %s\n" "$service_group"
  while read port
  do
    printf "create service_group %s '' services:tcp%s\n" "$service_group" "$port"
  done < file2
done < file1


Last edited by Scrutinizer; 03-02-2015 at 07:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-02-2015
Quote:
Originally Posted by Scrutinizer
With awk you could try something like this:
Code:
awk 'NR==FNR{A[$1]; next} {print s, $1; for(i in A) print s, $1, t i}' s="create service_group" t="'' services:tcp" file2 file

But perhaps a simple bit of shell script would do just fine here?
Code:
while read service_group
do
  printf "create service_group %s\n" "$service_group"
  while read port
  do
    printf "create service_group %s '' services:tcp%s\n" "$service_group" "$port"
  done < file2
done < file1

I've tested and both solutions work like a charm. Thanks a lot SmilieSmilie

Since file1 only contains single word, is it possible to use interactive way. I was thinking something like this.

Please let me know how to combine this code into yours.
Code:
type8code0@box:~/dbedit$ cat test
echo -n "Enter new group name : "
read g
echo "You entered: $g"
type8code0@box:~/dbedit$

Code:
type8code0@box:~/dbedit$ ./test
Enter new group name : GROUP-XYZ
You entered: GROUP-XYZ
type8code0@box:~/dbedit$

# 6  
Old 03-02-2015
You're welcome... You could adjust the shell loop like so, leaving out the first input file and reading from the terminal instead..
Code:
while 
  printf "Enter new group name : "
  read service_group
do
  printf "You entered: %s\n" "$service_group"
  printf "create service_group %s\n" "$service_group"
  while read port
  do
    printf "create service_group %s '' services:tcp%s\n" "$service_group" "$port"
  done < file2
done


Last edited by Scrutinizer; 03-02-2015 at 08:03 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-02-2015
Quote:
Originally Posted by Scrutinizer
You're welcome... You could adjust the shell loop like so, leaving out the first input file and reading from the terminal instead..
Code:
while 
  printf "Enter new group name : "
  read service_group
do
  printf "You entered: %s\n" "$service_group"
  printf "create service_group %s\n" "$service_group"
  while read port
  do
    printf "create service_group %s '' services:tcp%s\n" "$service_group" "$port"
  done < file2
done

This is perfect. Thanks again. SmilieSmilieSmilieSmilie

Quick question, how to stop this loop besides using combination of Ctrl + C?

Code:
type8code0@box:~/dbedit$ ./test
Enter new group name : XYZ
You entered: XYZ
create service_group XYZ
create service_group XYZ '' services:tcp22
create service_group XYZ '' services:tcp80
create service_group XYZ '' services:tcp443
Enter new group name :
You entered:
create service_group
create service_group  '' services:tcp22
create service_group  '' services:tcp80
create service_group  '' services:tcp443
Enter new group name : ABC
You entered: ABC
create service_group ABC
create service_group ABC '' services:tcp22
create service_group ABC '' services:tcp80
create service_group ABC '' services:tcp443
Enter new group name :

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

2. Shell Programming and Scripting

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... (10 Replies)
Discussion started by: Camrikron
10 Replies

3. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

4. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

5. Shell Programming and Scripting

Append ouput in a single line

Hi Guys, I need to append some data to a new file, but i need to make sure that when i use to >> command again.I dont go to the new line. i append the data on the same line. Please help regarding the same. Thanks in advance..!!! (3 Replies)
Discussion started by: jaituteja
3 Replies

6. 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

7. 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

8. Shell Programming and Scripting

read single chars

This reads single keystrokes and produces an output: #! /bin/bash while : ; do read -s -n 1 >/dev/null 2>&1 echo ${REPLY} done | awk '{print}' This second one don't. Even though these examples make no sense; the real code is more complicated. Who knows what the problem is... (2 Replies)
Discussion started by: elbrand
2 Replies

9. Shell Programming and Scripting

Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"): e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA When I read this record and either echo/print to screen or write to... (4 Replies)
Discussion started by: NinersFan
4 Replies

10. Shell Programming and Scripting

single input shell script?

hey, i'm trying to write a shell script which accepts: operand operator operand then, the script would see which operator it is (using case) and calculate it... but i dont know how to do it correctly with $1 $2 $3... (eliminating accepting separate inputs) (1 Reply)
Discussion started by: quipy
1 Replies
Login or Register to Ask a Question