Skip to content
Advertisement

Searching and selecting strings from a file

I have a trouble in separating few exact ‘fields’ with strings and then putting them into .txt file. I need to extract ‘nologin’ users from /etc/passwd file and that is an easy step. I’m using this command:

JavaScript

cat command gives me for example:

JavaScript

and it is saved to file1.txt

Now I have to extract from file1.txt a number (2, 3, 4), login (daemon, bin, sys) UID and shell. It should look like this

JavaScript

I also have to save that output to a *.txt file. How can I achieve this?

Advertisement

Answer

You can use the cut command like this:

JavaScript

According to the man page:

-d, –delimiter=DELIM

use DELIM instead of TAB for field delimiter

-f, –fields=LIST

select only these fields; also print any line that contains no delimiter character, unless the -s option is specified

Advertisement