Cut between two delimiters, / and .


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Cut between two delimiters, / and .

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


Code:
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


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

Here is what I need:


Code:
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?

Last edited by Scrutinizer; 4 Weeks Ago at 04:58 AM.. Reason: Joined broken line in input sample
# 2  
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:
Code:
cut -d '/' -f 12 out.out | cut -d '.' -f 1

cut reads from the input (stdin) unless it gets an argument (filename).
# 3  
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:
Code:
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:
Code:
awk -F, '{gsub ("^.*/|\..*$", _, $30); print $30}' file
999FAAA

# 4  
You could try something like this too, except my "-f" value is "10":
Code:
#!/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.
Code:
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> _

# 5  
you can try below command

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

or you can use

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

# 6  
Some more:
Code:
awk -F, '{n=split($(NF-3), F, "[/.]"); print F[n-3]}' file

Code:
sed 's|.*,/[^.]*/||; s|\..*||' file

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #976
Difficulty: Medium
In April 2014, Linus Torvalds banned Kay Sievers from submitting patches to the Linux kernel for failing to deal with bugs that caused systemd to negatively interact with the kernel.
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut counting consecutive delimiters as fields

When cut encounters consecutive delimiters it seems to count each instance as a field, at least with spaces. Is this typical behavior for any delimiter? #:~$ ifconfig eth0 | grep HWaddr eth0 Link encap:Ethernet HWaddr 94:de:80:a7:6d:e1 #:~$ ifconfig eth0 | grep HWaddr | cut -d " " -f... (6 Replies)
Discussion started by: Riker1204
6 Replies

4. UNIX for Dummies Questions & Answers

Cut fields between delimiters

I'm having bother getting both lines contained in a file to output as the same value. A simple example: john:123456:123:456:doe john:123456:123:doe cut -d: -f1,4 input file john:456 john:doe ^ first line should be same as second. trick one for me, i know why it's because of the... (4 Replies)
Discussion started by: landofus
4 Replies

5. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

6. Shell Programming and Scripting

cut -- line with no delimiters

I just discovered, to my dismay, the following part of the cut man page: -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified The -s option toggles the printing of lines with no delimiters. In most... (3 Replies)
Discussion started by: chlorine
3 Replies

7. Shell Programming and Scripting

Cut based on Two Delimiters at one go

Hi I wanted to cut the feilds comming after % and After $ at one go can we do some thing like this cut -f 2 -d "%|$" (But it doesnot work) Input File BWPG %TCPRP1 $SCSPR000 BWPH %TCPRP1 $SCSPR003 BWPI %TRTYUP ResourceDescription="IMPRIMANTE " $BWOPTY BWPJ %ZOMBIE ... (4 Replies)
Discussion started by: pbsrinivas
4 Replies

Featured Tech Videos