Search code examples
bashshellgrepshcut

How Do I Convert A Cut Command In Bash Into Grep With Given Code?


I've written a template engine script that uses cut to extract certain elements from a file, but I want to use grep in place of the cut. Here is the code I have written:

#!/bin/bash
IFS=@  #makes @ a delimiter. 
while read line
do
  dataspace=`echo ${line} | cut -d'=' -f1` 
  value=`echo ${line} | cut -d"=" -f2` 
  printf -v $dataspace "$value" #make the value stored in value into the name of a dataspace.
done < 'template.vars' #read template.vars for standard input.

skipflag=false           #initialize the skipflag to false                       
while read line  #while it is reading standard input one line at a time

Just came to the conclusion that the code blocks system here does not support bash. Anyway, since stackoverflow isn't letting me put Bash into codeblocks, I am not putting the entire script since it would look nasty. Based on what is currently high-lighted, how would I go about changing the part using the cut command into a line using the grep command?


Solution

  • As has been noted, you should give more information for a better answer. Going with what you have, I would say that awk is a better option than grep

    dataspace=$(awk '$0=$1' FS== <<< "$line")
        value=$(awk '$0=$2' FS== <<< "$line")