Score:0

How to print specific words from a file

za flag

I have a file which is like this

user:[dgalanos] rid:[0xa35]
user:[roleary] rid:[0xa36]
user:[smorgan] rid:[0xa37]

How can i print only the usernames like this

dgalanos
roleary
smorgan

I tried

cat users.txt | awk -F  ':' '{print $2 > "users3.out"}'

But it did not give me the correct result i expected.

αғsнιη avatar
cn flag
don't use [awk redirection](https://www.gnu.org/software/gawk/manual/html_node/Redirection.html) in `awk '{print $2 > "users3.out"}' input` when you don't needed and you actually wanted a shell redirection `awk '{print $2}' input > users3.out `.
Score:1
in flag

If you set a colon as your field separator, the line

user:[dgalanos] rid:[0xa35]

will be split to

user
[dgalanos] rid
[0xa35]

I guess you want to split at either colon or space, then remove the brackets:

Also, awk defaults to read from a file, no need for cat:

awk -F  '[: ]' '{gsub(/[][]/,"",$2); print $2}' users.txt > users3.out

Or you use the brackets as field separator:

awk -F  '[][]' '{print $2}' users.txt > users3.out

Anyways, for such task, I'd prefer grep:

grep -Po 'user:\[\K[^]]*' users.txt > users3.out
Score:0
gr flag

The most simple way is to modify your command as follows:

awk -F '[' '{print $2}' users.txt | awk -F ']' '{print $1}' > users3.out

  • I'm using two piped awk to catch at first what is after the [ and then what is after the ]. The delimiter are you interested in is not the semiclon, but the brackets.
  • Piping | means that the output of the first command is used as input of the second command. The second awk does not work if you use it alone, but it gets some sense only if you pipe it to the first command.
  • When piping, you don't have to repeat the input file: for this reason in the second awk there is no repetition of the input file users.txt. If you are curious to use the second awk alone, write: awk -F ']' '{print $1}' users.txt
  • You don't need to redirect cat output to awk because awk can accept files as input
  • Your redirection to an output file must not be inside the awk command, but at the end of the full command.

If you want to see the results in the terminal but not in a file, you can remove the part from >

There is a more elegant way to get the data without piping two awk commands but defining the two brackets as delimiters:

awk -F '[][]' '{print $2}' users.txt > users3.out

tcprks avatar
za flag
The command give desired result. However can you explain why the second print command did not print anything. awk -F '[' '{print $2}' users.txt | awk -F ']' '{print $1}' . Is it something like AND statement. it prints only if both conditions before/after the pipe satisfied.
Lorenz Keel avatar
gr flag
check my edited answer, it should clear your doubts.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.