The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SSH problems ysk UNIX for Advanced & Expert Users 1 07-19-2007 05:16 AM
Problems AIX and SAN. fjgonzalez AIX 4 07-18-2007 05:15 PM
Problems with Last By_Jam UNIX for Advanced & Expert Users 3 09-29-2006 03:12 AM
Few problems vivekshankar UNIX for Dummies Questions & Answers 3 05-21-2005 12:26 PM
'make' problems (compliation problems?) xyyz UNIX for Advanced & Expert Users 5 11-05-2001 10:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2008
scorpio scorpio is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 41
Problems with cut

Hi all,


0680046755000011 3040249 3005930 60180GPRS4Samsung_SGH_Z500


This is the format of my log file and parameters for log file are like

• 10 first characters: MSISDN
• 6 next characters: Hour
• 18 next characters: ID SA Source
• 18 next characters: ID SA Destination


I have 11 whitespaces in between my first and second colums.I need to cut fitst 10 char next 6 char and next 18 char(including white spaces) . and i tried follwing in my code

MSISDN=`echo $data | cut -c1-10`
HOUR=`echo $data | cut -c11-16`
ID_SA_SOURCE=`echo $data | cut -c17-35`
ID_SA_DEST=`echo $data | cut -c36-54`


But becos of whitespaces it is not cutting properly.. can any one suggest alternatives

Thnks
  #2 (permalink)  
Old 04-28-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by scorpio View Post
Hi all,


0680046755000011 3040249 3005930 60180GPRS4Samsung_SGH_Z500


This is the format of my log file and parameters for log file are like

• 10 first characters: MSISDN
• 6 next characters: Hour
• 18 next characters: ID SA Source
• 18 next characters: ID SA Destination


I have 11 whitespaces in between my first and second colums.I need to cut fitst 10 char next 6 char and next 18 char(including white spaces) . and i tried follwing in my code

MSISDN=`echo $data | cut -c1-10`
HOUR=`echo $data | cut -c11-16`
ID_SA_SOURCE=`echo $data | cut -c17-35`
ID_SA_DEST=`echo $data | cut -c36-54`


But becos of whitespaces it is not cutting properly.. can any one suggest alternatives

Thnks
First reduce the spaces in between the log file data with the command tr
Code:
HOUR=`echo $data | tr -s " " |cut -c11-16`
give a try to this one.
  #3 (permalink)  
Old 04-28-2008
scorpio scorpio is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 41
Hi
Thanks for the reply

But i cant trim my whitespaces from log files . i need to take that also..
becos parameters are set like
• 10 first characters: MSISDN
• 6 next characters: Hour
• 18 next characters: ID SA Source
• 18 next characters: ID SA Destination
This characters includes white spaces also
  #4 (permalink)  
Old 04-28-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by scorpio View Post
Hi
Thanks for the reply

But i cant trim my whitespaces from log files . i need to take that also..
becos parameters are set like
• 10 first characters: MSISDN
• 6 next characters: Hour
• 18 next characters: ID SA Source
• 18 next characters: ID SA Destination
This characters includes white spaces also
Code:
id=`echo 0680046755000011 3040249 3005930 60180GPRS4Samsung_SGH_Z500 | tr -s " " |cut -c17-35`
output is --- 3040249 3005930 60
  #5 (permalink)  
Old 04-28-2008
scorpio scorpio is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 41
Hi ,

Actually the log file is like 0608166896000001 in my first column 11 whitespaces and 3001339 in my second column( 3001339) then 11 whitespaces and
3204235 in my third coumn .
...

But while reading line by line using following code

while read data
do
echo data $data
ID_SA_SOURCE=`echo $data | cut -c17-35`
echo ID_SA_SOURCE $ID_SA_SOURCE
done < $TRACKING_LOGDIR/$listdata


I am not getting the 11 white spaces in "data" variable instead of i am getting only one space..i want 11whtespaces first and 3001339 value in my ID_SA_SOURCE variable....

CAn you pleasse check this
  #6 (permalink)  
Old 04-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You need to properly quote your variables when you echo them, otherwise the shell will trim whitespace. I'd advise against using echo at all, though.

But here's how to quote properly. To be a little bit on the safe side, I also zap IFS; I don't believe it's strictly necessary here, but it's a technique you should be aware of.

Code:
OLDIFS=$IFS
IFS='
' # just a newline, in single quotes
while read data
do
  MSISDN="`echo "$data" | cut -c1-10`"
  HOUR="`echo "$data" | cut -c11-16`"
  ID_SA_SOURCE="`echo "$data" | cut -c17-35`"
  ID_SA_DEST="`echo "$data" | cut -c36-54`"
done < $TRACKING_LOGDIR/$listdata
IFS=$OLDIFS
Having that out of the way, how about something like this?

Code:
awk '{ OFS=":"; print substr($0, 1, 10), substr($0, 11, 16),
    substr($0, 17, 35), substr($0, 17, 35) }' $TRACKING_LOGDIR/$listdata |
while IFS=: read MSISDN HOUR ID_SA_SOURCE ID_SA_DEST; do
  echo "'$HOUR': All your '$ID_SA_SOURCE' are '$ID_SA_DEST' to '$MSISDN'"
done
  #7 (permalink)  
Old 04-28-2008
scorpio scorpio is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 41
Hi All,

I tried the first its working Thanks to all
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0