LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2015, 04:04 AM   #1
firewallengineer
LQ Newbie
 
Registered: Jul 2013
Posts: 19

Rep: Reputation: Disabled
Shell Script: How to manipulate output files


Hi all, this is my draft script, but I’ve a small problem.

Code:
fweng@box:~/$ cat script
printf "Insert the Network Group name: "
read G
printf "Your Network Group name is: %s\n" "$G"
printf "create network_object_group %s\n" "$G"
while read host
do
  printf "addelement network_objects %s '' network_objects:host%s\n" "$G" "$host"
done < host.txt > output
fweng@box:~/$
Code:
fweng@box:~/$ cat host.txt
10.5.3.8
172.16.3.210
192.168.0.150
192.168.7.4
fweng@box:~/$
Code:
fweng@box:~/$ ./script
Insert the Network Group name: TEST
Your Network Group name is: TEST
create network_object_group TEST
fweng@box:~/$
Code:
fweng@box:~/$ cat output
addelement network_objects TEST '' network_objects:host10.5.3.8
addelement network_objects TEST '' network_objects:host172.16.3.210
addelement network_objects TEST '' network_objects:host192.168.0.150
addelement network_objects TEST '' network_objects:host192.168.7.4
fweng@box:~/$
The final output is supposed to be like this. I want to have “create network_object_group TEST” in the output file as well.
Please let me know how to do this.

Code:
create network_object_group TEST
addelement network_objects TEST '' network_objects:host10.5.3.8
addelement network_objects TEST '' network_objects:host172.16.3.210
addelement network_objects TEST '' network_objects:host192.168.0.150
addelement network_objects TEST '' network_objects:host192.168.7.4

Last edited by firewallengineer; 05-25-2015 at 04:57 AM.
 
Old 05-25-2015, 06:42 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
The first line of your script doesn't specify the interpreter.
 
Old 05-25-2015, 07:44 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Add another printf line to your cycle.
 
1 members found this post helpful.
Old 05-25-2015, 08:36 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Why not simply redirect your printf into the file first? I would add I see no benefit of printf here that couldn't be handled by a simple echo.
 
Old 05-26-2015, 02:32 AM   #5
firewallengineer
LQ Newbie
 
Registered: Jul 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Add another printf line to your cycle.
Thanks for your response. This is the output is I add another printf to the cycle. But this is not the expected output as I just want “create network_object_group” to be on the the first line only.

NEW CODE
Code:
fweng@box:~/$ cat test
printf "Insert the Network Group name: "
read G
printf "Your Network Group name is: %s\n" "$G"
printf "create network_object_group %s\n" "$G"
while read host
do
        printf "create network_object_group %s\n" "$G"
        printf "addelement network_objects %s '' network_objects:host%s\n" "$G" "$host"
done < host.txt > output
fweng@box:~/$
Code:
fweng@box:~/$ ./test
Insert the Network Group name: TEST-123
Your Network Group name is: TEST-123
create network_object_group TEST-123
fweng@box:~/$
Code:
fweng@box:~/$ cat output
create network_object_group TEST-123
addelement network_objects TEST-123 '' network_objects:host10.5.3.8
create network_object_group TEST-123
addelement network_objects TEST-123 '' network_objects:host172.16.3.210
create network_object_group TEST-123
addelement network_objects TEST-123 '' network_objects:host192.168.0.150
create network_object_group TEST-123
addelement network_objects TEST-123 '' network_objects:host192.168.7.4
fweng@box:~/$

Last edited by firewallengineer; 05-26-2015 at 02:35 AM.
 
Old 05-26-2015, 03:20 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then add a single printf before the cycle...
 
Old 05-26-2015, 03:35 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably only a redirection issue:
Code:
printf "create network_object_group s\n" "$G" > output
while .... < host.txt >> output
 
1 members found this post helpful.
Old 05-26-2015, 07:13 AM   #8
firewallengineer
LQ Newbie
 
Registered: Jul 2013
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
probably only a redirection issue:
Code:
printf "create network_object_group s\n" "$G" > output
while .... < host.txt >> output
Thanks, but this is not the expected output

Code:
fweng@box:~/$ cat test
printf "Insert the Network Group name: "
read G
printf "Your Network Group name is: %s\n" "$G"
printf "create network_object_group %s\n" "$G"
while read host
do
        printf "create network_object_group %s\n" "$G" > output
        printf "addelement network_objects %s '' network_objects:host%s\n" "$G" "$host"
done < host.txt >> output
fweng@box:~/$
RUN CODE
Code:
fweng@box:~/$ ./test
Insert the Network Group name: TEST
Your Network Group name is: TEST
create network_object_group TEST
fweng@box:~/$
OUTPUT
Code:
fweng@box:~/$ cat output
create network_object_group TEST
addelement network_objects TEST '' network_objects:host192.168.7.4
fweng@box:~/$
 
Old 05-26-2015, 07:23 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
still another redirection problem, the second printf "create network_object_group %s\n" "$G" > output not only unnecessary, but that line makes some troubles too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need a BASH for loop for processing many files and naming the output files Jeff9 Linux - Newbie 13 05-02-2014 12:36 PM
[SOLVED] awk question - read in txt files, offset data by given amount, output new txt files pomico Programming 19 09-17-2012 11:43 AM
[SOLVED] How to run a single program on consecutive files and send output to new files. WeiYi Linux - Newbie 7 06-29-2012 11:29 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
output to two files ust Linux - Newbie 2 07-19-2008 02:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration