I am a newbie and heard from someone that using awk command doesn't workout right sometimes when we have while and for loops...but for me as of now it works fine without any problem...
Awk doesn't mysteriously stop working inside while loops, no.
I can only guess what your friend meant, but I'm assuming the lesson intended was "Don't run awk 30,000 times to process 10,000 lines". And that is certainly correct, not to mention an error I see new shell programmers making a lot. They see awk used to get a column and think "Oh, that's what awk is for", and use it everywhere. Things like ...
...are extremely silly. Whenever get asked 'why is my script slow', they're doing that.
Better way to do it would be
so you run awk 0 times instead of 30,000, and sed 0 times instead of 10,000...
But I'm using awk to process entire files. Nothing silly about that.
Last edited by Corona688; 09-06-2012 at 02:50 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
Nice Corona688, what you said makes sense especially if you are processing large files. How about if there are many columns like says 10 columns, what is the efficient way of doing that instead of declaring 10 variables?
Supposing the columns are separated by , there's a few things you can do.
I like the set -- feature a lot, it's a simple thing which exists in any Bourne shell, capable of replacing arrays for many purposes and actually simpler to use.
This should work in any shell:
You can also use IFS="," read -a arrayname to read elements into an array if you have BASH.
If your columns are separated by ordinary whitespace, leave off the IFS="" part of course.
Last edited by Corona688; 09-07-2012 at 12:27 PM..
Assuming nothing more than that the data is a valid text file, while the following traps may not trigger often, I think it's a good idea to mention them for the benefit of novices considering adding these techniques to their repertoire.
Quote:
Originally Posted by Corona688
I like the set -- feature a lot, it's a simple thing which exists in any Bourne shell, capable of replacing arrays for many purposes and actually simpler to use.
Anything that looks like a shell filename pattern (and the possibilities of encountering one increase with increasing shell capability) could modify the data. I would suggest protecting against the worst by using set to toggle globbing.
Quote:
Originally Posted by Corona688
You can also use IFS="," read -a arrayname to read elements into an array if you have BASH.
Whether reading into an array or a string variable, without -r, backslashes and newlines in the data will be lost and a valid text file could be rendered invalid (if its concluding newline is consumed by read).
Regards,
Alister
These 3 Users Gave Thanks to alister For This Post:
@mjf... i have quick question i kept the command u send me in a function i want it to return a value....
and then do return $RCODE and will use this RCODE in another if statement. ....my doubt is that can i do this change and will it work...
draghun9,
How you have coded the RCODE above will not work (try it). If you want to code this into a function, then why try to do in all one command line? Use an If/else condition to echo the return code based on results of each compare.
@MJF...
Yeah, i tried and returncode doesnt work....and the thing is even if one line is not authenticated, it should return the value with rcode=5.... as i said i am new to the scripting i am really struck at this part (no clue abt how to proceed with if/else for this requirement)...please help.
I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ?
Source File
*************
# file: /local/test_1
# owner: own
#... (4 Replies)
Hi Guys ,
we have one directory ...in that directory all files will be set on each day..
files must have header ,contents ,footer..
i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Hi,
I need to compare two text files with awk.
File1:
-------
chr1 43815007 43815009 COSM19193 REF=TG;OBS=AA;ANCHOR=G AMPL495041
chr1 43815008 43815009 COSM18918 REF=G;OBS=T;ANCHOR=T AMPL495041
chr1 115256527 115256528 ... (6 Replies)
I've two files with data like below:
file1.txt:
AAA,Apples,123
BBB,Bananas,124
CCC,Carrot,125
file2.txt:
Store1|AAA|123|11
Store2|BBB|124|23
Store3|CCC|125|57
Store4|DDD|126|38
So,the field separator in file1.txt is a comma and in file2.txt,it is |
Now,the output should be... (2 Replies)
EDIT: My problems have been solved thanks to the help of bartus11 and pravin27
This code is just to help me learn. It serves no purpose other than that.
Here's a sample csv that I'm working with -
#listofpeeps.csv
Jackie Chan,1954,M
Chuck Norris,1930,M
Bruce Lee,1940,M
This code is... (13 Replies)
Dear experts,
In a directory i have both *.TXT and *.txt files. I have a script-
for file in `ls *.txt`; do
mv $file /tmp/$file
How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find.
br//purple (4 Replies)
Hi,
I have two big .txt files.and i need to compare those two files and redirect it into some other file.
If any body wants to resolve this issue then i can send the two text files.
Need some quick responce.
Thanks,
prakash (10 Replies)
Hi Guru's,
I need to create 3 files with the contents "ABC" using single command.
Iam using:
echo "ABC" > file1.txt file2.txt file3.txt
the above command is not working. pls help me...
With Regards / Ganapati (4 Replies)
Hi,
I have some text files. I need a separate shell say 1.sh in which i can open a particular text file and compare with another txt file. For example:
1.log.txt contains
apple
ball
cat
goat
2.log.txt contains
goat
cat
lion
apple
fox
In my i.sh i need to write script to... (5 Replies)