LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 11: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, 11:51 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,926

Rep: Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690Reputation: 3690
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, 12:28 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 12:31 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
A nasty little pipeline, rather than elegant awk.
Code:
cat knife.dat | colrm 2 | sed 's/^/\^/' | grep -f - snake.dat
 
Old 04-16-2014, 03: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, 03:27 PM   #6
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
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, 01:01 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 11: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 11:23 AM. Reason: added a thank you
 
Old 04-17-2014, 11: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, 11:39 AM   #10
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
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, 11:57 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 01: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, 02:00 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 02:55 PM   #14
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
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, 09: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:11 AM
Snake game in c code which run in gcc lafanga Linux - Newbie 4 09-06-2008 01:17 PM
BestBuy EasySnap Snake Webcam CarlosCardoso Linux - Networking 2 02-07-2006 09:50 AM
java snake ? jamaso Linux - Software 0 10-17-2004 07:45 PM
Anyone else has a snake? NSKL General 8 05-01-2003 10:47 AM


All times are GMT -5. The time now is 01:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration