943,907 Members | Top Members by Rank

Ad:
May 13th, 2007
0

awk script

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
migg_21 is offline Offline
5 posts
since May 2007
May 13th, 2007
0

Re: awk script

Click to Expand / Collapse  Quote originally posted by migg_21 ...
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>
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
May 13th, 2007
0

Re: awk script

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
migg_21 is offline Offline
5 posts
since May 2007
May 13th, 2007
0

Re: awk script

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.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
May 14th, 2007
0

Re: awk script

thanks agian,
ghostdog74 I wounder if you could expand on the loop, as I have difficulty with loops , and understanding them,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
migg_21 is offline Offline
5 posts
since May 2007
May 14th, 2007
0

Re: awk script

Can you expline more awk script?
Reputation Points: 5
Solved Threads: 5
Junior Poster
vssp is offline Offline
197 posts
since Jul 2006
May 14th, 2007
0

Re: 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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 14th, 2007
0

Re: awk script

#!/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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
migg_21 is offline Offline
5 posts
since May 2007
May 14th, 2007
0

Re: awk script

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
migg_21 is offline Offline
5 posts
since May 2007
May 14th, 2007
0

Re: awk script

Shell Scripting Syntax (Toggle Plain Text)
  1. counter=1
  2. for counter in 1 2 3 4 #say we rename your files with increasing numbers.
  3. do
  4. awk 'BEGIN{SEP="/ "}
  5. {print NR SEP $0}' file > file$counter.txt
  6. done
output:
Shell Scripting Syntax (Toggle Plain Text)
  1. #./test.sh
  2. # ls -1
  3. file4.txt
  4. file3.txt
  5. file2.txt
  6. file1.txt

all in awk:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN{ SEP="/ "; counter=1
  2. while (counter<5) { #assume you want to generate 4 files
  3. while ( ( getline line < "file" ) > 0 ) { #get every line from file, put into variable "line"
  4. print (counter SEP line ) > "file"counter".datmike"
  5. }
  6. close("file")
  7. counter+=1 #increase counter so that it can be used in next file name
  8. }
  9. } #end BEGIN
  10.  
Last edited by ghostdog74; May 14th, 2007 at 2:12 pm.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: add a 0 at end of data in a text file
Next Thread in Shell Scripting Forum Timeline: find online users





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC