Tuesday, May 14, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 402  / 3 Years ago, thu, may 27, 2021, 12:41:32

Suppose that I have an output of lines like this:


First name, Last name, 123456789(9 digits)
GPA
Class
Major
University

How can I write some code to print the first 3 lines and only keep the ID number in the first line?


I tried to use head, grep and cut, but I wasn't able to do it.


More From » command-line

 Answers
4

You could use awk, to print the last comma-space separated field:


$ cat file
First name, Last name, 123456789
GPA
Class
Major
University

$ awk -F', ' 'NR<4 {print $NF}' file
123456789
GPA
Class

or sed to replace the longest string up to comma-space:


$ sed -n '1,3{s/.*, //; p}' file
123456789
GPA
Class

[#1688] Friday, May 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nerta

Total Points: 414
Total Questions: 103
Total Answers: 97

Location: England
Member since Wed, Apr 19, 2023
1 Year ago
nerta questions
Thu, May 12, 22, 16:04, 2 Years ago
Thu, Dec 2, 21, 09:19, 3 Years ago
Sun, Dec 11, 22, 08:33, 1 Year ago
Tue, Sep 28, 21, 16:37, 3 Years ago
;