How to Print Linux Group Names on Multiple Line Instead of Single Line Output

How to print linux group names on multiple line instead of single line output

From what I see, you are trying to convert the groups of your user to an yaml array, try to use:

echo "groups:" ; for i in $(id -Gn myuser);do echo "  - $i" ;done

groups:
- users
- lp
- vboxusers
- kvm

You can use too:

echo "groups: [ $(groups myuser | sed -e 's/.\+\s\+:\s\+\(.\+\)/\1/g' -e 's/\(\s\+\)/, /g') ]"

groups: [ myuser, lp, vboxusers, kvm ]

How to print linux group names on multiple line instead of single line output

From what I see, you are trying to convert the groups of your user to an yaml array, try to use:

echo "groups:" ; for i in $(id -Gn myuser);do echo "  - $i" ;done

groups:
- users
- lp
- vboxusers
- kvm

You can use too:

echo "groups: [ $(groups myuser | sed -e 's/.\+\s\+:\s\+\(.\+\)/\1/g' -e 's/\(\s\+\)/, /g') ]"

groups: [ myuser, lp, vboxusers, kvm ]

How to concatenate multiple lines of output to one line?

Use tr '\n' ' ' to translate all newline characters to spaces:

$ grep pattern file | tr '\n' ' '

Note: grep reads files, cat concatenates files. Don't cat file | grep!

Edit:

tr can only handle single character translations. You could use awk to change the output record separator like:

$ grep pattern file | awk '{print}' ORS='" '

This would transform:

one
two
three

to:

one" two" three" 

Output groups one per line, with group name and group ID

And there comes perl:

$ id | perl -pe "s/.*=/,/;s/,(\d+)\(([^)]+)\)/'\2',\1\n/g"                                                                 
'arobert',1000
'adm',4
'cdrom',24
'sudo',27
'dip',30
'plugdev',46
'lpadmin',113
'sambashare',128

Explanations:

Two find and replace commands will be executed:

  • s/.*groups=/,/ is used to remove everything before the groups definition
  • s/,(\d+)\(([^)]+)\)/'\2',\1\n/g will put in place the following actions detailed @demo

It would be difficult to write something shorter than this.
Also if you are already good with sed and regex, than perl can be a nice plus to add to your list of skills.

As mentioned by Ed Morton, the current perl command generates an extra EOL at the end of the output. If you want to remove it you can use the following enhanced perl command:

$ id | perl -pe "s/.*=/,/;s/,(\d+)\(([^)]+)\)/'\2',\1\n/g;s/\n\n/\n/"

Thanks Ed!!!

How to output a multiline string in Bash?

Here documents are often used for this purpose.

cat << EOF
usage: up [--level <n>| -n <levels>][--help][--version]

Report bugs to:
up home page:
EOF

They are supported in all Bourne-derived shells including all versions of Bash.

Capturing multiple line output into a Bash variable

Actually, RESULT contains what you want — to demonstrate:

echo "$RESULT"

What you show is what you get from:

echo $RESULT

As noted in the comments, the difference is that (1) the double-quoted version of the variable (echo "$RESULT") preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas (2) the unquoted version (echo $RESULT) replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output with 'words' separated by single spaces (where a 'word' is a sequence of non-whitespace characters; there needn't be any alphanumerics in any of the words).

How can I extract a predetermined range of lines from a text file on Unix?

sed -n '16224,16482p;16483q' filename > newfile

From the sed manual:

p -
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.

n -
If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If
there is no more input then sed exits without processing any more
commands.

q -
Exit sed without processing any more commands or input.
Note that the current pattern space is printed if auto-print is not disabled with the -n option.

and

Addresses in a sed script can be in any of the following forms:

number
Specifying a line number will match only that line in the input.

An address range can be specified by specifying two addresses
separated by a comma (,). An address range matches lines starting from
where the first address matches, and continues until the second
address matches (inclusively).

splitting single line into multiple line in numbering format using awk

You could use sed instead:

sed "s#[^^]\([0-9]\.\) #\n\1 #g"

Example:

[~/Desktop]
==> echo "1. 11683 (<server01>: du.size[/,free] : 0.5 % 2. 21683 (<server02>: du.size[/,free] : 1.5 % 3. 31683 (<server03>: du.size[/,free] : 3.5 %" | sed "s#[^^]\([0-9]
\.\) #\n\1 #g"
1. 11683 (<server01>: du.size[/,free] : 0.5 %
2. 21683 (<server02>: du.size[/,free] : 1.5 %
3. 31683 (<server03>: du.size[/,free] : 3.5 %


Related Topics



Leave a reply



Submit