Operating System - HP-UX
1753783 Members
6887 Online
108799 Solutions
New Discussion юеВ

Re: cut command - specifying multiple delimiters

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

cut command - specifying multiple delimiters

Hi,

I'm attempting to use the "cut" command to extract elements from the filename having the form shown below:
A20051109.0215-0230_SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002_statsfile.xml

I'd want to extract the following elements:
1) A20051109
2) 0215
3) 0230
4)SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002
5) statsfile
6) xml

I've tried using "cut" in the following way, but it did not do the trick.

ls A20051109.0215-0230_SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002_statsfile.xml |cut -f1-6 -d".,-"
cut: invalid delimiter
bash-3.00$

I've also tried using single quotes as in '.,-,_' to the -d option but it produced the same error shown above.

How do I specify multiple delimiters to the "cut" -d option?

Also, how do I assign each element obtained through "cut" into their respective variable names in a script?

Could anyone help show me how it's done?

Thanks

27 REPLIES 27
James R. Ferguson
Acclaimed Contributor

Re: cut command - specifying multiple delimiters

Hi Danny:

A llok at the manpages for 'cut' will show you that the delimiter ('-d') switch supports only a simple character argument.

You asked "How do I specify multiple delimiters to the "cut" -d option?". Consider :

# echo "a b|c d"|cut -d " " -f2|cut -d"|" -f2

...this extracts the "c" from the input string. We need to do two 'cut's each with a different delimiter.

To assign the extracted value to a variable, simply do:

# VAR=`echo "a b|c d"|cut -d " " -f2|cut -d"|" -f2`

# echo ${VAR} #...to see the value.

Regards!

...JRF...



jon2
Advisor

Re: cut command - specifying multiple delimiters

Change the delimeters to all be the same before using cut.... I chose spaces then awk will delimit on space without setting field seperator - so no cut required". Read directly in to variables.

e.g.
echo "XX.yy,zz-aa,bb" |sed
"s/\,/\ /g
s/-/ /g" |awk '{print $1, $2, $3}' |read VAR1 VAR2 VAR3

If you like you can use this with cut as before but only need to use one type.
harry d brown jr
Honored Contributor

Re: cut command - specifying multiple delimiters

With a decently complicated sed you can parse and reconstruct, replacing the delimiters with spaces:

sed "s/^\(.*\)\.\(.*\)\-\(.*\)\_\(.*\)\_\(.*\)\_\(.*\)\.\(.*\)/\1 \2 \3 \4_\5 \6 \7/"

thus

echo A20051109.0215-0230_SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002_statsfile.xml | sed "s/^\(.*\)\.\(.*\)\-\(.*\)\_\(.*\)\_\(.*\)\_\(.*\)\.\(.*\)/\1 \2 \3 \4_\5 \6 \7/"

produces

A20051109 0215 0230 SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002 statsfile xml

Note that I had to reinsert the underscore (_) in the "SubNetwork" string.

live free or die
harry d brown jr

Live Free or Die
Hein van den Heuvel
Honored Contributor

Re: cut command - specifying multiple delimiters

Your problem will be the ","
For some parts of the string this is apparently desired to be a seperator, but not for all. You'll need (perl) logic to fix that.

Just doing the split is easy in perl:

#cat file1.tmp
A20051109.0215-0230_SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002_statsfile.xml
#
#perl -ne "foreach $x (split /[.,\-_]/) {print \"$x\n\"}" file1.tmp
A20051109
0215
0230
SubNetwork=ONRM
RootMo
SubNetwork=SNTDCAUJRNC002
MeContext=SNTDCAUJRNC002
statsfile
xml

btw... you can pass the the seperators on the command line with -F, anf perl (like awk) has build-ins for auto split fields.


hth,
Hein.
jon2
Advisor

Re: cut command - specifying multiple delimiters

Do not forget that where the delimiter is desired to be a "_" and has been made a space it can be reconstructed with the variables when they are used. (no perl etc required).

e.g echo "${VAR2}_${VAR3}....
harry d brown jr
Honored Contributor

Re: cut command - specifying multiple delimiters

To assign the "fields" into script variables you could do this:

create a script:

somescript.ksh
#!/usr/bin/ksh
#
while read aline
do
echo $aline | sed "s/^\(.*\)\.\(.*\)\-\(.*\)\_\(.*\)\_\(.*\)\_\(.*\)\.\(.*\)/\1 \2 \3 \4_\5 \6 \7/"|read var1 var2 var3 var4 var5 var6
echo var1=$var1
echo var2=$var2
echo var3=$var3
echo var4=$var4
echo var5=$var5
echo var6=$var6
done

chmod a+x somescript.ksh

cat yourdata | ./somescript.ksh

live free or die
harry d brown jr
Live Free or Die
jon2
Advisor

Re: cut command - specifying multiple delimiters

Harry,
Do you mean like in my sample at 11.26 am ?
Peter Nikitka
Honored Contributor
Solution

Re: cut command - specifying multiple delimiters

Hi,

if you like a solution using shell features only (I did this in ksh):

name='A20051109.0215-0230_SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC
002,MeContext=SNTDCAUJRNC002_statsfile.xml'
p1=${name%%.*}
remain=${name#$p1.}
p2=${remain%%-*}
remain=${remain#$p2-}
p3=${remain%%_*}
remain=${remain#${p3}_}
p4=${remain%_*}
remain=${remain#${p4}_}
p5=${remain%.*}
p6=${remain#*.}

print $name;print $p1;print $p2;print $p3;print $p4;print $p5;print $p6

A20051109.0215-0230_SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002_statsfile.xml
A20051109
0215
0230
SubNetwork=ONRM_RootMo,SubNetwork=SNTDCAUJRNC002,MeContext=SNTDCAUJRNC002
statsfile
xml

The variables p1...p6 will contain the requested values (watch for the correct string operators!).

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
harry d brown jr
Honored Contributor

Re: cut command - specifying multiple delimiters

jon2,

close if you consider the use of the "read" statement, but the examples I provided do the following:
- without the use of awk or perl (not that there is anything wrong with them)
- use of pattern matching
- providing all SIX desired output fields
- example of putting the code in a loop

BTW, you wouldn't be claiming plagurisim, would you?

live free or die
harry d brown jr
Live Free or Die