Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

any way we can change the delimiter that uses grep?

I think, by default, grep uses \n as the delimiter.

share|improve this question

migrated from stackoverflow.com May 14 '10 at 1:30

This question came from our site for professional and enthusiast programmers.

    
This sounds like it would be better suited on SuperUser.com –  Mitch Dempsey May 13 '10 at 20:12
    
Read the man page, see if there's such an option (no, I don't believe there is). –  Jefromi May 13 '10 at 20:18
    
I already have read it. and no, there is no option –  jhon May 13 '10 at 20:21
    
@jhon: Then don't ask how to change grep, ask how to do what you want to do with something else. For example, you can specify the record separator in perl ($/) and awk (RS). Or you could do something like echo "$var" | sed 's/<delimiter>/\n/' | grep <pattern>. Many many answers, depending on what your actual problem is. –  Jefromi May 13 '10 at 20:38

4 Answers 4

It you care just about the output, tr might work for you:

che@nok ~ $ grep cpu /proc/cpuinfo | tr '\n' ';'
cpu family      : 6;cpu MHz             : 800.000;cpu cores     : 2;cpuid level : 10;cpu family : 6;cpu MHz : 800.000;cpu cores      : 2;cpuid level : 10;
share|improve this answer
    
actually, I have a buch of data like this, stored inside a variable: something xxxxx yyyyyy@ aaaaaaaaa bbbbb@ asdasd @ asdsda@ asds...@ so, I was guessing if it was possible: echo $my_var | grep "something" this works, but not as I want, grep takes the bunch of data as a single line, that's why I want to change it to a specific character (here is '@') –  jhon May 13 '10 at 20:29
    
Then you might try putting it like this: echo $my_var | tr @ '\n' | grep blahblah –  che May 13 '10 at 20:42
    
I have done it, but, seems that the shell is "trying to" execute the content :S –  jhon May 13 '10 at 20:45
2  
You have to quote the variable $my var to preserve white-spaces and newlines, i.e. echo "$my_var" | .... –  mrucci May 14 '10 at 4:02

agrep from the University of Arizona allows you to set the delimiter. It's not a drop-in replacement for grep; it uses a different algorithm (approximate grep, can match up to errors) and has different pattern syntax. But it's a handy tool.

Unfortunately, I think because of licensing issues, there have been multiple similar projects called agrep, and they're not all compatible. But I think all versions allow you to set a delimiter.

share|improve this answer
case $# in
0|1|2)  cat >&2 <<EOF
    Usage: $0 delimiterstring pattern files....
    Behaves like grep -pdelimiterstring but on linux
    DOES NOT SUPPORT MOST ARGUMENTS
    unlike grep -p - leaves delimiterstring in output

    NO -p just the delimeterstring
    note: delimiterstring has to work in the sed command where the ${d} is
    if you are going to use @ in the delimeter string, then the @'s
    need to be replaced by someother character

EOF
    exit 0
;;
3)  mode=one ;;
*)  mode=many ;;
esac
d="${1}"
p="${2}"
shift 2
while [ $# -gt 1 ]
do
    sed "s@${d}@\x00@g" "${1}" | grep -z  "${p}" -  | (
        case $mode in 
        many) sed -e "s@\x00@${d}@g"  -e "s@^@${1}: @" ;;
        *)    sed -e "s@\x00@${d}@g"  ;;
        esac
    )
    shift
done
share|improve this answer
1  
(1) While this may solve the problem, we prefer answers to have a bit more explanation. For example, is there any real reason to say d="${1}" instead of just d="$1"? (2) You need to expand your warning: If this is used with multiple files, you also have a problem if any of the filenames contains `@’. (3) (Nit pick) A true “grep” replacement/emulator would also work with zero file arguments. –  Scott Mar 26 at 21:03

How's this ? Any use ?

grep file "search-string" |awk 'BEGIN{RS="\n";ORS="@"}{print}'

Set '@' to whatever suits your need.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.