1

I'm using rhel7 and trying to print my output from shadow for example to be like this :

test1 !!
test2 !!
test3 *

I am running this command:

i=`cat /home/mydir/shadow1 |awk -F ':' '{print $1,$2}' `

When I do echo $i, it will print the output in 1 line:

test !! test2 !! test3 *

Can anyone advice how should I modify my '{print $1,$2}' so that it will print a new line?

schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57
msmystic
  • 21
  • 2
  • 1
    You've not double quoted your variable when you use it, so the shell has re-interpreted the result - and this includes replacing a white space (and newlines) with single spaces – roaima Jan 26 '21 at 08:24
  • You may also want to look at `passwd --status` – roaima Jan 26 '21 at 08:25
  • Finally, don't use backticks for command evaluation. Use the relatively modern `$( command )` syntax instead – roaima Jan 26 '21 at 08:26
  • Do you need your shell variable `i` for anything else? If not, there's no point in having it at all. Just `awk ... shadow1`. – Kusalananda Jan 26 '21 at 09:31

3 Answers3

0

Your awk print-statement is all fine.

Use quotes for echo:

echo "$i"
user3188140
  • 321
  • 1
  • 4
0

The question is clear. This problem can be approached in different ways. Not sure what the intention is but you can use for example the following:

while IFS=: read -r user stat _; do echo "$user" "$stat"; done < /etc/shadow

If you want to store the whole output of awk you could use something like this:

 var=$(awk -F: '{print $1,$2}' /etc/shadow)

You'd then just need to echo "$var" to print the results.

Valentin Bajrami
  • 9,244
  • 3
  • 25
  • 38
0

Instead of echo $i, try using:

echo -e "$i\n"

This adds a line break at the end of the line.

Hope it helps.

Luis Talora
  • 376
  • 2
  • 6