Echo with loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo with loop
# 8  
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

can you explain what "NR" do ?
and how can i have this part IP[1], IP[1], IP[2] in loop?
i mean if ip file has 200 row, 200 file create and if it has 127 row, 127 file create, it depends on line number of ip file
# 9  
Old 01-23-2018
In awk, NR is the "number of record", i.e. row or line No. Above small script will loop through your input file, creating n files for n lines in file, i.e. FILE1, FILE2, ... FILEn. Did you check? Please consider Don Cragun's hint if your line count exceeds the system parameter OPEN_MAX (getconf OPEN_MAX):
# 10  
Old 01-23-2018
nice job,
is it possible to have thsis logic without awk?
like with source, read, while .... ?
# 11  
Old 01-23-2018
Of course. But it is way less efficient. What be the reason you want / have to avoid awk?
# 12  
Old 01-23-2018
mm im not familiar with awk options, now with this code i have to read more about awk

Last edited by nimafire; 01-23-2018 at 01:45 PM..
# 13  
Old 01-23-2018
Which might not be a fault . . . and pay off soon.
Nevertheless, try
Code:
cat -n file | while read NR IP
  do cat <<EOF > FILE$NR
internal=yes
internalip=${IP%/*}
internal.dest:${IP%/*} port=${IP#*/}
EOF
    done

This is not a uuoc (useless use of cat) as it supplies the line Nos and eliminates the necessity of shell arithmetics.

Last edited by RudiC; 01-23-2018 at 03:11 PM..
# 14  
Old 01-23-2018
I agree with RudiC that you would find life much easier working problems like this if you take a little time to learn how to use awk. You might also try the following which doesn't need either invocation of cat and just uses shell built-ins:
Code:
NR=0
while IFS=/ read -r ip port
do	NR=$((NR + 1))
	printf 'internal=yes\ninternalip= %s\ninternal.dest: %s port=%s\n' \
	    "$ip" "$ip" "$port" > FILE$NR
done < ip.txt

which produces output filenames like FILE1, FILE2, ... FILE1234, etc., or if you would prefer output filenames like FILE0001, FILE0002, ... FILE1234, etc. you could try:
Code:
NR=0
while IFS=/ read -r ip port
do	NR=$((NR + 1))
	FILENAME=$(printf 'FILE%04d' $NR)
	printf 'internal=yes\ninternalip= %s\ninternal.dest: %s port=%s\n' \
	    "$ip" "$ip" "$port" > "$FILENAME"
done < ip.txt

Either of these should work with any shell that attempts to follow the POSIX standard's shell syntax (such as bash, ksh, and several others; but not csh and its derivatives).
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