8

Got here a list of columns, as follows:

59  LOUIS   1202    +1
60  FREDDIE 1201    +4
61  FINLAY  1200    -2
62  LEON    1137    +12
63  HARLEY  1132    +6
64  DAVID   1127    -1
65  MOHAMMAD1100    +6
66  REECE   1095    -1
67  KIAN    1090    0
68  KAI     1056    -6
69  KYLE    1030    -18
70  BRANDON 1011    -4
71  HAYDEN  1006    +5
72  ZACHARY 995     +10
73  KIERAN  973     -12
73  LUCA    973     -1
75  ASHTON  954     +4
76  BAILEY  939     -6
77  JAKE    913     +10
78  GABRIEL 910     +14
79  SAM     900     -2
80  EVAN    890     0
81  BRADLEY 847     -13

How could be extracted only the lines with, for example, letter "L", as follows:

73  LUCA    973     -1
  • 3
    Does the L need to be in the second column? How are columns defined (spaces? tabs?)? Can the L be anywhere in the column or does it have to be the first letter? Your question says only "with", but you seem to be expecting "starts with". Please edit your question and clarify. – terdon Oct 15 at 18:23
  • Its to extract the lines which contains the name with given letter, for example: Need here lines which names starts with letter "E", then it should return a file that contains 80 EVAN 890 0 only, just as the guy down there just answered. – Rubem74 Oct 15 at 18:46
  • Just curious about the [sed] tag: does the solution have to use sed? – Christopher Schultz Oct 18 at 19:00
5

That seems to be duplicated, but anyway, if it was understood, one may do it as follows:

First, save the list in a some nameslist.txt file, then:

sed -rn '/^[^\s]+\s+[F]/p' list.txt > result.txt, which should return following output in result.txt file:

60 FREDDIE 1201 +4 61 FINLAY 1200 -2

19
awk '$2 ~ /^L/' file

Tells awk to test the second field $2 using the regex ~ expression ^L and print the matching lines.

The regex matches any field (string) that starts ^ with an L

  • awk 'substr($2, 1, 1) == "L"' file would also work – glenn jackman Oct 15 at 19:59
  • @glennjackman Are there any benifits to using substr over a regex field match as in this answer? – Paul Evans Oct 16 at 22:33
  • Don't know. There may be a new microseconds performance benefit, but there may not be. I was just pointing out an alternate function. – glenn jackman Oct 17 at 0:54
7

Building on @bu5hman's answer: With awk, you can easily pass the letter as a parameter:

awk -v letter="L" 'substr($2, 1, 1) == letter' file
# or with a constructed regex
awk -v letter="L" '$2 ~ "^" letter' file
  • 3
    +1. The $2 ~ ... version also allows regexp searches like -v letter='[FL]' to match lines where the name begins with either an F or an L. – cas Oct 16 at 9:12
2

Assuming that you have that list in list.txt, you can do the following:

 grep -h L list.txt > list_L.txt 

Explanation: grep L list.txt gives you all the lines containing an uppercase L. The option -h suppresses the output of the file name, but is only necessary if you are searching through multiple files (e.g. grep -h L lis*.txt). Adding > list_L.txt redirects the lines to the specified file.

If the L must only occur at the beginning of a "word" (on name, in this case), you need to tweak the above command a bit:

 grep -h '\<L' list.txt > list_L.txt 

Adding \< matches the beginning of a "word", so \<L selects lines containing a word beginning with an uppercase L.

2

Another grep example:

grep -E "[0-9]+ +L" file

+ matches one or more occurrence of the previous character, so in this case we match at least one numeric character, and at least one space, followed by a capital L.

-E is required to interpret the + as a special character instead of a literal '+'

output:

59  LOUIS   1202    +1
62  LEON    1137    +12
73  LUCA    973     -1
1

Avoid RegEx when possible. AWK example:

index($2, "L") == 1
  • Why do you think it's a bad idea to answer with a working example like awk 'index($2, "L") == 1' file like everybody else does? – Freddy Oct 17 at 0:30
  • @Freddy I posted my code carefully and purposefully. Your code is a valid shell script, while mine is a valid AWK script. Please stop changing it. – user327359 Oct 17 at 0:45
  • I stopped changing it, I'm just curious. This question is tagged with sed and not everybody knows how to run your script, that's all. – Freddy Oct 17 at 0:50
  • @Freddy its public information for at least 20 years now... pubs.opengroup.org/onlinepubs/007908799/xcu/awk.html – user327359 Oct 17 at 1:54
  • This is about as useful as answering with just oldfile newfile without bothering to mention cp, when someone asks "how do i copy a file?" and then snarkily pointing out that cp has been public information for decades. Actually, it's worse than that because "avoid regex when possible" is a terrible answer for a simple text processing problem. – cas Oct 21 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.