Sponsored Content
Top Forums Shell Programming and Scripting No. of underscores in a file name Post 302853195 by alister on Friday 13th of September 2013 11:29:40 AM
Old 09-13-2013
Quote:
Originally Posted by wisecracker
Code:
text="a_b_c"; count=0; for n in $( seq 0 1 ${#text} ); do if [ "${text:$n:1}" == "_" ]; then count=$[ ( $count + 1 ) ]; fi; done; echo""; echo "Underscores = $count"

If your shell supports substring expansion (${text:$n:1}), then you can probably replace all of that with a much simpler, pattern substitution alternative:
Code:
u=${text//[!_]/}
echo Underscores = ${#u}

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

change spaces to underscores script !!!

Hi everybody! Im not good in scripting and I need a script to take all the files with spaces in their names and change it to underscores. alice cooper.mp3 >> alice_cooper.mp3 Thanks in advance. (2 Replies)
Discussion started by: piltrafa
2 Replies

2. Shell Programming and Scripting

Scripting to convert underscores to spaces

Greetings, I have a bunch of music files that I want to strip the underscores out, and leave only spaces. All that I've found on the web is how to add underscores to files that have spaces, and reversing the "tr" command does not make a difference. Here is how to convert spaces to... (6 Replies)
Discussion started by: brakeb
6 Replies

3. Shell Programming and Scripting

extract string from file name between two underscores

Hi, Here is my question, I need to extract string between two underscores from the filename for example, filename is atmos_8xdaily_instant_300x300_1_12.nc what I want to extract is 300x300. There are many such files in my directory, so I guess the code should be like: for file... (7 Replies)
Discussion started by: 1988PF
7 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. Shell Programming and Scripting

How to find no of underscores in a variable?

Hi i have a variable var=a_b_c i want command to find no. of underscores in a variable Thank you (7 Replies)
Discussion started by: pracheth
7 Replies

6. UNIX for Dummies Questions & Answers

Appending "_" (underscores) to variable names

Hello, I have a file, inputs.list that contain prefixes to files that are inputs for a program inputs.list A B C D E ... My files are of the format A_1, A_2, B_1, B_2 and so on. I am planning to run a shell script that takes each line from the above file and feed it to the runProg.sh... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

7. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

8. Shell Programming and Scripting

How to separate data with varying amounts of underscores?

All, I've searched and haven't found a solution for my particular issue. I have a file with lines of data that contain varying amounts of underscores imbedded. But all the strings have a final underscore and an interface name: dmk_hcn_dalian2.XXXX.XXX.XX.COM_Se0/0/0... (4 Replies)
Discussion started by: turk22
4 Replies

9. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

10. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies
PREG_REPLACE_CALLBACK(3)						 1						  PREG_REPLACE_CALLBACK(3)

preg_replace_callback - Perform a regular expression search and replace using a callback

SYNOPSIS
mixed preg_replace_callback (mixed $pattern, callable $callback, mixed $subject, [int $limit = -1], [int &$count]) DESCRIPTION
The behavior of this function is almost identical to preg_replace(3), except for the fact that instead of $replacement parameter, one should specify a $callback. PARAMETERS
o $pattern - The pattern to search for. It can be either a string or an array with strings. o $callback - A callback that will be called and passed an array of matched elements in the $subject string. The callback should return the replacement string. This is the callback signature: string handler (array $matches) You'll often need the $callback function for a preg_replace_callback(3) in just one place. In this case you can use an anonymous function to declare the callback within the call to preg_replace_callback(3). By doing it this way you have all information for the call in one place and do not clutter the function namespace with a callback function's name not used anywhere else. Example #1 preg_replace_callback(3) and anonymous function <?php /* a unix-style command line filter to convert uppercase * letters at the beginning of paragraphs to lowercase */ $fp = fopen("php://stdin", "r") or die("can't read stdin"); while (!feof($fp)) { $line = fgets($fp); $line = preg_replace_callback( '|<p>s*w|', function ($matches) { return strtolower($matches[0]); }, $line ); echo $line; } fclose($fp); ?> o $subject - The string or an array with strings to search and replace. o $limit - The maximum possible replacements for each pattern in each $subject string. Defaults to -1 (no limit). o $count - If specified, this variable will be filled with the number of replacements done. RETURN VALUES
preg_replace_callback(3) returns an array if the $subject parameter is an array, or a string otherwise. On errors the return value is NULL If matches are found, the new subject will be returned, otherwise $subject will be returned unchanged. CHANGELOG
+--------+---------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------+ | 5.1.0 | | | | | | | The $count parameter was added | | | | +--------+---------------------------------+ EXAMPLES
Example #2 preg_replace_callback(3) example <?php // this text was used in 2002 // we want to get this up to date for 2003 $text = "April fools day is 04/01/2002 "; $text.= "Last christmas was 12/24/2001 "; // the callback function function next_year($matches) { // as usual: $matches[0] is the complete match // $matches[1] the match for the first subpattern // enclosed in '(...)' and so on return $matches[1].($matches[2]+1); } echo preg_replace_callback( "|(d{2}/d{2}/)(d{4})|", "next_year", $text); ?> The above example will output: April fools day is 04/01/2003 Last christmas was 12/24/2002 Example #3 preg_replace_callback(3) using recursive structure to handle encapsulated BB code <?php $input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain"; function parseTagsRecursive($input) { $regex = '#[indent]((?:[^[]|[(?!/?indent])|(?R))+)[/indent]#'; if (is_array($input)) { $input = '<div style="margin-left: 10px">'.$input[1].'</div>'; } return preg_replace_callback($regex, 'parseTagsRecursive', $input); } $output = parseTagsRecursive($input); echo $output; ?> SEE ALSO
PCRE Patterns, preg_quote(3), preg_replace(3), preg_last_error(3), Anonymous functions, information about the callback type. PHP Documentation Group PREG_REPLACE_CALLBACK(3)
All times are GMT -4. The time now is 07:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy