#!/bin/bash #convert multi-line records to a standard awk one-lin awk record # multi2one [-r DRS] [-d DFS] infile # -r DRS .. multi-line record, ["^$"] for input file # -d DFS .. default field separator ["\t"] for output # use case: there are severa data lines ending with empty line or # blank line. we wish to gang up the multi-data lines with FS=DFS # DRS specifies the input file data record separator as a regular expression # but without the "/". So in this case, "^$" or "^ *$" would be the appropriate # DRS. DFS can be set to "\t", or "," etc. #----------------------------------------------------------------------- DRS="^$"; DFS="\t"; HELP=; #----------------------------------------------------------------------- while getopts m:d:h optval do case $optval in r) DRS=$OPTARG;; d) DFS=$OPTARG;; h) HELP=1;; *) echo "multi2one -h for help"; exit -1 esac done shift $((OPTIND-1)) if [ $HELP ]; then echo "multi2one [-d DRS] [-d DFS] {-h} file (or pipe) echo '-d record separator given as regex string with the slashes, ["^$"]' echo '-d field sepataor ["\t"]' echo 'reads input file and writes out record with DRS and FS=DFS" echo "used to combine multi-line records" exit fi #----------------------------------------------------------------------- # check to see if data is being supplied by pipe #----------------------------------------------------------------------- case $# in 0) if [ -p /dev/stdin ]; then set -- "/dev/stdin" #set $1=/dev/stdin else echo "no file given nor is there a trailing pipe" exit -1; fi;; 1) ;; *) echo "can accept only one file"; exit -1;; esac IF=$1 gsed -n '1{H;b;};${H;x;s/\n/'"$DFS"'/gp};/'"$DRS"'/!{H;b};/'"$DRS"'/{x;s/\n/'"$DFS"'/gp}' $IF | gsed 's/^'"$DFS"'//'