Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Parsing a file based on positional constraints

$
0
0
I have a list file1 like
Code:

dog
cow
fox
cat
fish
duck
crow

I want to classify the elements of file1 based on constrains applied on file2. Additionally the number of elements (words) in the each line of file2 is not fixed. This is my file2
Code:

cow cat fox dog
cow fox dog
fish crow fox dog cat
cat dog duck
cow

explanation of position constrains:
- For searching first line of file1 "dog" in file 2, first its is needed to check if is first or last element of the line1 in file2 then again if is first or last element of the line2 in file2 and so on till the end of file. If the position of "dog" occurs every time (whenever it occurs) as a first or last position then it will go to a new file called file_external. If not then it will go to new file file_internal. In this case dog also comes at line 3 position 4 so it will move to file_internal.
- The second element in file1 is cow. In the file2 cow is every-time at 1st or last position (whenever occurs), so cow will append to file_external.
- fox comes every time in middle positions so it will be in file_internal.
- cat comes in middle, end and first positions, so will append to file_internal.
- fish and duck will move to file_external as these are strictly as first or last element.
- crow will fly into file_internal.
I am looking for help to do this with awk/perl.

Viewing all articles
Browse latest Browse all 16232

Trending Articles