LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2014, 10:16 AM   #1
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Rep: Reputation: 1
awk for cutting off the snake's head :)


hi guys,

i need a little awk data manipulation help...

i have two files as input files (snake.dat and knife.dat)and i need an awk script to output the head.dat file

Code:
snake.dat
1   1.71
2   8.02
3   1.03
4   2.04
5   3.15
6   4.06
7   5.07
Code:
knife.dat
5   1.54
6   8.06
7   1.23
and the desired output file would be

Code:
head.dat
5   3.15
6   4.06
7   5.07
so my "thinking algorithm" is to start with the knife.dat file, read the first row first column value (in this example 5). then use that value of 5 as a "look-up key" in the first column of the snake.dat file, and then use the look-up key (in this example 5) as a knife to cut the head off the snake.dat file and output just the head of the snake, the file head.dat

thanks for whatever help!

tabby
 
Old 04-16-2014, 10:51 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by tabbygirl1990 View Post
hi guys,
i need a little awk data manipulation help... i have two files as input files (snake.dat and knife.dat)and i need an awk script to output the head.dat file
Code:
snake.dat
1   1.71
2   8.02
3   1.03
4   2.04
5   3.15
6   4.06
7   5.07
Code:
knife.dat
5   1.54
6   8.06
7   1.23
and the desired output file would be
Code:
head.dat
5   3.15
6   4.06
7   5.07
so my "thinking algorithm" is to start with the knife.dat file, read the first row first column value (in this example 5). then use that value of 5 as a "look-up key" in the first column of the snake.dat file, and then use the look-up key (in this example 5) as a knife to cut the head off the snake.dat file and output just the head of the snake, the file head.dat
That's certainly one way to do it. Since you need help with your script, why don't you post what you've written/tried so far, and tell us where you're stuck. We're not going to write your scripts for you, though, but will be happy to assist.
 
Old 04-16-2014, 11:28 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Agreed ... your solution "sounds" like it has merit but you will need to post solution and where you are stuck if you want us to help
 
Old 04-16-2014, 11:31 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
A nasty little pipeline, rather than elegant awk.
Code:
cat knife.dat | colrm 2 | sed 's/^/\^/' | grep -f - snake.dat
 
Old 04-16-2014, 02:07 PM   #5
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
Smile

oh sorry guys, i had to run off i didn't mean for you all to write it

here's what i've been stabbing at...

BUT!!! in what i've written below, i did one major thing wrong. the knifevalue var should be "looked-up" as the first value in the first column of knife.dat, not user specified (set to 5) as i did below (please don't laugh i don't know how to do that, look up and pass over in a var field.

Code:
awk -knifevalue=5 -F " " 'FNR==NR{if($1==knifevalue){knifevalue==$1;nextfile};next}$1==knifevalue{print $0}' snake.dat knife.dat > head.dat
ok so here's my thinking and 1st attempt to write something

tell awk to look in the 1st column of the first file (snake.dat) for the knifevalue that i told it, on the same line as knifevalue is found in the "nextfile" the second file (knife.dat), when it occurs (it's equal) then print that entire line (there's actually more columns of data than I showed in the example files) to the head.dat file
 
Old 04-16-2014, 02:27 PM   #6
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:
Code:
bash-4.2$ awk 'FNR==NR{ if (NR == 1) knife=$1; next }{ if ($1 >= knife) print }' knife snake
5   3.15
6   4.06
7   5.07
 
Old 04-17-2014, 12:01 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If i may, is it just a coincidence of data presented that the numbers in the first column are from both files? ie do you want all that match from knife and found in snake to be shown
or only the first number that matches? Also, what happens, assuming current example, if '5' is not in snake but '6' is?

Neither of the current solutions allow for this second scenario.
 
Old 04-17-2014, 10:12 AM   #8
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
good morning grail, and the other guys.

yea i just made up the example data, the real data is on a closed lan, i'd love to post it but i can't last night i played with the example data some more at home (not when i do my best thinking) and ran into the same kinds of problems.

so a different set of example knife files...

Code:
snake.dat
1   1.71
2   8.02
3   1.03
4   2.04
5   3.15
6   4.06
7   5.07
and

Code:
knife.dat
5.1   2.51
6.3   8.66
7.7   1.29
then the output would be

Code:
head.dat
6   4.06
7   5.07
because 5.1 in knife.dat has already passed 5 in snake.dat so the next data point 6 would be used

and if

Code:
knife.dat
4.9   2.51
6.3   8.66
7.7   1.29
then the output would be as before

Code:
head.dat
5   3.15
6   4.06
7   5.07
because 4.9 in knife.dat has not yet passed 5 in snake.dat so a less than or equal to kind of thinking

here's kind of a picture of what the input and output should look like
Code:
snake.dat    ===========================================
knife.dat                                   ||||||||||||
head.dat                                    ============
thanks sooo much guys, tabby

Last edited by tabbygirl1990; 04-17-2014 at 10:23 AM. Reason: added a thank you
 
Old 04-17-2014, 10:37 AM   #9
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
actually, the more i play around with the example data and metaschima's command, the more i think it works, maybe your knowledge sees something i don't???
 
Old 04-17-2014, 10:39 AM   #10
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
My code seems to work with that.
Code:
bash-4.2$ awk 'FNR==NR{ if (NR == 1) knife=$1; next }{ if ($1 >= knife) print }' knife snake
6   4.06
7   5.07
bash-4.2$ awk 'FNR==NR{ if (NR == 1) knife=$1; next }{ if ($1 >= knife) print }' knife2 snake
5   3.15
6   4.06
7   5.07
 
1 members found this post helpful.
Old 04-17-2014, 10:57 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is a cut down version to do the same:
Code:
awk 'NR==1{x=$1;nextfile}$1 >= x' knife.dat snake.dat
 
1 members found this post helpful.
Old 04-17-2014, 12:58 PM   #12
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
thanks metaschima! it makes sense except i'm doomed to messing up FNR and NR

grail, i somewhat had something similar to yours, where you had the variable "x" i had "knifevalue" i just didn't know how to pass in something other than a user defined value. how does your do that? it starts in knife and graps the first rows first column value, then switches to the 2nd file and looks for the greater than or equal to value
 
Old 04-17-2014, 01:00 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
it starts in knife and graps the first rows first column value, then switches to the 2nd file and looks for the greater than or equal to value
Yep
 
1 members found this post helpful.
Old 04-17-2014, 01:55 PM   #14
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I prefer my version as 'nextfile' is not as portable, and the print is implicit. Explicit is always better than implicit, IMO.
 
1 members found this post helpful.
Old 04-17-2014, 08:32 PM   #15
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
yep boys, i like both answers, muah

tabby
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
For snake lovers everywhere, I really appreciated the info on this site. Dogs General 2 09-29-2009 05:11 AM
Snake game in c code which run in gcc lafanga Linux - Newbie 4 09-06-2008 12:17 PM
BestBuy EasySnap Snake Webcam CarlosCardoso Linux - Networking 2 02-07-2006 08:50 AM
java snake ? jamaso Linux - Software 0 10-17-2004 06:45 PM
Anyone else has a snake? NSKL General 8 05-01-2003 09:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration