awk '{print NR SEP $0}' SEP="/ " exfile1.txt > try2.txt

here is a awk scritp which copies the contents form one to anthor
it works well, but how do I change it to copy the contents in to several files ? can any one help please

Recommended Answers

All 9 Replies

awk '{print NR SEP $0}' SEP="/ " exfile1.txt > try2.txt

here is a awk scritp which copies the contents form one to anthor
it works well, but how do I change it to copy the contents in to several files ? can any one help please

you can invoke the awk script again, or just use cp,
like: cp try2.txt <newfilename>

thanks ghostdog74 but the script is a shell program ,sorry I forgot to say,the idea is to use this shell script to send copies of text held in one file to sevral other files at the same time! such try2.txt and try3.txt and so on

awk '{print NR SEP $0}' SEP="/ " exfile1.txt > try2.txt

any suggestion would be great

yes, you can put those into a script. use loops to create a counter for your files.
eg psedocode

count=1
awk '{print NR SEP $0}' SEP="/ " exfile1.txt
for counter in the range 1 to the number of files:
cp exfile1.txt try$counter.txt

something like that.

thanks agian,
ghostdog74 I wounder if you could expand on the loop, as I have difficulty with loops , and understanding them,

Can you expline more awk script?

Uhhm, awk is a really large subject. O'Reilly publishing http://www.oreilly.com/ produces an entire book (and a good one at that) dedicated to sed and awk. Try that.

#!/bin/sh

# outputs the contents of the file(exfile1.txt) with line numbers prepended to e$
# sep is a variable separator (sep="/ ".)
# > redirects the output to another file(try1.txt)

awk '{print NR SEP $0}' SEP="/ " exfile1.txt > try2.txt

here is the awk script ,it doe's three things
1 it numbers the lines in a text document in the file,exfile1.txt with a seperator, inthis case a back slash , and it redirects the output the text to another file called try2.txt. what Iam trying to do is to redirect the output to sevral file with out having to use the cp command or to invoke the script more than once, note Iam very new to programming so am not to good yet , and due to my dissablty I find it difficult to interprate a lot of the books which are on the market

I've read the book you suggest, you are correct ,it is a good book , but like alot of books on programming its not written in a way that I can interprete well, it can be like driving in the fog, sometimes you know the path you should go, but you just can't see it , it's hard to explain, Iwish Icould ,some people need more than a book to learn,then need help from those who are more able to interprate the written word!

counter=1
for counter in 1 2 3 4 #say we rename your files with increasing numbers.
do
  awk  'BEGIN{SEP="/ "}
      {print NR SEP $0}'  file > file$counter.txt
done

output:

#./test.sh
# ls -1
file4.txt
file3.txt
file2.txt
file1.txt

all in awk:

awk  'BEGIN{  SEP="/ "; counter=1
        while (counter<5) {  #assume you want to generate 4 files
	    while ( ( getline line < "file" ) > 0 ) { #get every line from file, put into variable "line"
	        print (counter SEP line ) > "file"counter".datmike"
	    }
        close("file") 
	counter+=1 #increase counter so that it can be used in next file name
        }
       } #end BEGIN
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.