- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: cut command - specifying multiple delimiters
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2006 10:47 PM
тАО04-08-2006 10:47 PM
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
Solved! Go to Solution.
- Tags:
- cut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2006 01:51 AM
тАО04-09-2006 01:51 AM
Re: cut command - specifying multiple delimiters
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2006 11:26 PM
тАО04-09-2006 11:26 PM
Re: cut command - specifying multiple delimiters
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.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2006 11:45 PM
тАО04-09-2006 11:45 PM
Re: cut command - specifying multiple delimiters
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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2006 11:58 PM
тАО04-09-2006 11:58 PM
Re: cut command - specifying multiple delimiters
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 12:12 AM
тАО04-10-2006 12:12 AM
Re: cut command - specifying multiple delimiters
e.g echo "${VAR2}_${VAR3}....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 12:50 AM
тАО04-10-2006 12:50 AM
Re: cut command - specifying multiple delimiters
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 01:05 AM
тАО04-10-2006 01:05 AM
Re: cut command - specifying multiple delimiters
Do you mean like in my sample at 11.26 am ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 01:11 AM
тАО04-10-2006 01:11 AM
Solutionif 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2006 01:24 AM
тАО04-10-2006 01:24 AM
Re: cut command - specifying multiple delimiters
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