Cut between two delimiters, / and .

BASH : I have a very long list I am parsing through:

10/10/19 13:47:24,unique,unique,5816-14-0-4756FAA-20181030000000-20181030000000-5817,4.900500,0.000000,2785,0,79,43645844,2801458,3,1376,79,16140365,11900137,5816,5817,4756FAA,14,0,17835,17835,10/30/18,10/30/18,71,0,0,0,/mount/extract/output/unique/999FAAA.325.unique.unique,10/30/18 00:00:00,10/30/18 00:00:00,43645844

Here's what I am currently doing

[user@server tmp]$ cut -d '/' -f 12 out.out
925FAAA.325.unique.unique,10

Here is what I need:

999FAAA

I've tried a few iterations of awk,. but I cant get that to work. Is there a way to pipe two cut commands together?

If your output were the input file then you would use another cut -d '.' -f 1 on it.
You can chain the two cut commands with a pipe:

cut -d '/' -f 12 out.out | cut -d '.' -f 1

cut reads from the input (stdin) unless it gets an argument (filename).

As the number of / may vary with path depth, or with the locale date format, it might be safer to extract the relevant field first from that comma delimited file:

cut -d ',' -f 30 file
 /mount/extract/output/unique/999FAAA.325.unique.unique

and then extract the filename part as suggested.

EDIT: Try one more iteration of awk :

awk -F, '{gsub ("^.*/|\..*$", _, $30); print $30}' file
999FAAA

You could try something like this too, except my "-f" value is "10":

#!/bin/bash
echo '10/10/19 13:47:24,unique,unique,5816-14-0-4756FAA-20181030000000-20181030000000-5817,4.900500,0.000000,2785,0,79,43645844,2801458,3,1376,79,16140365,11900137,5816,5817,4756FAA,14,0,
17835,17835,10/30/18,10/30/18,71,0,0,0,/mount/extract/output/unique/999FAAA.325.unique.unique,10/30/18 00:00:00,10/30/18 00:00:00,43645844' > /tmp/out.out

text=$( cut -d '/' -f 10 /tmp/out.out ); printf "%s\n" "${text%%.*}"

Results OSX 10.14.6, default bash terminal.

Last login: Thu Oct 10 21:41:52 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./cut_me.sh

999FAAA
AMIGA:amiga~/Desktop/Code/Shell> _

you can try below command

awk -F"/" '{print $12}' <file_name>   | awk -F"." '{print $1}'

or you can use

cut -d '/' -f 12 out.out  | awk -F"." '{print $1}'

Some more:

awk -F, '{n=split($(NF-3), F, "[/.]"); print F[n-3]}' file
sed 's|.*,/[^.]*/||; s|\..*||' file
1 Like
Moderator comments were removed during original forum migration.

Adding 1 more solution here, which may help future users(Thanks to auto bumping LOVING this feature :slight_smile: ) .

rev Input_file | awk -F, '{split($4,array,"[./]");print array[4]}' | rev

Thanks,
R. Singh

1 Like

I also like auto-bumping, if not too much. It's good for both new users and it's good for older members who might want to add a new solution, here and there; or even reformat an older post, etc.

It's also good to like older posts and share more love :slight_smile:

1 Like

As it's here, another possible approach taking advantage of a built in parser for the / separator
basename $(cut -d\, -f 30 tmp.dat) | cut -d\. -f1

1 Like