Sponsored Content
Full Discussion: String Manipulation Help
Top Forums Shell Programming and Scripting String Manipulation Help Post 302174012 by Perderabo on Sunday 9th of March 2008 04:35:31 PM
Old 03-09-2008
I edited my earlier post to fix a couple of letters that needed swapping. At this point you are asking a lot for one line of code! But...
Code:
#! /usr/bin/ksh

L="abcdefghijklmnopqrstuvwxyz"
U="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo 'i%%%%long $$$john&&&&&&  ((())) silver' |\
sed 's/[^a-zA-Z]/ /g;s/  */ /g'| awk -v u=$U -v l=$L '{for (i=1;i<=NF;i++)printf "%s%s ",substr(u,index(l,substr($i,1,1)),1),substr($i,2);print "";}'

seems to be working. I retreated to my preferred syntax where the long constants are out of the awk script.You can put them back if you really want to keep it to one line. Test run:
Code:
$ ./awkcap
I Long John Silver
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Manipulation

Hi, Suppose I have the following text in a file. ORA-00942: table or view does not exist ORA-01555: snapshot too old: rollback segment number string with name "string" too small Is there any way I can list all the text that starts only with 'ORA-'? Or there any grep command that can... (7 Replies)
Discussion started by: kakashi_jet
7 Replies

2. UNIX for Dummies Questions & Answers

string manipulation

Hi, I have a file with rows of text like so : E100005568374098100000015667 D100005568374032000000112682 H100005228374060800000002430 I need to grab just the last digits(bolded) of each line without the proceeding text/numbers. Thanks (5 Replies)
Discussion started by: james6
5 Replies

3. Shell Programming and Scripting

String manipulation

Hi, i am just gettin exposed to UNIX. Could anyone of u help me out with dis problem..? i have a variable 'act' which has the value as follows, echo $act gives -0- -0- -----0---- 2008-06-04 -0- -0- echo "$act" | awk '{print ($act)}' gives, -0- -0- -----0---- 2008-06-04 -0- -0- I... (2 Replies)
Discussion started by: jerrynimrod
2 Replies

4. Shell Programming and Scripting

string manipulation

i have a file that contains a pattern like this: ajay 1234 newyork available kumar 2345 denver singh 2345 newyork ajay 3456 denver kumar 3456 newyork singh 3456 delhi available ajay 4567 miami kumar 4567 miami singh 4567 delhi i want to search for each line... (5 Replies)
Discussion started by: ajay41aj
5 Replies

5. Shell Programming and Scripting

I need help with string manipulation

