| | |
awk script
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
•
•
•
•
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
like: cp try2.txt <newfilename>
•
•
Join Date: May 2007
Posts: 5
Reputation:
Solved Threads: 0
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
awk '{print NR SEP $0}' SEP="/ " exfile1.txt > try2.txt
any suggestion would be great
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
----------------------------------------------
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
•
•
Join Date: May 2007
Posts: 5
Reputation:
Solved Threads: 0
#!/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
# 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
•
•
Join Date: May 2007
Posts: 5
Reputation:
Solved Threads: 0
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!
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
Shell Scripting Syntax (Toggle Plain Text)
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
Shell Scripting Syntax (Toggle Plain Text)
#./test.sh # ls -1 file4.txt file3.txt file2.txt file1.txt
all in awk:
Shell Scripting Syntax (Toggle Plain Text)
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
Last edited by ghostdog74; May 14th, 2007 at 2:12 pm.
![]() |
Similar Threads
- awk "for loop" and variables (Shell Scripting)
- awk script to write a file (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: add a 0 at end of data in a text file
- Next Thread: find online users
| Thread Tools | Search this Thread |






