Grab certain part of text

I want to grab and bring back a certain part of text within a file

Bash Script

example file

H|10|1137865333|M|04|??|000389||N|U| |UN|||02/11/2023|||00000.00| |00000.00|

1137865333 is the part i want to bring back

image

Hi Theowi,

Is this text selected because of its position (3rd field in pipe separated text file)?

If so using cut with the pipe as the delimiter and selecting the 3rd field should do the job

cut -d\| -f3
2 Likes

Hi @Theowi,

awk can do this too.

awk -F\| '{print $3}' /path/to/file.ext

@Skrynesaver recommendation to use cut is probably simpler than my suggestion to use awk. It's definitely the simpler of the utilities. However, the simple request is often, but not always, only one part of a larger process and sometimes that's when awk shines.

1 Like

Thank you the cut option did work but i will try out the awk as well.

Might comment at a later stage again for a search with value i returned from that previous command

Any particular reason for why it "did not work"? It should have worked correctly (unless... reasons: a typo, an inconsistent input data, missing filename, etc.)
Instead of "escaping" the pipe, you might try quoting the pipe, but the result should be exactly the same:

cut -d'|' -f3 /path/to/file.txt

Lol i said it did work

Right, sorry, not enough coffee today, and too late for the next cup already :slight_smile:
Anyway, for future reference, may also treat 'did not work' as 'would not work' :wink:

1 Like