Issue
How can i grep the first 3 letters of the below output and echo the result.
example:
more /etc/group |grep -i 1900
h10shm:x:1900:h10adm
I just want the first 3 letters (in above output h10) of the about output and echo the output.
Regards,
Satvik
Solution
If you have GNU grep
, try:
$ grep -iPo '^...(?=.*1900)' /etc/group
h10
If you don't havec GNU grep
, try:
$ grep -i 1900 /etc/group | grep -o '^...'
h10
Or:
$ sed -n 's/^\(...\).*1900.*/\1/p' /etc/group
h10
Or:
$ awk '/1900/{print substr($0,1,3)}' /etc/group
h10
Answered By - John1024 Answer Checked By - Senaida (WPSolving Volunteer)