The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
charecter or number ganapati UNIX for Dummies Questions & Answers 1 01-02-2008 05:44 AM
Cutting a tab delimiter file vinod.thayil Shell Programming and Scripting 4 11-30-2007 12:38 PM
Cutting Columns and Moving in to a file Serious Sam Shell Programming and Scripting 3 10-22-2007 03:56 AM
Comapring files charecter by charecter using AWK/Shell Script evvander Shell Programming and Scripting 1 09-12-2007 02:44 AM
cut First charecter in any string rinku Shell Programming and Scripting 6 05-29-2007 04:23 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-12-2006
Registered User
 

Join Date: Aug 2006
Posts: 45
cutting columns if delimiter has more than one charecter

Hi,

My file looks like

abc$%sdfhs$%sdf$%sdfaf$%

here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way

thanks and regards
mahabunta
Reply With Quote
Forum Sponsor
  #2  
Old 09-12-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
echo "abc$%sdfhs$%sdf$%sdfaf$%" | tr -s '$%' '  ' | cut -d" " -f3
I think you can not use two char as delimiter in cut. What you can do is translate that two char to single char and then cut it
Reply With Quote
  #3  
Old 09-12-2006
Registered User
 

Join Date: Aug 2006
Posts: 45
thanks for reply.....

but is there is any other wat wirhout using tr command.......may be in AWk or any other method...
Reply With Quote
  #4  
Old 09-12-2006
Registered User
 

Join Date: Aug 2006
Posts: 45
hi,

I guess it can be done using awk scripting....

say my file is delimited by ~%%~ and i need 6th field then following command will give me correct result.......

cat file | awk -F "~%%~" '{print $6}'

sandy
Reply With Quote
  #5  
Old 09-12-2006
Registered User
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 401
Hmm, that didn't work in my test. Passing more than one character to -F just returned the entire line as argument $1.

Code:
awk -F"\$%" '{print $1}' file

abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
abc$%sdfhs$%sdf$%sdfaf$%
You could use sed to change it:

Code:
sed -e "s/\$%/:/g" file | cut -f3 -d:

sdf
sdf
sdf
sdf
sdf
sdf
Or use tr as noted earlier:

Code:
cat files | tr -s "$%" ":" | cut -f3 -d:

sdf
sdf
sdf
sdf
sdf
sdf
It appears the $% is causing issues with the -F command. If I replace the $% with ", ", I can run

Code:
awk -F", " '{print $2}' files2

sdfhs
sdfhs
sdfhs
sdfhs
sdfhs
sdfhs
And it works. I also tried the $% delimiter in an awk script and it didn't work either. I tried escaping one and both characters without success.

Carl
Reply With Quote
  #6  
Old 09-12-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Escape the backslash, like this...
Code:
$ awk -F '\\$%' '{print $1}' file
abc
abc
abc
Reply With Quote
  #7  
Old 09-12-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by mahabunta
Hi,

My file looks like

abc$%sdfhs$%sdf$%sdfaf$%

here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way

thanks and regards
mahabunta
Just an alternative, in Python:
Code:
>>> line = "abc$%sdfhs$%sdf$%sdfaf$%"
>>> secondfield = line.split("$%")[1] #element 1 is second field
>>> print secondfield
sdfhs
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0