grep changing the delimiter

any way we can change the delimiter that uses grep?

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

4

5 Answers

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

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

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;
4

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.

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
1

How's this ? Any use ?

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

Set '@' to whatever suits your need.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like