0
1598427@931
PDD   220624P00051000 ohlc=0,0,0,0 vol=0 oi=424 nbbo=69@2316/113@532 nbbo2=69@145/113@95
PDD   220617C00051000 ohlc=0,0,0,0 vol=0 oi=434 nbbo=530@1921/710@1496 nbbo2=530@31/710@115
PDD   220722P00051000 ohlc=0,0,0,0 vol=0 oi=15 nbbo=285@1436/405@1772 nbbo2=230@15/455@15
PDD   220708C00051000 ohlc=0,0,0,0 vol=0 oi=17 nbbo=785@864/935@894 nbbo2=785@15/935@15
PDD   220624C00051000 ohlc=0,0,0,0 vol=0 oi=392 nbbo=645@771/795@947 nbbo2=645@83/795@80
PDD   220729C00051000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=870@902/1190@677 nbbo2=820@15/1195@20
PDD   220708P00051000 ohlc=0,0,0,0 vol=0 oi=32 nbbo=200@1413/320@2273 nbbo2=200@15/320@356
PDD   220722C00051000 ohlc=0,0,0,0 vol=0 oi=140 nbbo=795@1630/1175@1544 nbbo2=795@51/1175@21
PDD   220729P00051000 ohlc=0,0,0,0 vol=0 oi=11 nbbo=254@3/450@3 nbbo2=254@2/570@1
CSCO  220715C00090000 ohlc=0,0,0,0 vol=0 oi=739 nbbo=0@0/4@1056 nbbo2=0@0/4@121
CSCO  220617C00090000 ohlc=0,0,0,0 vol=0 oi=203 nbbo=0@0/1@2 nbbo2=0@0/0@0
CSCO  220617P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4685@654/4730@1155 nbbo2=4685@33/4730@33
CSCO  240119P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4695@202/4770@193 nbbo2=4695@75/4770@33

I have a file that looks like the above.

I want to find all lines that contain the word CSCO, or has length <= 15.

What command can I use to do this?

1
  • 2
    you used awk, sed and grep tags which all are capable to do this task nicely, then you have asked "What command can I use to do this?"!! have you tried something that didn't work? Jun 28 at 4:07

4 Answers 4

5

With grep -E for Extended regexps you can use alternation (|).

$ grep -E 'CSCO|^.{0,15}$' file
1598427@931
CSCO  220715C00090000 ohlc=0,0,0,0 vol=0 oi=739 nbbo=0@0/4@1056 nbbo2=0@0/4@121
CSCO  220617C00090000 ohlc=0,0,0,0 vol=0 oi=203 nbbo=0@0/1@2 nbbo2=0@0/0@0
CSCO  220617P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4685@654/4730@1155 nbbo2=4685@33/4730@33
CSCO  240119P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4695@202/4770@193 nbbo2=4695@75/4770@33

If you want to match "CSCO" only when it's at the beginning of the line and followed by white-space (spaces, tabs, etc):

$ grep -E '^CSCO[[:space:]]|^.{0,15}$' file

Or use the end-of-word boundary marker, \> (I can't remember if this is a GNU extension or if it's "standard", and it's a difficult thing to google for. It definitely works in GNU grep, and maybe others):

$ grep -E '^CSCO\>|^.{0,15}$' file

Note that GNU grep's info documentation defines a "word" character as [_[:alnum:]], which is different to the definition in the perlre man page, perl also recognises some connector punctuation and unicode characters as "words".

If you're using GNU grep, that version also understands perl's \s (any whitespace) and \b (word boundary marker), even when using -E rather than -P. GNU grep's -P option for PCRE support adds recognition of \h for horizontal whitespace. e.g.

$ grep -E '^CSCO\s|^.{0,15}$' file
$ grep -E '^CSCO\b|^.{0,15}$' file
$ grep -P '^CSCO\h|^.{0,15}$' file
5
grep -e CSCO -e '^.\{0,15\}$' filename

Here you give grep two patterns to look for. The first is "CSCO": so it'll match any lines with that. The second looks for the start of the line ^ followed by any character . occurring 0 to 15 times \{0,15\} followed by the end of the line $.

If CSCO must be a full "word" and not part of a larger word or pattern, you can use '\<CSCO\>' or '\bCSCO\b' or '[[:<:]]CSCO[[:>:]]' depending on your grep implementation instead (\<, \> being the most commonly supported), all those being word boundary operators that match at the transition between a word character (alnums or underscores) and a non-word character (all the others).

1

I assume CSCO is on the 1st filed. The awk method:

awk '$1 == "CSCO"; length <= 15 { print }' INPUT
# length is a built-in awk function which returns the length of a line
1

Using Raku (formerly known as Perl_6)

raku -ne '.put if (.contains("CSCO")) | (.chars <= 15);'   

OR

raku -ne '.put if .contains("CSCO") or .chars <= 15;'   

Sample Input:

1598427@931
PDD   220624P00051000 ohlc=0,0,0,0 vol=0 oi=424 nbbo=69@2316/113@532 nbbo2=69@145/113@95
PDD   220617C00051000 ohlc=0,0,0,0 vol=0 oi=434 nbbo=530@1921/710@1496 nbbo2=530@31/710@115
PDD   220722P00051000 ohlc=0,0,0,0 vol=0 oi=15 nbbo=285@1436/405@1772 nbbo2=230@15/455@15
PDD   220708C00051000 ohlc=0,0,0,0 vol=0 oi=17 nbbo=785@864/935@894 nbbo2=785@15/935@15
PDD   220624C00051000 ohlc=0,0,0,0 vol=0 oi=392 nbbo=645@771/795@947 nbbo2=645@83/795@80
PDD   220729C00051000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=870@902/1190@677 nbbo2=820@15/1195@20
PDD   220708P00051000 ohlc=0,0,0,0 vol=0 oi=32 nbbo=200@1413/320@2273 nbbo2=200@15/320@356
PDD   220722C00051000 ohlc=0,0,0,0 vol=0 oi=140 nbbo=795@1630/1175@1544 nbbo2=795@51/1175@21
PDD   220729P00051000 ohlc=0,0,0,0 vol=0 oi=11 nbbo=254@3/450@3 nbbo2=254@2/570@1
CSCO  220715C00090000 ohlc=0,0,0,0 vol=0 oi=739 nbbo=0@0/4@1056 nbbo2=0@0/4@121
CSCO  220617C00090000 ohlc=0,0,0,0 vol=0 oi=203 nbbo=0@0/1@2 nbbo2=0@0/0@0
CSCO  220617P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4685@654/4730@1155 nbbo2=4685@33/4730@33
CSCO  240119P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4695@202/4770@193 nbbo2=4695@75/4770@33

Sample Output:

1598427@931
CSCO  220715C00090000 ohlc=0,0,0,0 vol=0 oi=739 nbbo=0@0/4@1056 nbbo2=0@0/4@121
CSCO  220617C00090000 ohlc=0,0,0,0 vol=0 oi=203 nbbo=0@0/1@2 nbbo2=0@0/0@0
CSCO  220617P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4685@654/4730@1155 nbbo2=4685@33/4730@33
CSCO  240119P00090000 ohlc=0,0,0,0 vol=0 oi=0 nbbo=4695@202/4770@193 nbbo2=4695@75/4770@33

The answer in Raku needs to respect precedence of OR commands in relation to other code elements. Above, the desired answer is obtained in the first example by placing parentheses around the two conditionals on either side of the (high-precedence) | (pipe) OR operator. If you want to drop the parens, use the (low-precedence) or (lowercase) OR operator.

Note this quote from the second URL below: "Please note that or is easy to misuse. See traps."

https://docs.raku.org/language/operators#Operator_precedence
https://docs.raku.org/routine/or
https://docs.raku.org/routine/|

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.