2

I have this code:

list=$(ls -l | awk '{print$3,$6,$7,$8,$9}' | grep -v root)
echo $list

and the output is a joint version of all the filtered strings which looks like:

> avahi Nov 16 21:50 1170 syslog Nov 16 21:50 1171 messagebus ...

and what I wanted looks like:

> avahi Nov 16 21:50 1170
> syslog Nov 16 21:50 1171
> messagebus Nov 16 21:50 1179
> ...

I've also read that the default output of grep should give me the paragraphs version like what I want but this isn't the case. What am I doing wrong?

PTwno
  • 41
  • 4

1 Answers1

6

Given:

$ txt="line 1
> line 2
> line 3
> line 4"

(ie, txt being set to line 1\nline 2\nline 3\nline 4)

Consider:

$ echo $txt
line 1 line 2 line 3 line 4

vs:

$ echo "$txt"
line 1
line 2
line 3
line 4

Use quotes or Bash will interpret and split the whitespace (like the \n at the end of lines.)

Separately, it is usually not a good idea to try and parse the output of ls since file names can have spaces in them. The spaces in the file names will break the rest of your pipe (as you have written it.)

Use a loop with a glob instead.

dawg
  • 98,345
  • 23
  • 131
  • 206