Picking matching strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Picking matching strings
# 15  
Old 04-28-2011
Quote:
Originally Posted by kristinu
When I remove the RS=" ", all the list is displayed. I thought it will display only the ones that match!!!
Then don't remove it!

Why would you to need remove it?

It takes
Code:
-a b c=d

and turns it into
Code:
-a
b
c=d

Then you can easily check (/^-/ || /=/) for what you need.

The alternative is not to use RS at all, and instead put a for-loop in your code to look at each field individually. The point, when RS saves you this trouble?
# 16  
Old 04-28-2011
My question is that if I do

Code:
echo "file1 file2 file3 -file4 file5= sdf-sdf =file filr=232 file-6" | awk '/^-/ || /=/'

I still get

Code:
file1 file2 file3 -file4 file5= sdf-sdf =file filr=232 file-6

It outputs everything.
# 17  
Old 04-28-2011
Quote:
Originally Posted by kristinu
My question is that if I do

Code:
echo "file1 file2 file3 -file4 file5= sdf-sdf =file filr=232 file-6" | awk '/^-/ || /=/'

I still get

Code:
file1 file2 file3 -file4 file5= sdf-sdf =file filr=232 file-6

It outputs everything.
I'm not sure if you're following what scottn's been explaining.
Why are still insisting on NOT using RS=' ' ?
# 18  
Old 04-28-2011
I will, but am wondering about this and try to understand why it returns everything without it. And how the RS=" " solves the problem.

---------- Post updated at 04:48 PM ---------- Previous update was at 04:46 PM ----------

I agree with you. I originally had a loop and wanted something more straightforward.

Quote:
Originally Posted by scottn
Then don't remove it!

Why would you to need remove it?

It takes
Code:
-a b c=d

and turns it into
Code:
-a
b
c=d

Then you can easily check (/^-/ || /=/) for what you need.

The alternative is not to use RS at all, and instead put a for-loop in your code to look at each field individually. The point, when RS saves you this trouble?
# 19  
Old 04-28-2011
Quote:
Originally Posted by kristinu
I originally had a loop and wanted something more straightforward.
You had it about ninety minutes ago Smilie

But you're complicating it again.

The use, and function, of RS is explained in the awk man page.
# 20  
Old 04-28-2011
Quote:
Originally Posted by kristinu
I will, but am wondering about this and try to understand why it returns everything without it. And how the RS=" " solves the problem.
I'm not sure I can explain it any better than what scottn's already tried.
Setting the RS=' ' sets a list of a space separated entries INTO a multiline record (conceptually speaking). Having a multiline record easily positions itself for using the /^-/ || /=/ pragma.

Not sure if I'm confusing you more than you deserve Smilie
# 21  
Old 04-28-2011
Ok, let's try to explain myself better. If I write it this way, I understand everything.

Code:
echo "file1 file2 file3 -file4 file5= sdf-sdf =file filr=232 file-6" | awk 'BEGIN {RS=" ";ORS=" "} /^-/ || /=/'

What I'm missing is why one sets RS=" " after the pattern matching and everything is ok.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

(g)awk: Matching strings from one file in another file between two strings

Hello all, I can get close to what I am looking for but cannot seem to hit it exactly and was wondering if I could get your help. I have the following sample from textfile with many thousands of lines: File 1 PS001,001 HLK PS002,004 L<G PS004,002 XNN PS004,006 BVX PS004,006 ZBX=... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

Matching only the strings I provide - sed

Hello.. I am currently learning sed and have found myself in some trouble.. I wrote this command: sed -ne 's/*\(\{2\}*\{2\}*\{2\}*\).*\(\{2\}*\{2\}*\{2\}*\).*/\1\2/p' and some of the output i get is : ->stockholm->paris<-stockholmpi<-tokyo->paris<-stockholmpi... (8 Replies)
Discussion started by: jozo95
8 Replies

3. Shell Programming and Scripting

Need to append matching strings in a file

Hi , I am writing a shell script to check pvsizes in linux box. # for i in `cat vgs1` > do > echo "########### $i ###########" > pvs|grep -i $i|awk '{print $2,$1,$5}'>pvs_$i > pvs|grep -i $i|awk '{print $1}'|while read a > do > fdisk -l $a|head -2|tail -1|awk '{print $2,$3}'>pvs_$i1 >... (3 Replies)
Discussion started by: nanduri
3 Replies

4. Shell Programming and Scripting

matching strings from different files

I want to compare file 1 to file 2 and if a string from file 1 appears in file 2, then print the file 2 row, where the string appears, onto file3. file 1 looks like this. DOG_0004340 blah blah2 j 22424 DOG_3010311 blah blah3 o 24500 DOG_9949221 blah blah6 x 35035 file 2 looks like... (5 Replies)
Discussion started by: verse123
5 Replies

5. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

6. Programming

help with matching strings

In C programming how do i check if a char is equal to a vowel , like a e i o or u, small or big case. in my function i have the parameter like *word, and i am using word in a for loop, to check if its equal. i use tolower(word)=='a' || ..... but for some reason it only matches on lower case and... (1 Reply)
Discussion started by: omega666
1 Replies

7. Shell Programming and Scripting

Strings not matching

Hi, I have got two variables holding strings, if i echo them, they print the same value but if i compare the condition fails?? can somebody suggest something?? I have checked the word count too, they are also same. Thanks, Atul (4 Replies)
Discussion started by: atulmt
4 Replies

8. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

9. Shell Programming and Scripting

Remove matching lines with list of strings

Hi, HP-UX gxxxxxxxc B.11.23 U ia64 3717505098 unlimited-user license I have a file with below pipe separated field values: xxx|xxx|abcd|xxx|xxx|xx xxx|xxx|abcd#123|xxx|xxx|xx xxx|xxx|abcd#345|xxx|xxx|xx xxx|xxx|pqrs|xxx|xxx|xx xxx|xxx|pqrs#123|xxx|xxx|xx The third field has values like... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies

10. UNIX for Dummies Questions & Answers

matching characters between strings

please send the logic or program to find the matching characters between two strings for ex string1 :abc string2 :adc no .of matching characters is 2(a,c) (9 Replies)
Discussion started by: akmtcs
9 Replies
Login or Register to Ask a Question