UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2005, 12:49 PM
smokingguns smokingguns is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 16 smokingguns User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 4 sec
Reputation Power: 0
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..

Reply With Quote
  #2  
Old June 27th, 2005, 02:38 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 17 m 10 sec
Reputation Power: 48
Code:
cut -d ' ' -f 1,2 myfile1 > f1
cut -d ' ' -f 1,2 myfile2 > f2
paste f1 f2 > file_wth_4_columns

Reply With Quote
  #3  
Old July 13th, 2005, 04:30 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 7 h 31 m 24 sec
Reputation Power: 9
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.

Reply With Quote
  #4  
Old July 15th, 2005, 07:34 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
jim
no, niet, nein, non
ls -l >file
demonstrate me how do you get the 4. col using cut -d' ' file
????




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

Reply With Quote
  #5  
Old July 15th, 2005, 03:48 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 7 h 31 m 24 sec
Reputation Power: 9
Quote:
Originally Posted by zlutovsky
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

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 ????

Reply With Quote
  #6  
Old July 18th, 2005, 06:28 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
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 ????



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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > separating fields of a data file using awk


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway