grep changing the delimiter

12

2

any way we can change the delimiter that uses grep?

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

jhon

Posted 2010-05-13T20:11:11.957

Reputation:

This sounds like it would be better suited on SuperUser.com – Mitch Dempsey – 2010-05-13T20:12:28.170

Read the man page, see if there's such an option (no, I don't believe there is). – Cascabel – 2010-05-13T20:18:52.680

I already have read it. and no, there is no option – None – 2010-05-13T20:21:23.670

@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. – Cascabel – 2010-05-13T20:38:04.537

Answers

2

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;

che

Posted 2010-05-13T20:11:11.957

Reputation: 266

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 '@') – None – 2010-05-13T20:29:14.650

Then you might try putting it like this: echo $my_var | tr @ '\n' | grep blahblah – che – 2010-05-13T20:42:06.967

I have done it, but, seems that the shell is "trying to" execute the content :S – None – 2010-05-13T20:45:01.983

2You have to quote the variable $my var to preserve white-spaces and newlines, i.e. echo "$my_var" | .... – mrucci – 2010-05-14T04:02:30.673

1

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.

Norman Ramsey

Posted 2010-05-13T20:11:11.957

Reputation: 2 613

1

Replace the zero-delimiter ('@') with a newline (\n) before grep:

cat file | tr '\00' '\n' | grep "search-string"

Franz Meyer

Posted 2010-05-13T20:11:11.957

Reputation: 11

1

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

Kurt

Posted 2010-05-13T20:11:11.957

Reputation: 11

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 – 2014-03-26T21:03:52.150

0

How's this ? Any use ?

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

Set '@' to whatever suits your need.

user42723

Posted 2010-05-13T20:11:11.957

Reputation: 161