Sponsored Content
Top Forums UNIX for Dummies Questions & Answers how to get first two characters from a word Post 302149563 by broli on Thursday 6th of December 2007 02:32:47 PM
Old 12-06-2007
you can use cut
echo $somevar | cut -c 2
i dont know if its the best option, but is the most straight forward
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Word Count without any characters but the Output String

Hi all, I am logging any access to a server, and i wanted to write a script which tells me how much entries there are. The Problem is that "wc -l log" outputs the correct number of lines but with the name of the file attached. is there any nice possibility to solve this that i ONLY... (3 Replies)
Discussion started by: JP_II
3 Replies

2. Shell Programming and Scripting

Print the characters in a word

Hi, How can I split the characters in a word? For Eg: If my input is: command my output should be: c o m m a n d Please help me in doing it so. (5 Replies)
Discussion started by: chella
5 Replies

3. Shell Programming and Scripting

deleting last characters of a word

Hi All is there a way to delete last n characters from a word like say i have employee_new i want to delete _new. and just get only employee I want this in AIX Shell scripting Thanks (3 Replies)
Discussion started by: rajaryan4545
3 Replies

4. Shell Programming and Scripting

Help to fetch first two characters from a word in perl

Hi All, I have a word "DE_PR_Package__Basic" , i need to check if the first two characters of this words is DE or something else using perl script. Can anyone pls let me know how to proceed? Thanks in advance. Giri! (3 Replies)
Discussion started by: girish.raos
3 Replies

5. Shell Programming and Scripting

Adding Characters to a Word List

If I had a word list with a large amount of words in it, how would I (using a unix command) add, say, 123 to the end of each word? EDIT: The word list is stored in a large text file. I need a command that applies the ending to each word in the file and saves the result in a new text file. (7 Replies)
Discussion started by: evillion
7 Replies

6. UNIX for Dummies Questions & Answers

find a word in a file, plus the next 6 characters?

I plan to use sed in a script to replace a string. My problem is the last 6 characters of the word to be replaced can be different each time, plus it's not always in the same spot on the line so I can't use cut or nawk to get the field. So I am looking for a way to find a certain word in a file,... (6 Replies)
Discussion started by: mikayla73
6 Replies

7. Shell Programming and Scripting

Find word in file then get following characters

Hello, I have several xml files from which I want to find and return a particular string I want to locate the InId="00000008". Now that is inlcuded within a tag and ofcourse the number is different every time this is what I came up with given that after greping the line that contains the... (6 Replies)
Discussion started by: TasosARISFC
6 Replies

8. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

9. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

10. Shell Programming and Scripting

Grep a word that contains minimum 5 or 6 same characters

Hi, I am looking for a solution to grep for minimum 5 or 6 characters in a file, otherwise ignore. Example 1121221222 2212121211 1221122122 2121222222 2222112222 1211221121 So it greps 5 X 1 or 6 X 1 2212121211 1211221121 Thanks for you help (6 Replies)
Discussion started by: stinkefisch
6 Replies
SUBSTR(3)								 1								 SUBSTR(3)

substr - Return part of a string

SYNOPSIS
string substr (string $string, int $start, [int $length]) DESCRIPTION
Returns the portion of $string specified by the $start and $length parameters. PARAMETERS
o $string - The input string. Must be one character or longer. o $start - If $start is non-negative, the returned string will start at the $start'th position in $string, counting from zero. For instance, in the string ' abcdef', the character at position 0 is ' a', the character at position 2 is ' c', and so forth. If $start is negative, the returned string will start at the $start'th character from the end of $string. If $string is less than or equal to $start characters long, FALSE will be returned. Example #1 Using a negative $start <?php $rest = substr("abcdef", -1); // returns "f" $rest = substr("abcdef", -2); // returns "ef" $rest = substr("abcdef", -3, 1); // returns "d" ?> o $length - If $length is given and is positive, the string returned will contain at most $length characters beginning from $start (depend- ing on the length of $string). If $length is given and is negative, then that many characters will be omitted from the end of $string (after the start position has been calculated when a $start is negative). If $start denotes the position of this trunca- tion or beyond, false will be returned. If $length is given and is 0, FALSE or NULL, an empty string will be returned. If $length is omitted, the substring starting from $start until the end of the string will be returned. Example #2 Using a negative $length <?php $rest = substr("abcdef", 0, -1); // returns "abcde" $rest = substr("abcdef", 2, -1); // returns "cde" $rest = substr("abcdef", 4, -4); // returns false $rest = substr("abcdef", -3, -1); // returns "de" ?> RETURN VALUES
Returns the extracted part of $string; or FALSE on failure, or an empty string. CHANGELOG
+--------------+---------------------------------------------------+ | Version | | | | | | | Description | | | | +--------------+---------------------------------------------------+ |5.2.2 - 5.2.6 | | | | | | | If the $start parameter indicates the position | | | of a negative truncation or beyond, false is | | | returned. Other versions get the string from | | | start. | | | | +--------------+---------------------------------------------------+ EXAMPLES
Example #3 Basic substr(3) usage <?php echo substr('abcdef', 1); // bcdef echo substr('abcdef', 1, 3); // bcd echo substr('abcdef', 0, 4); // abcd echo substr('abcdef', 0, 8); // abcdef echo substr('abcdef', -1, 1); // f // Accessing single characters in a string // can also be achieved using "square brackets" $string = 'abcdef'; echo $string[0]; // a echo $string[3]; // d echo $string[strlen($string)-1]; // f ?> Example #4 substr(3) casting behaviour <?php class apple { public function __toString() { return "green"; } } echo "1) ".var_export(substr("pear", 0, 2), true).PHP_EOL; echo "2) ".var_export(substr(54321, 0, 2), true).PHP_EOL; echo "3) ".var_export(substr(new apple(), 0, 2), true).PHP_EOL; echo "4) ".var_export(substr(true, 0, 1), true).PHP_EOL; echo "5) ".var_export(substr(false, 0, 1), true).PHP_EOL; echo "6) ".var_export(substr("", 0, 1), true).PHP_EOL; echo "7) ".var_export(substr(1.2e3, 0, 4), true).PHP_EOL; ?> The above example will output: 1) 'pe' 2) '54' 3) 'gr' 4) '1' 5) false 6) false 7) '1200' ERRORS
/EXCEPTIONS Returns FALSE on error. Example #5 <?php var_dump(substr('a', 1)); // bool(false) ?> SEE ALSO
strrchr(3), substr_replace(3), preg_match(3), trim(3), mb_substr(3), wordwrap(3), String access and modification by character. PHP Documentation Group SUBSTR(3)
All times are GMT -4. The time now is 12:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy