0
cat DecisionService.txt
/MAGI/Household/MAGI_EDG_FLOW.erf;/Medicaid/MAGI_EDG_FLOW;4;4
/VCL/VCL_Ruleflow_1.erf;/VCL/VCL1_EBDC_FLOW;4;4
/VCL/VCL_Ruleflow_2.erf;/VCL/VCL2_EBDC_FLOW;4;4

I tried this:

cat DecisionService.txt |  cut -d ';' -f2 | cut -d '/' -f2 | tr -s ' ' '\n'

My output is:

$i=Medicaid
VCL
VCL

Whereas I need the output to be:

$a=Medicaid
$b=VCL
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
iaav
  • 484
  • 2
  • 9
  • 26
  • How do you choose the variable names? Why are you using `$i=xxxx` notation instead of shell assignment `i=xxxx` notation? Are you looking for unique names? Must they be kept in order? What happens if a name such as VCL appears in lines 2, 3, 5 (with something else in line 4)? It appears that you want the top-level directory listed after the first semi-colon in the line — is that correct? – Jonathan Leffler May 08 '13 at 19:26

2 Answers2

1

If you just want the unique values then:

awk -F'/' 'NF&&!a[$(NF-1)]++{print $(NF-1)}' file
Medicaid
VCL

If you actually want the output to contain prefixed incremental variables then:

awk -F'/' 'NF&&!a[$(NF-1)]++{printf "$%c=%s\n",i++,$(NF-1)}' i=97 file
$a=Medicaid
$b=VCL

Note: If your input may contain more than 26 unique value you will need to do something cleverer to avoid output such as $|=VCL.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • Thanks but i get this: awk: (FILENAME=DecisionService.txt FNR=4) fatal: attempt to access field -1 – iaav May 08 '13 at 14:50
  • @iaav then you have empty lines in the file, easily fixed, see update. – Chris Seymour May 08 '13 at 14:56
  • This will work for the posted input but it's better to test `NF` than `$0` when checking for empty records as the latter will fail if the record is populated with a numerical zero. In this case the problem you're trying to avoid occurs due to NF being zero so it seems more natural to test for NF being non-zero anyway. – Ed Morton May 08 '13 at 16:30
  • @iaav great, you should now accept this answer by clicking the tick mark next to it, this will show the question as solved. – Chris Seymour May 08 '13 at 17:33
  • lol `you should now accept this answer` :D :D, why not `that` answer :D seems he copy pested same line to all answers. Just kidding. Btw,@sudo_O, seems you are one person where i can expect all my `awk` problems solved. I've been following your awesome awk one liners. great~~ – abasu May 08 '13 at 17:41
  • true, iaav accept whichever answer was of the most use to you. @abasu ha thanks. – Chris Seymour May 08 '13 at 17:46
0

Well from the question, it's not much clear what exactly you want, but i guess you don't want repeated VCL in output. Try adding sort and uniq at the end.

cat DecisionService.txt
/MAGI/Household/MAGI_EDG_FLOW.erf;/Medicaid/MAGI_EDG_FLOW;4;4
/VCL/VCL_Ruleflow_1.erf;/VCL/VCL1_EBDC_FLOW;4;4
/VCL/VCL_Ruleflow_2.erf;/VCL/VCL2_EBDC_FLOW;4;4

cat DecisionService.txt |  cut -d ';' -f2 | cut -d '/' -f2 | tr -s ' ' '\n'|sort|uniq
Medicaid
VCL
abasu
  • 2,454
  • 19
  • 22
  • strange, `uniq` should maintain the line separation. How you are treating the output? Try adding another pipe at the end and then feed the data through `xargs`. I tested like `cat DecisionService.txt | cut -d ';' -f2 | cut -d '/' -f2 | tr -s ' ' '\n'|sort|uniq|xargs touch`. So this created 2 distinct files, `Medicaid` and `VCL` – abasu May 08 '13 at 15:03
  • cat DecisionService.txt | cut -d ';' -f2 | cut -d '/' -f2 | tr -s ' ' '\n'|sort|uniq|xargs touch -- Gives no output. – iaav May 08 '13 at 15:37
  • `I tested` it means that I tested that the output comes in separate lines. `touch` does not give anything output. It created empty files in the run directory :) you should replace touch with what you want to do, also be sure to check `xargs` manpage to see how arguments are passed. `xargs` passes each output line separately to the command passed to it. – abasu May 08 '13 at 15:43