combine -A and -v in grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers combine -A and -v in grep
# 1  
Old 04-07-2010
combine -A and -v in grep

I have a data file that looks like this:
infile:
Code:
A 
13
Z 
23
F
22
Z
413
R 
16

how do I combine the -A option with the -v option to get the lines that don't match 'Z' and their next lines?

the outfile should be:
Code:
A 
13
F
22
R 
16

I tried:
Code:
grep -A1 -v Z infile

the outfile looks the same as the infile
# 2  
Old 04-07-2010
Hi.

A nonstandard command, cgrep, often works where grep does not:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate cgrep.
# http://www.bell-labs.com/project/wwexptools/cgrep/
# http://sourceforge.net/projects/cgrep/

# Test run as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
# Infrastructure details, environment, commands for forum posts. 
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p specimen grep cgrep
set -o nounset
echo

FILE=${1-data1}

# Sample file, with head & tail as a last resort.
specimen $FILE \
|| { echo "(Using head/tail)" ; head -5 $FILE ; echo " --" ; tail -5 $FILE; }

echo
echo " Results, expect failure:"
grep -A1 -v Z $FILE

echo
echo " Results, cgrep:"
cgrep -D -V  +1 Z $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
specimen (local) 1.15
GNU grep 2.5.3
cgrep - (local: ~/executable/cgrep May 29 2009 )

Whole: 5:0:5 of 10 lines in file "data1"
A 
13
Z 
23
F
22
Z
413
R 
16

 Results, expect failure:
A 
13
Z 
23
F
22
Z
413
R 
16

 Results, cgrep:
A 
13
F
22
R 
16

You will need to download and compile the program. I did that a few years ago in 32-bit mode with success. Recently I used 64-bit mode, also successfully.

The documentation is longish, but includes examples. The options are complex.

I used the code from the Bell Labs site, but connections recently have been rejected. I know that the sourceforge version compiles, but I have not tested it.

Good luck ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can I combine below mentioned grep commands using OR (when searching strings having spaces)

Command 1: $script | grep 'Write to ECC( SSID=MARGIN)' Command 2: $script | grep 'is not greater than existing logical processing' The above commands run my script and search the mentioned strings but I do not want to run my script twice. It is increasing run time. Can someone tell me... (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Combine awk Or Grep Input

grep -ia '^Windows Installer' Galant01.txt Galant02.txt | awk -F '\t' '{print substr($1,1,index($1,":")) $4 "<br>"}' ; The output produces: Galant01.txt:Manual<br> Galant02.txt:Manual<br> It will be good to show instead: The output produces: Galant01.txt,Galant02.txt:Manual<br> The idea... (2 Replies)
Discussion started by: alvinoo
2 Replies

3. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

4. Shell Programming and Scripting

Combine two lists From Multiple Grep commands.

I'm working with a file with an xml structure. I'd like to parse it down to just the bits i want. Here is and example of the file <message id="96352877" method="status"> <date rfc="Sat, 12 Mar 2011 16:13:15 -0600" unix="1299967995" /> <services> <service id="facebook"... (4 Replies)
Discussion started by: Erulisseuiin
4 Replies

5. Shell Programming and Scripting

combine

Hi I am having text file like this 001|ramu|hno221|>< sheshadripuram|delhi|560061>< 002|krishna|hno225|>< newdelhimain|delhi|560061>< i want to combine every two lines as single...line... i.e 001|ramu|hno221|sheshadripuram|delhi|560061 can u pls help me (3 Replies)
Discussion started by: suryanarayana
3 Replies

6. Shell Programming and Scripting

combine two grep statements

Hi I am wondering is it possible to combine two greps together I have two greps. grep "^,, *\." file (grep the line which has a '.' in the third column) grep "=" file (grep the line which has = anywhere) How to put them together so that if the content of the file that match either... (1 Reply)
Discussion started by: tiger66
1 Replies

7. Shell Programming and Scripting

grep this no. 0100025144 and combine

<CRMSUB:MSIN=0100025144,BSNBC=TELEPHON-9814072360-TS11&TS21&TS22,NDC=9814 <ENTROPRSERV:MSIN=0100025144,OPRSERV=UCSI,UCSI=771-919814047117&775-919814047117; <ENTRGCSERV:MSIN=0100025144,GCSERV=CALLWAIT-PROV&CALLHOLD&CLIP&HOTBILL&NATSS05,CLIPOVR=NO;... (3 Replies)
Discussion started by: dodasajan
3 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

sed, grep, cut or combine?

I am a beginner at shell scripting, actually i am working on my first script right now. Anyway i have searched the world how to grep two letters from each word (it will always just be two words). For example: Example Blablabla I want my script to cut out Ex (from the first word) and Bl... (4 Replies)
Discussion started by: maskot
4 Replies

10. UNIX for Dummies Questions & Answers

sed or grep or perl combine two lines

how do i search for the phrase "expected" on line one and "received" on line two. (there is a newline in between the two) I would like to know if/how this can be done in perl and/or grep and/or sed (3 Replies)
Discussion started by: artjaniger
3 Replies
Login or Register to Ask a Question