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
problem in arithmetic operations ithirak17 Shell Programming and Scripting 3 02-06-2007 06:46 AM
AlphaNumeric String Operations lakshmikanth UNIX for Dummies Questions & Answers 3 01-05-2007 06:55 AM
File operations monks UNIX for Dummies Questions & Answers 2 04-26-2006 07:26 AM
File operations chiragmistry21 Shell Programming and Scripting 2 03-27-2006 05:00 PM
mathematics operations in unix cesar720213 UNIX for Dummies Questions & Answers 2 11-22-2001 11:24 AM

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-19-2006
Rohini Vijay Rohini Vijay is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 16
String Operations

Hi All,

Query 1 :
I want to know how we can get a count of multipe occurrences of a particular expression in another string.

For Eg. If my string is " 12" and i need to count the number of spaces preceeding 12

Query 2 :

Also want to know how we can change the alignment of a string from left justified to right justified in Unix.

For Eg. " 12" want to change it to "12 ".

Is there any command in unix for this. ??
  #2 (permalink)  
Old 04-19-2006
kenisand kenisand is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 8
Please try this for u r query2 i.e. left alignment
awk '{printf("%'filelength's\n",$0)}' S_file > D_file

filelength:= No of spaces u need for left alignment
S_file := Source filename
D_file:= Detination filename
  #3 (permalink)  
Old 04-19-2006
Raom Raom is offline
Registered User
  
 

Join Date: Sep 2005
Location: india
Posts: 79
for query one

echo " 12" | tr -cd '\040'| fold -1 | wc -l

to align to left

echo " 12" | tr -d '\040'

Last edited by Raom; 04-21-2006 at 07:13 AM..
  #4 (permalink)  
Old 04-19-2006
Rohini Vijay Rohini Vijay is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 16
Hi kenisand ,
Actually my problem is that i have column in a fixed length file that is right alingned. I need to change that column to make it right aligned.

The problem is that the length of the column is 5, and it can hold a max of 5 characters. So i do not know the number of spaces that are preceding a valid character.
For Eg : Records in the file are as follows :
Cl1 Cl2 Cl3
Rec 1 :ABC 3DEF
Rec 2 :EFG 34HIJ

In the above file, has 3 cols
first record is : Col1 "ABC", Col2:" 3" and col3:"DEF"
second record :Col1 "EFG", Col2:" 34" and col3:"HIJ"

Now my requirement is to change the right alignement of the 2 nd col in the file to left alignement.

I want the output to be as follows :
Cl1 Cl2 Cl3
Rec 1 :ABC3 DEF
Rec 2 :EFG34 HIJ

If i can find the number of spaces preceding the valid character then i can use the solution u hav suggested, but this count can be variable and that is the problem.
  #5 (permalink)  
Old 04-19-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Under man ksh look into typeset features. It might be of some help.
  #6 (permalink)  
Old 04-19-2006
Rohini Vijay Rohini Vijay is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 16
Hey Raom,

Thanks a lot for that answer to my Query 1. That is exactly what i was looking for..

-Regards
Rohini
  #7 (permalink)  
Old 04-19-2006
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Quote:
Originally Posted by Raom
for query one

echo " 12" | tr -cd '\040'| fold -1 | wc -l
Nice , anyway u can also use:
Code:
echo " 12" | tr -cd ' '|awk '{print length}'
Cheers
Sponsored Links
Closed Thread

Bookmarks

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 11:11 PM.


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