0

I'm trying to display part of the content of a file. I currently use the following code to cut and format the fields I'm interested in

CONTENT="(cat foo.dat | cut -d ' ' -f 1,4,5)"
TOPRINT=$(printf "%7s  %10s  %14s\n" $(eval $CONTENT))

Now my problem is I would like to further edit the 5th field that looks like

bla=0.,bleh=2.,name=hello_im_here

as the only part I want to display is the name ("hello_im_here"). I'm guessing I could use awk (as is done in cut string in a specific column in bash) but as I'm not at all familiar with this command, I would appreciate more specific input on this. Thanks in advance.

Community
  • 1
  • 1
Neutrinoceros
  • 228
  • 3
  • 9
  • What did you try to make yourself familiar with `awk`? – hek2mgl Nov 02 '16 at 13:38
  • Maybe I should have emphasized that I don't see how to deal with _only_ the 5th field of my file while leaving the rest untouched. Otherwise I would not ask. And as the title suggests I would rather use cut if possible because it makes the code easier to read for users. – Neutrinoceros Nov 02 '16 at 13:45
  • So what did you try to make it work with `cut`? – hek2mgl Nov 02 '16 at 13:49
  • I tried using arrays `ARR1=$(cat foo.dat | cut -d ' ' -f 1,4)` and `ARR2=$(cat foo.dat | cut -d ' ' -f 5 | cut -d '=' -f -1)` but then I don't know how to stack them in the `printf` line. I tried `TOPRINT=$(paste < ("%7s %10s\n" "${(eval $CONTENT)[@]}"))` but this does work and I don't know why Also this doesn't feel like the most elegant solution even then, so I favored asking for the core question. – Neutrinoceros Nov 02 '16 at 14:11
  • Question is not very clear. Giving us a sample of `foo.dat` and an expected output is your best bet to get a fitting answer. – John B Nov 02 '16 at 14:25
  • Thank you for this constructive feedback. I'll apply it next time. – Neutrinoceros Nov 02 '16 at 14:44

1 Answers1

1

could you try this;

#!/bin/bash
CONTENT=$(awk '{gsub(" *.*=","",$5); printf "%7s  %10s  %14s\n",$1,$4,$5}' foo.dat)
echo $CONTENT
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24