Need help please with Grep/Sed command to extract text and numbers from a file

Hello All,

I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created.

My problem is that sometimes 16 appears in other part of the line. I'm only interested to print lines that contain 16 which comes after ALPHANUMERIC
Basically the 16 is the lentgh of ALPHANUMERIC Charcters.
In the first line for example ALPHANUMERIC 16 << I need to print
lines that contain ALPHANUMERIC and that it is set t o16

Can u please help??? I tried many grep command but cannot get this working. I do not know perl and never worked with sed

DSPLY_NAME "DISPLAY NAME"          3  NULL        ALPHANUMERIC       16  RMTES_STRING    16
YTM_HIFLAG "YTM HIGH FLAG"       160  NULL        ALPHANUMERIC        1  RMTES_STRING     1
YTM_LOFLAG "YTM LOW FLAG"        161  NULL        ALPHANUMERIC        1  RMTES_STRING     1
BUYER_ID   "BUYER ID"            162  NULL        ALPHANUMERIC        4  RMTES_STRING     4
SELLER_ID  "SELLER ID"           163  NULL        ALPHANUMERIC        4  RMTES_STRING     4
MKT_MKR_NM "MRKT MAKER NAME"     214  NULL        ALPHANUMERIC       16  RMTES_STRING    16
ROW64_2    "MONROW 2"            216  NULL        ALPHANUMERIC       64  RMTES_STRING    64
ROW80_2    "IRGROW 2"            316  NULL        ALPHANUMERIC       80  RMTES_STRING    80
 echo "DSPLY_NAME "DISPLAY NAME" 3 NULL ALPHANUMERIC 16 RMTES_STRING 16
YTM_HIFLAG "YTM HIGH FLAG" 160 NULL ALPHANUMERIC 1 RMTES_STRING 1
YTM_LOFLAG "YTM LOW FLAG" 161 NULL ALPHANUMERIC 1 RMTES_STRING 1
BUYER_ID "BUYER ID" 162 NULL ALPHANUMERIC 4 RMTES_STRING 4
SELLER_ID "SELLER ID" 163 NULL ALPHANUMERIC 4 RMTES_STRING 4
MKT_MKR_NM "MRKT MAKER NAME" 214 NULL ALPHANUMERIC 16 RMTES_STRING 16
ROW64_2 "MONROW 2" 216 NULL ALPHANUMERIC 64 RMTES_STRING 64
ROW80_2 "IRGROW 2" 316 NULL ALPHANUMERIC 80 RMTES_STRING 80" |grep -Po 'ALPHANUMERIC [0-9]+'
ALPHANUMERIC 16
ALPHANUMERIC 1
ALPHANUMERIC 1
ALPHANUMERIC 4
ALPHANUMERIC 4
ALPHANUMERIC 16
ALPHANUMERIC 64
ALPHANUMERIC 80

Hi & thanks

But when I type ( where test is the name of my text file )

grep -Po 'ALPHANUMERIC' [0-9]+' test
>

I just get the prompt > ?

I'm in bash? does that make a difference?

---------- Post updated at 01:33 PM ---------- Previous update was at 01:31 PM ----------

sorry Imade a typo

grep -Po 'ALPHANUMERIC [0-9]+' test

I type the above and nothing happens?

remove the single quote after "ALPHANUMERIC", and try again

I also need to print the entire line from begining to end
not just APLHANUMERIC 16 ?
I need the full line like in thei example

RELNEWS "RELATED NEWS" 5757 NULL ALPHANUMERIC 16 RMTES_STRING 16

thanks

grep -w 'ALPHANUMERIC [0-9]*'

this is what I types and it produces blank- Also I need t oprint the entire line

grep -o 'ALPHANUMERIC [0-9]*' test

using -w, as the code in the above "#6"

This is what I have in my text file called test

DSPLY_NAME "DISPLAY NAME"          3  NULL        ALPHANUMERIC       16  RMTES_STRING    16
YTM_HIFLAG "YTM HIGH FLAG"       160  NULL        ALPHANUMERIC        1  RMTES_STRING     1
YTM_LOFLAG "YTM LOW FLAG"        161  NULL        ALPHANUMERIC        1  RMTES_STRING     1

I only want to print the line in which the alphanumeric values in the line is set to 16
So the output should be this extracted from the 3 lines above

DSPLY_NAME "DISPLAY NAME"          3  NULL        ALPHANUMERIC       16  RMTES_STRING    16

Thanks

---------- Post updated at 01:41 PM ---------- Previous update was at 01:40 PM ----------

grep -w 'ALPHANUMERIC [0-9]*'
this prints all the lines in the file
Thanks

---------- Post updated at 01:42 PM ---------- Previous update was at 01:41 PM ----------

So basically I need t oread this huge file with tons of lines.
I need to print only lines in which the ALPHANUMERIC field is set to value of 16

As I showed in example above.

echo "DSPLY_NAME "DISPLAY NAME" 3 NULL ALPHANUMERIC 16 RMTES_STRING 16
> YTM_HIFLAG "YTM HIGH FLAG" 160 NULL ALPHANUMERIC 1 RMTES_STRING 1
> YTM_LOFLAG "YTM LOW FLAG" 161 NULL ALPHANUMERIC 1 RMTES_STRING 1
> " |grep -w 'ALPHANUMERIC 16'
DSPLY_NAME DISPLAY NAME 3 NULL ALPHANUMERIC 16 RMTES_STRING 16
1 Like

None of the suggested options worked

grep 'ALPHANUMERIC  *16 ' test

had you used code tags, it would have been solved on the first try.

1 Like
echo "DSPLY_NAME "DISPLAY NAME" 3 NULL ALPHANUMERIC 16 RMTES_STRING 16
> YTM_HIFLAG "YTM HIGH FLAG" 160 NULL ALPHANUMERIC 1 RMTES_STRING 1
> YTM_LOFLAG "YTM LOW FLAG" 161 NULL ALPHANUMERIC 1 RMTES_STRING 1
> " |grep -w 'ALPHANUMERIC 16'
DSPLY_NAME DISPLAY NAME 3 NULL ALPHANUMERIC 16 RMTES_STRING 16
 

This works is there was only one sapce between ALPHANUMERIC and number 16. The problem is that the spaces between ALPHANUMERIC and 16 varies

So I have a Line that has

Line 1 >  ALPHANUMERIC 16
And another
Line 2 > ALPHANUMERIC      16
 

So It does not work becuase it does not match exact string

---------- Post updated at 02:05 PM ---------- Previous update was at 01:58 PM ----------

Thank u both -Sorry new to the Forums - NOT USING CODE TAGS
Will use them in future
Thanks

$ ruby -ne 'print if /ALPHANUMERIC\s*16/' file
grep "ALPHANUMERIC.*16"

should work as long as there is no other 16 to the right of "ALPHANUMERIC". It will give incorrect output if there is the possibility of a line like:

ALPHANUMERIC 10 BLAH BLAH 16

edit: now that I think about it, this would be better: It matches one or more space:

grep -E "ALPHANUMERIC +16"