Echo with loop

Hello
i have a file with this format:

ip.txt content:

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)

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

im confuses how add loop when try to echo.

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

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

1 Like

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:

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

OS/LANG: centos/shell

Try

awk '                                                                      
        {split ($0, IP, "/")
         printf "internal=yes\ninternalip=%s\ninternal.dest:%s port=%s\n", IP[1], IP[1], IP[2] > ("FILE" NR)
        }
' file
1 Like

Unless you know that file will never contain more than about nine lines, I would add a close() statement to that:

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.

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

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 ):

nice job,
is it possible to have thsis logic without awk ?
like with source, read, while .... ?

Of course. But it is way less efficient. What be the reason you want / have to avoid awk ?

mm im not familiar with awk options, now with this code i have to read more about awk

Which might not be a fault . . . and pay off soon.
Nevertheless, try

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.

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:

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:

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

Another shell script

i=0; while IFS=/ read ip port
do
  echo "\
internal=yes
internalip=$ip
internal.test: $ip port=$port" >"ip$((i+=1)).txt"
done < ip.txt