First of all I am VERY new to this so bare with me and try and explain everything even if it seems simple. Basically I want to read a line of text from a html file. See if the line of text has a certain string in it. copy an unknown number of characters (the last 4 characters wiil be ".jpg" the... (1 Reply)
Discussion started by: c3lica
1 Replies

6. Shell Programming and Scripting

String Manipulation

Hi, I have a file in the following format 123|shanwer|15DEC2010|bgbh|okok|16JAN3000|okok| I want the following to be in following format 123|shanwer|12\15\2010|bgbh|okok|01\16\3000|okok| SED/PERL/AWK Gurus could you please help me with this? Thanks Shankar (8 Replies)
Discussion started by: Shan2210
8 Replies

7. Shell Programming and Scripting

String manipulation

Hi All, Pls help me out on the below, 05 LAMSZ201-ZM-MEMO2-DATE02-5 PIC X(10). 05 LAMSZ201-ZM-MEMO2-AMT02-5 PIC S9(13)V99. 05 LAMSZ201-ZM-MEMO2-TYPE02-6 PIC XXX. 05 LAMSZ201-ZM-MEMO2-DATE02-6 PIC X(10). 05 ... (2 Replies)
Discussion started by: baskivs
2 Replies

8. Shell Programming and Scripting

String manipulation

Hello Could you help with small script: How to split string X1 into 3 string String X1 can have 1 or many strings X1='A1:B1:C1:D1:A2:B2:C2:D2:A3:B3:C3:D3' This is output which I want to have: Z1='A1:B1:C1:D1' Z2='A2:B2:C2:D2' Z3='A3:B3:C3:D3' (5 Replies)
Discussion started by: vikus
5 Replies

9. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

10. Shell Programming and Scripting

String Manipulation

I'm making a little game in Perl, and I am trying to remove the first instance of a character in an arbitrary string. For example, if the string is "cupcakes"and the user enters another string that contains letters from "cupcake" e.g: "sake"the original string will now look like this (below)... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies
Template::Plugin::String(3pm)				User Contributed Perl Documentation			     Template::Plugin::String(3pm)

NAME
Template::Plugin::String - Object oriented interface for string manipulation SYNOPSIS
# create String objects via USE directive [% USE String %] [% USE String 'initial text' %] [% USE String text => 'initial text' %] # or from an existing String via new() [% newstring = String.new %] [% newstring = String.new('newstring text') %] [% newstring = String.new( text => 'newstring text' ) %] # or from an existing String via copy() [% newstring = String.copy %] # append text to string [% String.append('text to append') %] # format left, right or center/centre padded [% String.left(20) %] [% String.right(20) %] [% String.center(20) %] # American spelling [% String.centre(20) %] # European spelling # and various other methods... DESCRIPTION
This module implements a "String" class for doing stringy things to text in an object-oriented way. You can create a "String" object via the "USE" directive, adding any initial text value as an argument or as the named parameter "text". [% USE String %] [% USE String 'initial text' %] [% USE String text='initial text' %] The object created will be referenced as "String" by default, but you can provide a different variable name for the object to be assigned to: [% USE greeting = String 'Hello World' %] Once you've got a "String" object, you can use it as a prototype to create other "String" objects with the "new()" method. [% USE String %] [% greeting = String.new('Hello World') %] The "new()" method also accepts an initial text string as an argument or the named parameter "text". [% greeting = String.new( text => 'Hello World' ) %] You can also call "copy()" to create a new "String" as a copy of the original. [% greet2 = greeting.copy %] The "String" object has a "text()" method to return the content of the string. [% greeting.text %] However, it is sufficient to simply print the string and let the overloaded stringification operator call the "text()" method automatically for you. [% greeting %] Thus, you can treat "String" objects pretty much like any regular piece of text, interpolating it into other strings, for example: [% msg = "It printed '$greeting' and then dumped core " %] You also have the benefit of numerous other methods for manipulating the string. [% msg.append("PS Don't eat the yellow snow") %] Note that all methods operate on and mutate the contents of the string itself. If you want to operate on a copy of the string then simply take a copy first: [% msg.copy.append("PS Don't eat the yellow snow") %] These methods return a reference to the "String" object itself. This allows you to chain multiple methods together. [% msg.copy.append('foo').right(72) %] It also means that in the above examples, the "String" is returned which causes the "text()" method to be called, which results in the new value of the string being printed. To suppress printing of the string, you can use the "CALL" directive. [% foo = String.new('foo') %] [% foo.append('bar') %] # prints "foobar" [% CALL foo.append('bar') %] # nothing CONSTRUCTOR METHODS
These methods are used to create new "String" objects. new() Creates a new string using an initial value passed as a positional argument or the named parameter "text". [% USE String %] [% msg = String.new('Hello World') %] [% msg = String.new( text => 'Hello World' ) %] copy() Creates a new "String" object which contains a copy of the original string. [% msg2 = msg.copy %] INSPECTOR METHODS
These methods are used to examine the string. text() Returns the internal text value of the string. The stringification operator is overloaded to call this method. Thus the following are equivalent: [% msg.text %] [% msg %] length() Returns the length of the string. [% USE String("foo") %] [% String.length %] # => 3 search($pattern) Searches the string for the regular expression specified in $pattern returning true if found or false otherwise. [% item = String.new('foo bar baz wiz waz woz') %] [% item.search('wiz') ? 'WIZZY! :-)' : 'not wizzy :-(' %] split($pattern, $limit) Splits the string based on the delimiter $pattern and optional $limit. Delegates to Perl's internal "split()" so the parameters are exactly the same. [% FOREACH item.split %] ... [% END %] [% FOREACH item.split('baz|waz') %] ... [% END %] MUTATOR METHODS
These methods modify the internal value of the string. For example: [% USE str=String('foobar') %] [% str.append('.html') %] # str => 'foobar.html' The value of "str" is now '"foobar.html"'. If you don't want to modify the string then simply take a copy first. [% str.copy.append('.html') %] These methods all return a reference to the "String" object itself. This has two important benefits. The first is that when used as above, the "String" object '"str"' returned by the "append()" method will be stringified with a call to its "text()" method. This will return the newly modified string content. In other words, a directive like: [% str.append('.html') %] will update the string and also print the new value. If you just want to update the string but not print the new value then use "CALL". [% CALL str.append('.html') %] The other benefit of these methods returning a reference to the "String" is that you can chain as many different method calls together as you like. For example: [% String.append('.html').trim.format(href) %] Here are the methods: push($suffix, ...) / append($suffix, ...) Appends all arguments to the end of the string. The "append()" method is provided as an alias for "push()". [% msg.push('foo', 'bar') %] [% msg.append('foo', 'bar') %] pop($suffix) Removes the suffix passed as an argument from the end of the String. [% USE String 'foo bar' %] [% String.pop(' bar') %] # => 'foo' unshift($prefix, ...) / prepend($prefix, ...) Prepends all arguments to the beginning of the string. The "prepend()" method is provided as an alias for "unshift()". [% msg.unshift('foo ', 'bar ') %] [% msg.prepend('foo ', 'bar ') %] shift($prefix) Removes the prefix passed as an argument from the start of the String. [% USE String 'foo bar' %] [% String.shift('foo ') %] # => 'bar' left($pad) If the length of the string is less than $pad then the string is left formatted and padded with spaces to $pad length. [% msg.left(20) %] right($pad) As per left() but right padding the "String" to a length of $pad. [% msg.right(20) %] center($pad) / centre($pad) As per left() and right() but formatting the "String" to be centered within a space padded string of length $pad. The "centre()" method is provided as an alias for "center()". [% msg.center(20) %] # American spelling [% msg.centre(20) %] # European spelling format($format) Apply a format in the style of "sprintf()" to the string. [% USE String("world") %] [% String.format("Hello %s ") %] # => "Hello World " upper() Converts the string to upper case. [% USE String("foo") %] [% String.upper %] # => 'FOO' lower() Converts the string to lower case [% USE String("FOO") %] [% String.lower %] # => 'foo' capital() Converts the first character of the string to upper case. [% USE String("foo") %] [% String.capital %] # => 'Foo' The remainder of the string is left untouched. To force the string to be all lower case with only the first letter capitalised, you can do something like this: [% USE String("FOO") %] [% String.lower.capital %] # => 'Foo' chop() Removes the last character from the string. [% USE String("foop") %] [% String.chop %] # => 'foo' chomp() Removes the trailing newline from the string. [% USE String("foo ") %] [% String.chomp %] # => 'foo' trim() Removes all leading and trailing whitespace from the string [% USE String(" foo ") %] [% String.trim %] # => 'foo' collapse() Removes all leading and trailing whitespace and collapses any sequences of multiple whitespace to a single space. [% USE String(" foo bar ") %] [% String.collapse %] # => "foo bar" truncate($length, $suffix) Truncates the string to $length characters. [% USE String('long string') %] [% String.truncate(4) %] # => 'long' If $suffix is specified then it will be appended to the truncated string. In this case, the string will be further shortened by the length of the suffix to ensure that the newly constructed string complete with suffix is exactly $length characters long. [% USE msg = String('Hello World') %] [% msg.truncate(8, '...') %] # => 'Hello...' replace($search, $replace) Replaces all occurences of $search in the string with $replace. [% USE String('foo bar foo baz') %] [% String.replace('foo', 'wiz') %] # => 'wiz bar wiz baz' remove($search) Remove all occurences of $search in the string. [% USE String('foo bar foo baz') %] [% String.remove('foo ') %] # => 'bar baz' repeat($count) Repeats the string $count times. [% USE String('foo ') %] [% String.repeat(3) %] # => 'foo foo foo ' AUTHOR
Andy Wardley <abw@wardley.org> <http://wardley.org/> COPYRIGHT
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template::Plugin perl v5.14.2 2011-12-20 Template::Plugin::String(3pm)
All times are GMT -4. The time now is 10:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy