LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2008, 04:07 PM   #1
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Rep: Reputation: 0
awk multilines / grep colums.. or indeed anything that will work


Hi all,

I am trying to pull a series of rows out of a list.txt e.g

file# point# flag pc
123 1001 0 pc2
124 1000 0 pc1
125 999 8 pc1
126 998 0 pc1
127 997 0 pc3
128 996 0 pc2
129 995 2 pc2
130 994 0 pc1


I want to pull out the line with flags 2 4 8 12 but also the lines above and below. i.e

124 1000 0 pc1
125 999 8 pc1
126 998 0 pc1
128 996 0 pc2
129 995 2 pc2
130 994 0 pc1

grep and awk do both of this to an extent

% grep -C1 '[2 4 8 12]' list.txt
this will give me everything as 2 and 4 feature in the other lines
- can I grep column 3?

% awk '{if($6=="2"||$6=="4"||$6=="8"||$6=="12") print $0}' list.txt
but this will give me only single lines - can I use getline?

... anything else that would do the job would be appreciated
can you help?

M.x.
 
Old 12-03-2008, 05:26 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,214

Rep: Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953Reputation: 2953
Try "grep -E -C1 '[[:space:]]([248]|12)[[:space:]]' list.txt"

Last edited by syg00; 12-03-2008 at 05:32 PM. Reason: D'oh; test was bad
 
Old 12-04-2008, 01:52 AM   #3
acerimusdux
LQ Newbie
 
Registered: Aug 2004
Posts: 12

Rep: Reputation: 0
There's also a command called column that will do that. Try "man column" for details. Might depend on the distro, but I think it's pretty standard in Debian, from the bsdmainutils package.

Another interesting package is called nosql; it contains a whole bunch of utilities for managing data from the command line. Supposedly enough to be able to do complete relational database management just using command line utilities and ASCII test files. Hence, "no" "sql".
 
Old 12-04-2008, 07:26 PM   #4
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Original Poster
Rep: Reputation: 0
Halfway there...

Thanks, that grep works nicely, but here's another one for you...

I now have a list...

file# point# flag pc
124 1000 0 pc1
125 999 8 pc1
126 998 0 pc1
128 996 0 pc2
129 995 2 pc2
130 994 0 pc1
etc

I now want to relabel the flags for 0 to be 1 and all others to be 2. i.e.
if ($3==0) then
$3=="1"
else
$3=="2"
endif

...obviously this won't work... can I use arrays with a shell script?

M.x.
 
  


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
Can I use grep inside awk? Helene Programming 10 09-29-2015 09:48 PM
Using grep and awk to get IP address snowman81 Programming 12 08-25-2012 11:45 AM
cat|grep|awk pudhiyavan Programming 6 07-14-2008 02:56 AM
Help with Bash (grep/awk/etc) piercey Programming 12 02-27-2008 11:21 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 05:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

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