Y
Y
Yuri Gubko2016-12-19 23:21:52
bash
Yuri Gubko, 2016-12-19 23:21:52

How to find a word in a line of a file (bash)?

There is the following task: there is a log file which contains records about the users who used the server. Each line contains a record of one session (date, time, ip, user, etc.).
How can I extract the username from this file? it is known that all lines are ordered, i.e. the username is exactly between the 6th and 7th spaces.
log example:

Sun Oct 12 11:23:54 2016 101.45.x.x User  ........... .. .  . .

how to implement it as a script in bash?
UPD: did this:
#!/bin/bash
cut -d " " -f 6

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cap_nemo, 2016-12-19
@cap_nemo

Alternatively, make a similar script using AWK:
cat filename.log | gawk 'BEGIN { FS=" " } // { print $7 }'

S
Saboteur, 2016-12-19
@saboteur_kiev

Use
cut -d "<separator, e.g. space>" -f <number of desired column"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question