Echo with loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo with loop
# 1  
Old 01-23-2018
Echo with loop

Hello
i have a file with this format:

ip.txt content:
Code:
192.168.1.1/2020
192.136.1.2/2028
192.168.1.10/3047

....

need to create 1000 files and each files content, import data from ip.txt line (first file with first line data, second file with second line...etc)

Code:
internal=yes
internalip= (IP FROM FILE)
internal.dest: (IP FROM FILE) port=(PORT FROM FILE)

im confuses how add loop when try to echo.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-23-2018 at 06:11 AM.. Reason: Added CODE tags.
# 2  
Old 01-23-2018
Any attempts from your side? Any preferred tools?
# 3  
Old 01-23-2018
Quote:
Originally Posted by RudiC
Any attempts from your side? Any preferred tools?
what you mean ?
this script try to add ip and port to our firewall
im confused how seprate ip and port from source file and add them by echo to file
# 4  
Old 01-23-2018
Dear nimafire,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 01-23-2018
mm this is my personal script
i have create file with 1000 line with this format:
ip/port like 192.168.1.1/1055, let say ip.txt and create a core.sh file

main part of this project is when core.sh execute, it read ip and port from ip.txt and create 1000 files with this pattern:

config1 config2 config3

and echo this to each of files:

Code:
internal=yes
internalip= $ip
internal.dest: $ip port=$port

OS/LANG: centos/shell
# 6  
Old 01-23-2018
Try
Code:
awk '                                                                      
        {split ($0, IP, "/")
         printf "internal=yes\ninternalip=%s\ninternal.dest:%s port=%s\n", IP[1], IP[1], IP[2] > ("FILE" NR)
        }
' file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-23-2018
Quote:
Originally Posted by RudiC
Try
Code:
awk '                                                                      
        {split ($0, IP, "/")
         printf "internal=yes\ninternalip=%s\ninternal.dest:%s port=%s\n", IP[1], IP[1], IP[2] > ("FILE" NR)
        }
' file

Unless you know that file will never contain more than about nine lines, I would add a close() statement to that:
Code:
awk '                                                                      
        {split ($0, IP, "/")
         printf "internal=yes\ninternalip=%s\ninternal.dest:%s port=%s\n", IP[1], IP[1], IP[2] > ("FILE" NR)
         close("FILE" NR)
        }
' file

to keep from running out of file descriptors.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo For ... Loop output into a file

Hi, All: I have one for ... loop code to generate a list of rename statement. However, echo the loop output on screen is OK. But store the echo output into a file is not working. So come here to seek help. My basic code is: ! /bin/ksh echo > DAS_VetFed.txt dir=/mydirectory cd $dir files=`ls... (6 Replies)
Discussion started by: duke0001
6 Replies

2. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

3. Shell Programming and Scripting

BASH - Need to echo for loop output to one line

I'm trying to echo the release version of some of our Linux servers. Typically I do these types of things by "catting" a text file with the host names, "ssh-ing" to the host and running my string. This is what I've written for i in `cat versions.txt` ; do echo $i ; ssh $i cat /etc/issue |... (5 Replies)
Discussion started by: lombardi4851
5 Replies

4. Shell Programming and Scripting

Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date : for i in `ls $replog/*.gz` do echo " $i " `echo $i date +%d:%m:%Y` `echo $datee `>> $replog/mrnet.log mv $i /var/log/vieux-logs done I need to echo... (10 Replies)
Discussion started by: Dabless
10 Replies

5. Shell Programming and Scripting

Script to loop line in a file and add info or do echo

I have a record.txt it will update weekly, and it could be 2 lines or more ... it just echo each line to the script san jose,23.34% tampa,2.15% dallas,30.20% seattle,44.29% Unknown,16.72% How do i write a shell script to give me a test.pl or bash file which contain #!/home/perl... (8 Replies)
Discussion started by: sabercats
8 Replies

6. UNIX for Dummies Questions & Answers

skipping echo stmnt first time a while loop entered

one more question. I want to skip the first echo statement the first time the loop gets entered while #keep prompting for more funds until selling price achieved do echo "You have inserted a total of ${total_inserted} cents. Please insert $total_remaining more cents" echo... (1 Reply)
Discussion started by: danieldcc
1 Replies

7. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

8. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

9. Shell Programming and Scripting

Echo var1,var2,var3,var4,var5,var6 in loop help

Hello, I have created numerous variables called $mask1 $mask2... $maskN. I wish to echo these variables, below is the code for the two functions, the first creates the variables and the second echos them. It is the second function which does not work. The line (i believe to be wrong) is in... (1 Reply)
Discussion started by: rorey_breaker
1 Replies

10. Shell Programming and Scripting

Nested Loop to Echo Multiple Arrays

I have three arrays which hold three elements each. I have a fourth array which contains the names of those three arrays. I'm having difficulty creating a nested loop that can loop through each array and echo their values. script #!/bin/ksh # array of locations (usa, london, australia)... (1 Reply)
Discussion started by: yongho
1 Replies
Login or Register to Ask a Question