run command against txt file

marka01

New Member
Joined
Jan 29, 2021
Messages
4
Reaction score
0
Credits
30
hi, how do I run i.e. "cat /etc/passwd" against all lines in txt file(those are users) and get output on the screen?

thank you
 


f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,893
Reaction score
2,631
Credits
28,122
Code:
cat /etc/passwd | cut -d ":" -f1
or
Code:
awk -F: '{ print $1 }' /etc/passwd
 
Last edited:

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,893
Reaction score
2,631
Credits
28,122
You want to send the output to a file?
Code:
cat /etc/passwd | cut -d ":" -f1 > file1.txt
awk -F: '{ print $1 }' /etc/passwd > file2.txt
 
OP
M

marka01

New Member
Joined
Jan 29, 2021
Messages
4
Reaction score
0
Credits
30
Sorry I think I formulated my question really badly.. I have txt file with about 1000 lines with user names. What I want is to run this command against each user in txt file:
cat /etc/passwd | grep user

and get output on a screen or to another txt file
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,893
Reaction score
2,631
Credits
28,122
Can you give an example of what you would like to see and you want all the users in a different text file?
 
OP
M

marka01

New Member
Joined
Jan 29, 2021
Messages
4
Reaction score
0
Credits
30
Can you give an example of what you would like to see and you want all the users in a different text file?

I have txt with user1,user2,user3..etc (in lines). I want to run 'cat /etc/passwd | grep user. And get this output for each user in TXT file. Does it make sense ?
 

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
3,893
Reaction score
2,631
Credits
28,122
Do you want all the information per user in a file? I think you want something like this?
Code:
#!/bin/bash

mkdir users

for user in $(awk -F: '{ print $1 }' /etc/passwd)
    do
        grep $user /etc/passwd > users/$user.txt
    done
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top