Extract value from file

Hi,
I have a file where data is pipe seperated. I want to extract a field which is 12 characters in length and starts with BBG(|BBG000KFWBB1|). Both has pipe delimeter at the start and end. The value can be anywhere in the file.

I want to extract this field that starts with BBG from the file(inputfile.out). File has 70k or more rows. My output should be BBG000KFWBB1.

regards
Sam

perl -ne 'print "$1\n" if(/.*?\|(.*)\|.*/)' inputfile.out

Not sure what you want, can you post more real data?

awk 'a=match($0,/\|............\|/) {print substr($0,a+1,12)}'

My input file looks like below . I want to extract the field value which starts with BBG. This value is of 12 chars in length.

bcd|BBG000KFWBB1|yup|12f
abc|BBG000KFEXB1|red|34f
fuf|BBG001UPRTN7|tum|45f

The output should be like below written to a output.out file

BBG000KFWBB1
BBG000KFEXB1
BBG001UPRTN7

Then this should do it

awk 'a=match($0,/\|BBG.........\|/) {print substr($0,a+1,12)}' file

Does your file have other types of filed, other with of field?

awk -F\| '{print $2}' file
1 Like

Thanks it worked :slight_smile:

nawk 'a=match($0,/\|BBG.........\|/) {print substr($0,a+1,12)}' temp

Also need some details of how it does . What does $0 do and /\?

If you look at your AWK man page or search for AWK tutorials online, you'll quickly find the answer to you $0 question.

/ is a regular expression delimiter and \ is used to escape the character that follows it. If you are not familiar with regular expressions, you need to set aside some time for study.

Regards,
Alister

1 Like