awk script

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2007
Posts: 5
Reputation: migg_21 is an unknown quantity at this point 
Solved Threads: 0
migg_21 migg_21 is offline Offline
Newbie Poster

awk script

 
0
  #1
May 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: awk script

 
0
  #2
May 13th, 2007
Originally Posted by migg_21 View 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
you can invoke the awk script again, or just use cp,
like: cp try2.txt <newfilename>
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: migg_21 is an unknown quantity at this point 
Solved Threads: 0
migg_21 migg_21 is offline Offline
Newbie Poster

Re: awk script

 
0
  #3
May 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: awk script

 
0
  #4
May 13th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: migg_21 is an unknown quantity at this point 
Solved Threads: 0
migg_21 migg_21 is offline Offline
Newbie Poster

Re: awk script

 
0
  #5
May 14th, 2007
thanks agian,
ghostdog74 I wounder if you could expand on the loop, as I have difficulty with loops , and understanding them,
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 197
Reputation: vssp has a little shameless behaviour in the past 
Solved Threads: 5
vssp vssp is offline Offline
Junior Poster

Re: awk script

 
0
  #6
May 14th, 2007
Can you expline more awk script?
Thanks
VSSP
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,416
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 257
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: awk script

 
0
  #7
May 14th, 2007
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: migg_21 is an unknown quantity at this point 
Solved Threads: 0
migg_21 migg_21 is offline Offline
Newbie Poster

Re: awk script

 
0
  #8
May 14th, 2007
#!/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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: migg_21 is an unknown quantity at this point 
Solved Threads: 0
migg_21 migg_21 is offline Offline
Newbie Poster

Re: awk script

 
0
  #9
May 14th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: awk script

 
0
  #10
May 14th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC