LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-05-2008, 03:09 PM   #1
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

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


Hi everybody,

I simply can not get this to work can anyone help? I't does'nt matter how it's done if it works

I am trying to pull out rows with flags of 8 and 12 (in column 6) but also the lines above and below

I've tried %grep -E -C1 '[[:space:]]([8]|12)[[:space:]]' <file>

BUT,
I also need to relabel the flags so 0=1 and 8 or 12 = 3

file# point# usr pc label flag
1 1 174 1 1312001 0
2 1 175 1 1312001 0
3 1 176 1 1312001 8
4 1 177 1 1312001 0
5 1 178 1 1312001 0
6 1 179 1 1312001 0
7 1 180 1 1312001 0
8 1 181 1 1312001 0
9 1 182 1 1312001 12
10 1 183 1 1312001 0
11 1 184 1 1312001 0

so I would get
2 1 175 1 1312001 1
3 1 176 1 1312001 3
4 1 177 1 1312001 1
8 1 181 1 1312001 1
9 1 182 1 1312001 3
10 1 183 1 1312001 1
11 1 184 1 1312001 1

Can someone PLEASE help me, I'm useless with this.

Thanks M.x.
 
Old 12-05-2008, 04:34 PM   #2
thecarpy
Member
 
Registered: Apr 2005
Location: France
Distribution: Devuan, Suse, Slackware
Posts: 130

Rep: Reputation: 21
This has to be done in awk, and you need to do something like:

{
if ($6 ~ /12/)
{
$6 = 3;
lastWasTrue = "true";
print previous;
print $0;
}
else if ($6 ~ /8/)
{
$6 = 3;
lastWasTrue = "true";
print previous;
print $0;
}
else if (lastWasTrue == "true")
{
if ($6 ~ /0/)
{
$6=1;
}
print $0;
lastWasTrue = "false";
}
else
{

if ($6 ~ /0/)
{
$6=1;
}
previous=$0;
}
}

wap the code into a file and run awk as:

awk -F\ -f myscript.awk <file>

I have not tested this and I may have forgotten stuff/gotten stuff wrong ... but it should sort of work. Please test, code provided for your convenience, no warranties.

I write in many languages, from java to c, long time no awk ...

Last edited by thecarpy; 12-05-2008 at 04:39 PM.
 
Old 12-06-2008, 07:24 AM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Assuming line 11 in the example output is a typo and your sed supports the -r option:

Code:
sed -rn '
/ (8|(1(|2)))$/!{
  s/ 0$/ 1/;x;d;
  }
/ (8|(1(|2)))$/ {
  s/ [^ ]*$/ 3/ 
  x;p;x;p
  n;s/ 0$/ 1/;p;x;
}' infile
Or,
a pipeline with GNU grep and AWK:

Code:
grep -EC1 ' (8|(1(|2)))$' infile |
  awk '!/^--/&&$NF=/ 0$/?1:3'
If in the context lines $NF is not always 0:

Code:
grep -EC1 ' (8|(1(|2)))$' infile |
  awk '/ (8|(1(|2))|0)$/{$NF=/ 0$/?1:3}!/^--/'

Last edited by radoulov; 12-06-2008 at 08:34 AM.
 
Old 12-08-2008, 11:54 AM   #4
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Original Poster
Rep: Reputation: 0
Works, but has a bug

That sed works a treat, but I've discovered a bug that hinders me a bit

When there are two consecutive rows of 8s

IN:
233 1 405 1 1312001 0
234 1 406 1 1312001 8
235 1 407 1 1312001 8
236 1 408 1 1312001 0

OUT:
233 1 405 1 1312001 1
234 1 406 1 1312001 3
235 1 407 1 1312001 8
238 1 410 1 1312001 1

using:

#! /bin/bash -f

sed -rn '
/ (8|(1(|2)))$/!{
s/ 0$/ 1/;x;d;
}
/ (8|(1(|2)))$/{
s/ [^ ]*$/ 3/
x;p;x;p
n;s/ 0$/ 1/;p;x;
}' infile.txt > outfile.txt


Anyone got any ideas to get around this?

Or, If I grep for 8 in column 6 how can I insert a newline so that these lines are easy to spot?
 
Old 12-08-2008, 12:18 PM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Something like this:

Code:
sed -rn '
/ (8|(1(|2)))$/!{
s/ 0$/ 1/;x;d;
}
/ (8|(1(|2)))$/{
s/ [^ ]*$/ 3/
x;p;x;p
n
s/ 0$/ 1/
s/ (8|(1(|2)))$/ 3/
p;x;
}' infile
And what should be the expected output, given the above example input?

Last edited by radoulov; 12-08-2008 at 12:22 PM.
 
Old 12-08-2008, 03:45 PM   #6
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Original Poster
Rep: Reputation: 0
Clients!!!!!

you're a sed guru!!

I'm sorry, but they keep on changing the specs, I'll explain..this is to be a contents file to be read in, the 0's (replaced with 1's) in column 6 are good files, the 8's (replaced with 3's) are bad files.

Now the client is saying they want the start and end of the good files defined as 2 (start), 3 (end) and 1 (bad file). SO if...

IN:
233 1 405 1 1312001 0 #first good file
234 1 406 1 1312001 0
235 1 407 1 1312001 0 #last good file
236 1 408 1 1312001 8 #bad
237 1 409 1 1312001 0
238 1 410 1 1312001 0
239 1 411 1 1312001 8

OUT:
2 233 1 405 1 1312001 1 #start of good records
3 335 1 407 1 1312001 1 #end of good records
1 236 1 408 1 1312001 3 #single file (bad)
2 237 1 409 1 1312001 1 #start of good records
3 238 1 410 1 1312001 1 #end of good records
1 239 1 411 1 1312001 3 #single file (bad)
etc...

Buggers!!

This is geetting quite silly!!

M.x
 
Old 12-09-2008, 04:09 AM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Assuming only 0/8 at the end of the line.
Use nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk '/ 8$/ {
  sub(/ [^ ]*$/, " 3")
  print (!b++ && s) ? s RS $0 : $0
  s = g = 0
  }
/ 0$/ { 
  sub(/ 0$/, " 1")
  if (!g++) print
  s = $0; b = 0
  }' infile
 
  


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
Can I use grep inside awk? Helene Programming 10 09-29-2015 08:48 PM
awk multilines / grep colums.. or indeed anything that will work baidym Programming 3 12-04-2008 06:26 PM
cat|grep|awk pudhiyavan Programming 6 07-14-2008 01:56 AM
Help with Bash (grep/awk/etc) piercey Programming 12 02-27-2008 10:21 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM

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

All times are GMT -5. The time now is 02:54 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