Sponsored Content
Top Forums Shell Programming and Scripting Awk,sed : change every 2nd field ":" to "|" Post 302825667 by Scrutinizer on Tuesday 25th of June 2013 12:42:51 AM
Old 06-25-2013
The sed command uses a substitute command ( s/.../.../ ) . The first part of the expression contains a basic regular expression (regex). The escaped parentheses \( and \) are used to group parts of matched text that can be back referenced by \1 in the second part of the s-command.

So in this case only the second colon is outside the grouped part and does not get back referenced, so effectively it gets discarded. If we apply this to your input file then the first match is :8:, which gets substituted with :8|. At the end of the expression is the letter g, which is the "global" flag, which means that the operation should be repeated for every occurrence on the line.

So this will be repeated, and crucial here, is that the next match will start after the previous match, so next up will be :4: which becomes :4| and then :14: becomes :14| and so on...

Last edited by Scrutinizer; 06-25-2013 at 01:49 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

3. Shell Programming and Scripting

Help to change the file with "sed" and "awk"

Hi experts I want your help to change the file format to my wanted version, please give me a hand thanks $cat file install pass make os pass make build kernel failed usb storage pass chane to | *install* | *make os* | *make build kernel* | *usb storage* | | pass | pass... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

4. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

8. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

9. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

10. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
regsub(n)						       Tcl Built-In Commands							 regsub(n)

__________________________________________________________________________________________________________________________________________________

NAME
regsub - Perform substitutions based on regular expression pattern matching SYNOPSIS
regsub ?switches? exp string subSpec ?varName? _________________________________________________________________ DESCRIPTION
This command matches the regular expression exp against string, and either copies string to the variable whose name is given by varName or returns string if varName is not present. (Regular expression matching is described in the re_syntax reference page.) If there is a match, then while copying string to varName (or to the result of this command if varName is not present) the portion of string that matched exp is replaced with subSpec. If subSpec contains a "&" or "", then it is replaced in the substitution with the portion of string that matched exp. If subSpec contains a " ", where n is a digit between 1 and 9, then it is replaced in the substitution with the portion of string that matched the n'th parenthesized subexpression of exp. Additional backslashes may be used in subSpec to prevent special inter- pretation of "&", "", " " and backslashes. The use of backslashes in subSpec tends to interact badly with the Tcl parser's use of back- slashes, so it is generally safest to enclose subSpec in braces if it includes backslashes. If the initial arguments to regsub start with - then they are treated as switches. The following switches are currently supported: -all All ranges in string that match exp are found and substitution is performed for each of these ranges. Without this switch only the first matching range is found and substituted. If -all is specified, then "&" and " " sequences are handled for each sub- stitution using the information from the corresponding match. -expanded Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as speci- fying the (?x) embedded option (see the re_syntax manual page). -line Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag, "[^" bracket expressions and "." never match newline, "^" matches an empty string after any newline in addition to its normal function, and "$" matches an empty string before any newline in addition to its normal function. This flag is equivalent to specifying both -linestop and -lineanchor, or the (?n) embedded option (see the re_syntax manual page). -linestop Changes the behavior of "[^" bracket expressions and "." so that they stop at newlines. This is the same as specifying the (?p) embedded option (see the re_syntax manual page). -lineanchor Changes the behavior of "^" and "$" (the "anchors") so they match the beginning and end of a line respectively. This is the same as specifying the (?w) embedded option (see the re_syntax manual page). -nocase Upper-case characters in string will be converted to lower-case before matching against exp; however, substitutions specified by subSpec use the original unconverted form of string. -start index Specifies a character index offset into the string to start matching the regular expression at. The index value is interpreted | in the same manner as the index argument to string index. When using this switch, "^" will not match the beginning of the line, and A will still match the start of the string at index. index will be constrained to the bounds of the input string. -- Marks the end of switches. The argument following this one will be treated as exp even if it starts with a -. If varName is supplied, the command returns a count of the number of matching ranges that were found and replaced, otherwise the string after replacement is returned. See the manual entry for regexp for details on the interpretation of regular expressions. EXAMPLES
Replace (in the string in variable string) every instance of foo which is a word by itself with bar: regsub -all {mfooM} $string bar string or (using the "basic regular expression" syntax): regsub -all {(?b)<foo>} $string bar string Insert double-quotes around the first instance of the word interesting, however it is capitalized. regsub -nocase {yinterestingy} $string {"&"} string Convert all non-ASCII and Tcl-significant characters into u escape sequences by using regsub and subst in combination: # This RE is just a character class for everything "bad" set RE {[][{};#\$su0080-uffff]} # We will substitute with a fragment of Tcl script in brackets set substitution {[format \\u%04x [scan "\&" %c]]} # Now we apply the substitution to get a subst-string that # will perform the computational parts of the conversion. set quoted [subst [regsub -all $RE $string $substitution]] SEE ALSO
regexp(n), re_syntax(n), subst(n), string(n) | KEYWORDS
match, pattern, quoting, regular expression, substitute Tcl 8.3 regsub(n)
All times are GMT -4. The time now is 09:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy