Sponsored Content
Top Forums Shell Programming and Scripting Awk command to replace specific position characters. Post 302594660 by pinnacle on Wednesday 1st of February 2012 01:14:50 AM
Old 02-01-2012
Thanks Mirni and Chubler.

I got most of it except the following.

substr(s,1,f-1) --> line before the string that needs to be replaced

sprintf("%-*s", t-f+1, v) --> here "s" represents the entire line. t-f+1 lenght of string being replaced, v is the new string to be inserted. But where are we saying the start and end position to the sprintf function for the new string to be inserted. We are just passing the length of the new string wanted.

Here is where i need little more help. I looked at the sprint function man page but is not of much help.


substr(s,t+1) --> line after substring to be replaced.


And also we are not using any concatenate function to join various substrings. But in sql and other Datawarehouse tools we use concatenate function.

Appreciate your help on this.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies

2. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

3. Shell Programming and Scripting

Add characters at specific position in file

Hello I want to add some value at the specific position. My file has data like Hello Welcome to UNIX Forums Need Assistance I want to add some value at the end but at same character position for all lines. I want my output file to have data like : Here '_' represents blanks.... (3 Replies)
Discussion started by: dashing201
3 Replies

4. Shell Programming and Scripting

Sed position specific replace

I'm drawing a blank on how to use sed to replace selectively based on position in the string (vs nth occurence): hello.|there.|how.|are.|you.| I want the period removed in the 3rd item (as defined by the pipe delimiter) if a period is present. So the result in this case would be: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

5. Shell Programming and Scripting

Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do: I'm trying to replace a string at a certain location with another string. Heres the file I'm trying to change: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

6. Shell Programming and Scripting

position specific replace in file

How to replace the position specific values in the file.. i searched a lot the forums but i couldn't able to do... i have file like below 576666666666666666666666666 7878 897987 121 0asdas Y12 5900fbb 777 09JJJ 78798347892374 234234234364 234232898 89HJHIHIGIUG989902743748327khjkhkjlh... (6 Replies)
Discussion started by: greenworld123
6 Replies

7. Shell Programming and Scripting

Printing characters at specific position in line

Hi, I am trying to get an output like : +----------------------------------+ ----------- + + some variable substitution + some text + Is there a way I can specify in printf (in ksh) the particular position I want to print a character, and also repeat a character from... (1 Reply)
Discussion started by: neil.k
1 Replies

8. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

9. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

10. Shell Programming and Scripting

sed command on AIX, replace specific characters

Hi, Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed. the source data that I have is$cat v.txt 4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY... (9 Replies)
Discussion started by: dsid
9 Replies
Str(3)								   OCaml library							    Str(3)

NAME
Str - Regular expressions and high-level string processing Module Module Str Documentation Module Str : sig end Regular expressions and high-level string processing === Regular expressions === type regexp The type of compiled regular expressions. val regexp : string -> regexp Compile a regular expression. The following constructs are recognized: - . Matches any character except newline. - * (postfix) Matches the preceding expression zero, one or several times - + (postfix) Matches the preceding expression one or several times - ? (postfix) Matches the preceding expression once or not at all - [..] Character set. Ranges are denoted with - , as in [a-z] . An initial ^ , as in [^0-9] , complements the set. To include a ] char- acter in a set, make it the first character of the set. To include a - character in a set, make it the first or the last character of the set. - ^ Matches at beginning of line (either at the beginning of the matched string, or just after a newline character). - $ Matches at end of line (either at the end of the matched string, or just before a newline character). - | (infix) Alternative between two expressions. - (..) Grouping and naming of the enclosed expression. - 1 The text matched by the first (...) expression ( 2 for the second expression, and so on up to 9 ). -  Matches word boundaries. - Quotes special characters. The special characters are $^.*+?[] . val regexp_case_fold : string -> regexp Same as regexp , but the compiled expression will match text in a case-insensitive way: uppercase and lowercase letters will be considered equivalent. val quote : string -> string Str.quote s returns a regexp string that matches exactly s and nothing else. val regexp_string : string -> regexp Str.regexp_string s returns a regular expression that matches exactly s and nothing else. val regexp_string_case_fold : string -> regexp Str.regexp_string_case_fold is similar to Str.regexp_string , but the regexp matches in a case-insensitive way. === String matching and searching === val string_match : regexp -> string -> int -> bool string_match r s start tests whether a substring of s that starts at position start matches the regular expression r . The first character of a string has position 0 , as usual. val search_forward : regexp -> string -> int -> int search_forward r s start searches the string s for a substring matching the regular expression r . The search starts at position start and proceeds towards the end of the string. Return the position of the first character of the matched substring. Raises Not_found if no substring matches. val search_backward : regexp -> string -> int -> int search_backward r s last searches the string s for a substring matching the regular expression r . The search first considers substrings that start at position last and proceeds towards the beginning of string. Return the position of the first character of the matched sub- string. Raises Not_found if no substring matches. val string_partial_match : regexp -> string -> int -> bool Similar to Str.string_match , but also returns true if the argument string is a prefix of a string that matches. This includes the case of a true complete match. val matched_string : string -> string matched_string s returns the substring of s that was matched by the last call to one of the following matching or searching functions: - Str.string_match - Str.search_forward - Str.search_backward - Str.string_partial_match - Str.global_substitute - Str.substitute_first provided that none of the following functions was called inbetween: - Str.global_replace - Str.replace_first - Str.split - Str.bounded_split - Str.split_delim - Str.bounded_split_delim - Str.full_split - Str.bounded_full_split Note: in the case of global_substitute and substitute_first , a call to matched_string is only valid within the subst argument, not after global_substitute or substitute_first returns. The user must make sure that the parameter s is the same string that was passed to the matching or searching function. val match_beginning : unit -> int match_beginning() returns the position of the first character of the substring that was matched by the last call to a matching or searching function (see Str.matched_string for details). val match_end : unit -> int match_end() returns the position of the character following the last character of the substring that was matched by the last call to a matching or searching function (see Str.matched_string for details). val matched_group : int -> string -> string matched_group n s returns the substring of s that was matched by the n th group (...) of the regular expression that was matched by the last call to a matching or searching function (see Str.matched_string for details). The user must make sure that the parameter s is the same string that was passed to the matching or searching function. Raises Not_found if the n th group of the regular expression was not matched. This can happen with groups inside alternatives | , options ? or repetitions * . For instance, the empty string will match (a)* , but matched_group 1 will raise Not_found because the first group itself was not matched. val group_beginning : int -> int group_beginning n returns the position of the first character of the substring that was matched by the n th group of the regular expression that was matched by the last call to a matching or searching function (see Str.matched_string for details). Raises Not_found if the n th group of the regular expression was not matched. Invalid_argument if there are fewer than n groups in the regular expression. val group_end : int -> int group_end n returns the position of the character following the last character of substring that was matched by the n th group of the regu- lar expression that was matched by the last call to a matching or searching function (see Str.matched_string for details). Raises Not_found if the n th group of the regular expression was not matched. Invalid_argument if there are fewer than n groups in the regular expression. === Replacement === val global_replace : regexp -> string -> string -> string global_replace regexp templ s returns a string identical to s , except that all substrings of s that match regexp have been replaced by templ . The replacement template templ can contain 1 , 2 , etc; these sequences will be replaced by the text matched by the corresponding group in the regular expression. stands for the text matched by the whole regular expression. val replace_first : regexp -> string -> string -> string Same as Str.global_replace , except that only the first substring matching the regular expression is replaced. val global_substitute : regexp -> (string -> string) -> string -> string global_substitute regexp subst s returns a string identical to s , except that all substrings of s that match regexp have been replaced by the result of function subst . The function subst is called once for each matching substring, and receives s (the whole text) as argument. val substitute_first : regexp -> (string -> string) -> string -> string Same as Str.global_substitute , except that only the first substring matching the regular expression is replaced. val replace_matched : string -> string -> string replace_matched repl s returns the replacement text repl in which 1 , 2 , etc. have been replaced by the text matched by the correspond- ing groups in the regular expression that was matched by the last call to a matching or searching function (see Str.matched_string for details). s must be the same string that was passed to the matching or searching function. === Splitting === val split : regexp -> string -> string list split r s splits s into substrings, taking as delimiters the substrings that match r , and returns the list of substrings. For instance, split (regexp [ ]+ ) s splits s into blank-separated words. An occurrence of the delimiter at the beginning or at the end of the string is ignored. val bounded_split : regexp -> string -> int -> string list Same as Str.split , but splits into at most n substrings, where n is the extra integer parameter. val split_delim : regexp -> string -> string list Same as Str.split but occurrences of the delimiter at the beginning and at the end of the string are recognized and returned as empty strings in the result. For instance, split_delim (regexp ) abc returns [""; abc ; ] , while split with the same arguments returns ["abc"] . val bounded_split_delim : regexp -> string -> int -> string list Same as Str.bounded_split , but occurrences of the delimiter at the beginning and at the end of the string are recognized and returned as empty strings in the result. type split_result = | Text of string | Delim of string val full_split : regexp -> string -> split_result list Same as Str.split_delim , but returns the delimiters as well as the substrings contained between delimiters. The former are tagged Delim in the result list; the latter are tagged Text . For instance, full_split (regexp [{}] ) {ab} returns [Delim { ; Text ab ; Delim } ] . val bounded_full_split : regexp -> string -> int -> split_result list Same as Str.bounded_split_delim , but returns the delimiters as well as the substrings contained between delimiters. The former are tagged Delim in the result list; the latter are tagged Text . === Extracting substrings === val string_before : string -> int -> string string_before s n returns the substring of all characters of s that precede position n (excluding the character at position n ). val string_after : string -> int -> string string_after s n returns the substring of all characters of s that follow position n (including the character at position n ). val first_chars : string -> int -> string first_chars s n returns the first n characters of s . This is the same function as Str.string_before . val last_chars : string -> int -> string last_chars s n returns the last n characters of s . OCamldoc 2014-06-09 Str(3)
All times are GMT -4. The time now is 11:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy