LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk multilines / grep colums.. or indeed anything that will work (https://www.linuxquestions.org/questions/programming-9/awk-multilines-grep-colums-or-indeed-anything-that-will-work-688108/)

baidym 12-03-2008 04:07 PM

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.

syg00 12-03-2008 05:26 PM

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

acerimusdux 12-04-2008 01:52 AM

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".

baidym 12-04-2008 07:26 PM

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.


All times are GMT -5. The time now is 09:38 PM.