tags:

views:

106

answers:

2

more file

param1=" 1,deerfntjefnerjfntrjgntrjnvgrvgrtbvggfrjbntr*rfr4fv*frfftrjgtrignmtignmtyightygjn      2,3,4,5,6,7,8, 
rfcmckmfdkckemdio8u548384omxc,mor0ckofcmineucfhcbdjcnedjcnywedpeodl40fcrcmkedmrikmckffmcrffmrfrifmtrifmrifvysdfn drfr4fdr4fmedmifmitfmifrtfrfrfrfnurfnurnfrunfrufnrufnrufnrufnruf"****

need to match the content of param1 as

sed -n "/$param1/p" file 

but because the line length (very long line) I cant match the line what’s the best way to match very long lines?

+1  A: 

The problem you are facing is that param1 contains special characters which are being interpreted by sed. The asterisk ('*') is used to mean 'zero or more occurrences of the previous character', so when this character is interpreted by sed there is nothing left to match the literal asterisk you are looking for.

The following is a working bash script that should help:

#!/bin/bash

param1=' 1,deerfntjefnerjfntrjgntrjnvgrvgrtbvggfrjbntr\*rfr4fv\*frfftrjgtrignmtignmtyightygjn 2,3,4,5,6,7,8, rfcmckmfdkckemdio8u548384omxc,mor0ckofcmineucfhcbdjcnedjcnywedpeodl40fcrcmkedmrikmckffmcrffmrfrifmtrifmrifvysdfn'

cat <<EOF | sed "s/${param1}/Bubba/g"
 1,deerfntjefnerjfntrjgntrjnvgrvgrtbvggfrjbntr*rfr4fv*frfftrjgtrignmtignmtyightygjn 2,3,4,5,6,7,8, rfcmckmfdkckemdio8u548384omxc,mor0ckofcmineucfhcbdjcnedjcnywedpeodl40fcrcmkedmrikmckffmcrffmrfrifmtrifmrifvysdfn    
EOF
Derek Greer
THX but I not need to replace the line only to match like grepthe target is to verify matchingyael
yael
This is just an example to illustrate the problem you were having. The intent was for you to focus on the issue (you have special characters in your matching expression), not the way I'm using it. Obviously you wouldn't be replacing your match with 'Bubba'.
Derek Greer
@yael: Why don't you just use `grep`?
Dennis Williamson
+1  A: 

Maybe the problem is that your $param1 contains special characters? This works for me:

A="$(perl -e 'print "a" x 10000')"
echo $A | sed -n "/$A/p"

($A contains 10 000 a characters).

echo $A | grep -F $A

and

echo $A | grep -P $A

also works (second requires grep with built-in PCRE support. If you want pattern matching you should use either this or pcregrep. If you don't, use the fixed grep (grep -F)).

echo $A | grep $A

is too slow.

ZyX
can I get more usful solution from perl?
yael
What you mean by «useful»?
ZyX