Hi. Purpose, “print only the string from all line”
My example-file is:
Row, 1, first, a, a, a, a, a, NUMBER-100111, and, WORD-something, a, a
Row, 2, And, second, number, could, be, NUMBER-100222, and, WORD-something_else, b,
Row, 3, Last , line, a, a, a, a, a, a, a, NUMBER-100322, b ,b ,b ,b ,b ,b, WORD-hello
etc“command should be:”
grep “NUMBER-xxxxxx” and “WORD-xxxxxxx” and print just NUMBER and WORD” > new_fileAnd new_file should be as below.
NUMBER-100111, WORD-something
NUMBER-100222, WORD-something_else
NUMBER-100322, WORD-helloIs this possible?
BR
johan
message edited by johan
It's possible, but not with just grep as grep returns the entire line if the NUMBER or WORD pattern exist. How about an awk script that cycles thru each field (Field Seperator is a comma) and captures the field if it starts with NUMBER of WORD:
#!/bin/ksh # look at each field in the line. Assume if each line has a NUMBER, # it has a WORD # using nawk because I'm using Solaris nawk ' FS="," { gsub(" ","") # get rid of all spaces f1="" f2="" for (i=1; i<=NF; i++) { if( $i ~ /^NUMBER/) f1=$i if($i ~ /^WORD/) f2=$i } printf("%s,%s\n", f1, f2) } ' examfile.txtmessage edited by nails
Thank you for your prompt reply. Your rule works well. BR
johan