0

I need to extract substrings from a file into a new file. Mac or Linux.

The data is between the 4th and 5th "|" symbol.

HD|262339|9400530374||K7UKD|A|HA|12/15/2009|03/13/2020

The actual columnar position varies, sometimes by a lot, but the data is always between the 4th and 5th pipe symbol.

Sample data is as above, expected output would be K7UKD.

I've tried various hacks at a regex:

grep  "/\|(\w+)\|/" input.txt > output.txt
anubhava
  • 761,203
  • 64
  • 569
  • 643
Alan
  • 1,265
  • 4
  • 22
  • 44
  • 2
    Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Feb 28 '21 at 16:38
  • One attempt added to question along with expected output. Sample input was in the original question. – Alan Feb 28 '21 at 16:44
  • 3
    Try: `awk -F'|' '{print $5}' file` – anubhava Feb 28 '21 at 16:47

1 Answers1

3

Converting my comment to answer so that solution is easy to find for future visitors.

There are 2 ways to get it:

Any awk version:

awk -F'|' '{print $5}' file

K7UKD

or using gnu-awk:

awk -v RS='|' 'NR == 5' file

Here is a bash solution using read:

IFS='|' read -ra arr <<< 'HD|262339|9400530374||K7UKD|A|HA|12/15/2009|03/13/2020' &&
echo "${arr[4]}"

K7UKD

Or using cut:

cut -d'|' -f5 file

Or using sed:

sed -E 's/^([^|]*\|){3}\|([^|]*).*/\2/' file
anubhava
  • 761,203
  • 64
  • 569
  • 643