Hello,
W/in the script I'm working on, I have a need to take a column from a file, and format it so I can have a variable that will egrep for & invert the regex from another file.
My solution is this:
VAR=`awk -F, '{print $2}' $FAIL | sed 's/-i/\|/g'`
VAR2=`echo $VAR | sed 's/ //g;s/^.\{1\}//g'`
egrep "$VAR2" file.txt >> newfile.txt
egrep -v "$VAR2" file.txt >> newfile.txt
The above works, but it's just ugly sed code.
All together...
input.txt:
-h a, -i b, -j c
-h d, -i e, -j f
-h g, -i h, -j i
VAR=`awk -F, '{print $2}' input.txt | sed 's/-i/\|/g'`
... echo $VAR, prints: | b | e | h
So I'm running the variable again, through "another filter":
VAR2=`echo $VAR | sed 's/ //g;s/^.\{1\}//g'`
... echo $VAR2, prints b|e|h
... which is what I'd need for a good egrep command.
What I don't understand, why 'this' isn't working:
VAR=`awk -F, '{print $2}' input.txt | sed 's/-i/\|/g;s/^.\{1\}//g;s/ //g'`
???
It'll produce something like this:
b e h
... I'm losing the "|", and the spaces are still there?
Can someone please help me understand what I'm doing wrong w/ SED? I can get the results I want, but it's ugly. Is what I'm doing correct? Lastly... if there's nothing wrong w/ how I'm doing things... is there a better or more efficient way?
Thanks everybody.