Keep whenever - occurs right after a +


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keep whenever - occurs right after a +
# 1  
Old 04-19-2010
Keep whenever - occurs right after a +

Hi,
I have a file that I want to manipulate, so basically I want to keep the rows that have a + and then - right after. So here is what I mean:

my file (tab delim.)

Code:
bee  +
see  +
jee   -
bep  +
iott  +
mun -
moo -

So I only want the rows that have + then a - right after. The output would look like this .

Code:
see  +
jee   -
iott  +
mun -

thanks
# 2  
Old 04-19-2010
Try this.

Code:
$2 ~ /+/{
        prev=$0;
        getline;
        if( $2 == "-"){ printf("%s\n%s\n",prev,$0)}
        else { prev =$0;
               getline;
               if( $2 == "-"){ printf("%s\n%s\n",prev,$0)}
              }
        }

put the above in a awkscript file and execute as

Code:
awk -f awkscrfile  filename

cheers,
Devaraj Takhellambam
# 3  
Old 04-19-2010
strange that did not work, I cant see where things went wrong. I got this error message.

awk: illegal primary in regular expression + at
source line number 1 source file 3.txt
context is
$7 ~ >>> /+/ <<<
# 4  
Old 04-19-2010
use a backslash for '+'
Code:
$2 ~ /\+/{

Another solution in perl

Code:
cat filename | perl -e 'while (<>){
chomp;
($word,$sign) = split("\ ");
if ($prev_sign && $prev_sign ne $sign){
print "$prev_word\t$prev_sign\n$word\t$sign\n";
($prev_sign,$prev_word) = ("","");
}
else {
($prev_sign,$prev_word) = ($sign,$word);
}
}
'
see     +
jee     -
iott    +
mun     -


HTH,
PL
# 5  
Old 04-19-2010
Code:
$ sed -n '/-$/{x; /+$/{p;x;p;};};h' data
see  +
jee   -
iott  +
mun -


Last edited by alister; 04-19-2010 at 10:18 PM..
# 6  
Old 04-20-2010
Code:
while(<DATA>){
	if(!/-/){
		$tmp=$_;
	}
	else{
		if($tmp ne ""){
			print $tmp;
			print;
			$tmp="";
		}
	}
}
__DATA__
bee + 
see + 
jee - 
bep + 
iott + 
mun - 
moo -

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Egrep find word that occurs twice in a row

Hi there I am trying to figure out and understand what the syntax would be to egrep lines that have a word occur twice in a row. the two words obviously should have a space between them and also it has to be case sensitive which I believe grep is by deffault. the closest I have come is... grep... (7 Replies)
Discussion started by: spo_2138
7 Replies

2. Shell Programming and Scripting

awk to print array that occurs the most with matching value in another field

In the below awk I am splitting $7 on the : and then counting each line or NM_xxxx. If the $1 value is the same for each line then print the $7 that occurs the most with the matching $1 value. The awk seems close but I am not sure what is going on. I included a description as well as to what I... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN.1.2.1.2.14.1.1" := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS.1.2.1.2.13.6.2" := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

4. Shell Programming and Scripting

Shell script to execute condition until an event occurs

I need a script to keep polling "receive_dir" directory till "stopfile" get written in the directory. This has to run despite empty directory. So far i have this but fails if receive_dir is empty with no files with "unary operator expected". Help !! #!/usr/bin/ksh until do for i... (1 Reply)
Discussion started by: iaav
1 Replies

5. UNIX for Dummies Questions & Answers

how to read how many times same result occurs?

Is there a way to read how many times each result occurs in a list, say the list.txt has the results: a c d a f c a d e and I need to know how many times each of them occurs such as : a=3 c=2 ..etc. (5 Replies)
Discussion started by: Iifa
5 Replies

6. Shell Programming and Scripting

Deleting a type of file in a directory immediately it occurs

Hi All, I have to maintain few servers and in those servers in a specific directory some special jobs keeps on running ....... The directory is having many kind of files like.....acd*, hud*, rca*....bla bla bla....... My aim is that if due to any job-any time a file having initials like... (4 Replies)
Discussion started by: jitendra.pat04
4 Replies

7. UNIX for Advanced & Expert Users

stty: error occurs when installing rpm

Hello All, when I install rpm rpm --install rpm packagename I got the following errors stty:standard input:invalid argument stty:standard input:invalid argument I dont know what i have to do exactly. I search on google for the same but not a particular standard solution is given... (17 Replies)
Discussion started by: amitpansuria
17 Replies

8. Shell Programming and Scripting

Exiting from script when error occurs

Hi Friends, Is it possible to exit nicely(ie, to echo a message with the error occurred) from a shell script(quiet a big one :)) once it encounter an error in between the lines? For example, in my script I am calling the command mkdir and sometimes (when the directory already exists) it... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

9. Programming

Cannot catch SIGINT while serial break condition occurs

I setup termios structure with IGNBRK is not set and BRKINT is set. To allow the process to receive signals I call: fcntl(fd, F_SETOWN, getpid()); I have made a signal handler to catch all signals. I can catch SIGINT when pressing ctrl+c but when I send break signal over serial then it cannot... (13 Replies)
Discussion started by: gzz
13 Replies

10. HP-UX

error occurs while some users login

hi all, i have a problem that while some of the users trying to login the following error occurs and the session is automatically closed. ssl error: RAND_status reported there wasn't enough randomness for the PRNG. ssl error: You need to specify RandomFile or EGDFile to obtain the randomness.... (0 Replies)
Discussion started by: rrlog
0 Replies
Login or Register to Ask a Question