i have a small problem. i need to sort a file according to the certain starting string of the file. for example in this form according to digits 04, 05 or 06:
04..............
................
................
05..............
................
................
06..............
................
................ etc..
here is my awk code: http://pastebin.com/dLsWkV3q
or just here:
echo "starting...input file"
read file
echo "reading file..."
echo "... now sorting..."
cat $file | awk '{
if($1=="04"){
print >> "04_file.txt";
}
if($1=="05"){
print >> "05_file.txt";
}
if($1=="06"){
print >> "06_file.txt";
}
}'
echo "finished, bye?"
read wait
echo "bye"
the goal is that i need multiple files as a result which contain only corresponding blocks, for the example above: as a result i will have 3 files. 04_file.txt, 05_file.txt and 06_file.txt. AND 05_file.txt doesnot have any line from 04 block. final 04_file.txt file will have only this:
04..............
................
................
my problem is that it is saving other blocks also into 04_file.txt..
i will appreciate any help. thanks a lot
I assume that the only lines starting with 04, 05 etc. are the lines separating the different blocks:
awkis basically an environment to execute actions depending on patterns. Every pattern-action statement has the formIn its most simple form a pattern is a regular expression matching the current input line. BEGIN is a special case which “matches” before the input is read and there is also an END “pattern” which gets executed after the input file is consumed.
On execution
awkreads the input file line by line and executes all actions where the pattern matches the line. In the code above themodevariable is set if the input line begins (^) with 04, 05 etc. The last line (without the pattern) matches to all lines and just writes the whole line to the corresponding file.I’ll try to summarize this in some pseudo code:
If you want to go further have a look at the man page or browse the web for
awkintroduction. There you also can learn more about the pattern part which can do much more than just match the current input line as in the example above.In case all blocks start with two digits (any digits) the code above can be shortened to