How to scan a file with 2 different field separators?
I have a linux assignment - I'm not asking for any code, just an idea - and the task is to scan a text file and sort it first by one field separator, than every field to sort it by another separator. Awk looks like a good choice but I don't know how to use 2 FS in one awk script or to call an awk script from another awk script.
|
If the scanning process is sequential (that is the output of the first scanning represents in toto the input for the second scanning) you can pipe two awk calls, as in
Code:
awk -F, 'some_awk_code_here' filename | awk -F: 'some_other_awk_code_here' |
For the hell of it, here's another suggestion. We'll go with the same assumption -- that one delimiter is a comma and the other is a colon.
First, run it through tr to change all delimiters to the same type: Code:
tr ',' ':' < some-data-file [ caveat: It's entirely possible I am misunderstanding what you are trying to do. I'm not too sure what you mean when you say "sort by separator". ] |
Here's an extract from the info file for sort which may suggest a way to accomplish your task.
Code:
* Sort a set of log files, primarily by IPv4 address and secondarily |
Quote:
anomie, I cannot transform every separator because I need different functions to be run on different fields. PTrenholme, also a good idea, but I don't want to sort the fields. thank you all. |
Quote:
Code:
BEGIN { FS = "," } Code:
split($2,names,":") A correct answer to your question requires the knowledge of the task you have to accomplish. By the way, this is a general issue for the great awk language! |
Hi,
(GNU) Awk accepts multiple separators. awk -F",|:" '{ .........}' infile The , and the : are used as separator. Code:
$ cat infile |
thanks guys but I cannot use your ideas:
colucix: I need the fields inside the big field in order and using arrays I cannot acomplish this. druuna: I need to ditinguish between the fields separated by ":" and the ones separated by "," |
cdog,
Post some sample data and how you want the results to come out. That'll make this less ambiguous and get you help quicker. |
Hi,
If you need to distinguish between the field separators, it seems that PTrenholme gave the answer (post #4). Sort can do this. Man sort or info sort for details. |
druuna I don't want to sort the input. is there a way to use sort with its options and not sort the input?
anomie, here is an example: january,february,june:sunday,saturday,monday. the output will be 1,2,6:1,7,2; something like that |
colucix, I take it back your idea works, runnning throw the array using for(index in array) does not get the elements in order but using for (i=1;i=array_size;i++) does. thanks
|
Hi,
If your input has a fixed layout you could use something like this (shortened, but you probably get the idea): Code:
#!/bin/bash |
druuna the input is not fix, but I managed to solve it using something similar. thanks.
|
All times are GMT -5. The time now is 02:07 AM. |