|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
separating fields of a data file using awk
hi guys,
i need help regarding comparsion of 2 data files i Have 2 data files( both of them have 5 fields). I have to compare fields 1,2 of one file with field 1,2 another, to check some conditions. Im writing a script in awk langauge to do so. My problem is to extract the fields ($1,$2) of both files and place them in a separate file as 4 different fields. Ne takers for this problem.. |
|
#2
|
|||
|
|||
|
Code:
cut -d ' ' -f 1,2 myfile1 > f1 cut -d ' ' -f 1,2 myfile2 > f2 paste f1 f2 > file_wth_4_columns |
|
#3
|
|||
|
|||
|
jim
no, niet, nein, non ls -l >file demonstrate me how do you get the 4. col using cut -d' ' file ????
__________________
working on Solaris[5-9], preferred languages french and C. |
|
#4
|
|||
|
|||
|
Quote:
If you are looking for a helping hand, you will find it on the end of your arm (G. B. Shaw). This time I need some problems for my class of awk programming and here is my solution. It is tested on Linux under bash. If you are on Solaris, write nawk instead of awk: : # Script to read two files and compare first two fields # Generate the third file containing all four compared fields # [ $# -eq 3 ] || { echo "$0: call: $0 file1 file2 file_out" 1>&2 exit 10 } awk ' BEGIN { while (1) { # Get one line from file1 RC1 = getline <"'"$1"'" if (RC1 > 0) { P1 = $1 P2 = $2 } # Get one line from file2 RC2 = getline <"'"$2"'" if (RC1 > 0) { Q1 = $1 Q2 = $2 } # Compare the return codes if (RC1 == 0 && RC2 == 0) { exit 0 # Both input files equaly long } else if (RC1 == 0 && RC2 > 0) { printf "%s longer then %s\n", "'"$2"'", "'"$1"'" | "cat 1>&2" exit 2 } else if (RC1 > 0 && RC2 == 0) { printf "%s longer then %s\n", "'"$1"'", "'"$2"'" | "cat 1>&2" exit 1 } # Both files read # compare the fields P1 : Q1, P2 : Q2 and output them printf "%s %s %s %s\n", P1, P2, Q1, Q2 > "'"$3"'" } } ' The indentation gets lost here, but I hope, it is no problem. Try it and say your results. Regards zlutvsky ![]() |
|
#5
|
|||
|
|||
|
Quote:
YOU DONT ANSWER THE Q ls -l >file demonstrate me how do you get the 4. col using cut -d' ' file and a 30 line shell to do that ???? ![]() |
|
#6
|
|||
|
|||
|
Quote:
Dear guggach, as usual, you are beating around the bush. What about reading the question before writing your criticism? And what about presentig your original solution? The question of smokingguns was to get first two fields from two input files, to do some unspecified comparisons and to generate one output file with all four extracted fields. My script does this work, I have tested it. There was no mention of col. 4 and cut -d ' ' you are speaking about. Now I am awaiting the answer from smokingguns and you have the chance to publish your solution to the given question. Have a good day. zlutovsky ![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > separating fields of a data file using awk |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|