sed inside the awk script to replace a string in the array


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed inside the awk script to replace a string in the array
# 1  
Old 08-27-2019
sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it.

I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i achieve this?

Code:
for (i=msgtkn_20_lnno; i<msg_lnno;)
{
if(msg_arr[i] ~ /^{5:{/ )
{
tag5_before_value= msg_arr[i];
msg_arr[i]="echo $tag5_before_value | sed 's/{5:{/5:/g'";
printf "after conversion is %s/n", abc > "LOG.txt";
print msg_arr[i++];
}
else
{
print msg_arr[i++];
}

}

sample :
Code:
{1:36rirfmvhfgf
:20:687nkj
:56:6979840
{5:{CBS:.....}}

Output:
Code:
{1:36rirfmvhfgf
:20:687nkj
:56:6979840
5:CBS:.....}}

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 08-27-2019 at 09:32 PM.. Reason: Add CODE and ICODE tags
# 2  
Old 08-27-2019
try:
Code:
gsub("{5:{", "5:", msg_arr[i]);

instead of:
Code:
tag5_before_value= msg_arr[i];
msg_arr[i]="echo $tag5_before_value | sed 's/{5:{/5:/g'";

# 3  
Old 08-27-2019
Tried this :

Code:
               for (i=msgtkn_20_lnno; i<msg_lnno;)
                {
                   if( msg_arr[i] ~ /^{5:{/ )
                   {
                   msg_arr[i]= gsub("{5:{", "5:", msg_arr[i]);
                   print msg_arr[i++];
                   }
                   else
                   {
                   print msg_arr[i++];
                   }
                }

Error :

Code:
awk: syntax error near line 180
awk: illegal statement near line 180

Tried below as well and got the same error

Code:
  for (i=msgtkn_20_lnno; i<msg_lnno;)
                {
                   if( msg_arr[i] ~ /^{5:{/ )
                   {
                  gsub("{5:{", "5:", msg_arr[i]); print msg_arr[i++];
                   }
                   else
                   {
                   print msg_arr[i++];
                   }
                }


Last edited by Don Cragun; 08-27-2019 at 09:35 PM.. Reason: Add CODE tags, again.
# 4  
Old 08-27-2019
Quote:
Originally Posted by bhagya123
Tried this :

Code:
               for (i=msgtkn_20_lnno; i<msg_lnno;)
                {
                   if( msg_arr[i] ~ /^{5:{/ )
                   {
                   msg_arr[i]= gsub("{5:{", "5:", msg_arr[i]);
                   print msg_arr[i++];
                   }
                   else
                   {
                   print msg_arr[i++];
                   }
                }

Error :

Code:
awk: syntax error near line 180
awk: illegal statement near line 180

Tried below as well and got the same error

Code:
  for (i=msgtkn_20_lnno; i<msg_lnno;)
                {
                   if( msg_arr[i] ~ /^{5:{/ )
                   {
                  gsub("{5:{", "5:", msg_arr[i]); print msg_arr[i++];
                   }
                   else
                   {
                   print msg_arr[i++];
                   }
                }

With what you have shown us, we have to do a lot of guessing to try to figure out what might be going wrong. Please, whenever you start a thread, tell us what operating system and shell you're using. And, when showing us a fragment of an awk script with associated diagnostic messages, indicate which line of the code you have shown us is the one that the diagnostic refers to (in this case, which line is line #180).
# 5  
Old 08-27-2019
Sorry..The error is right at the gsub command in both cases.

below is my unix os version

SunOS unixs1095 5.10 Generic_150400-62 sun4v sparc sun4v
# 6  
Old 08-27-2019
OK.

First, use /usr/xpg4/bin/awk or nawk; not awk when you're using any Solaris/SunOS system. (I strongly recommend /usr/xpg4/bin/awk since it meets all of the POSIX standard's requirements.)

Second, while sed uses Basic Regular Expressions (BREs), awk uses Extended Regular Expressions (EREs). And, in EREs, the opening brace character ({) is special and needs to be escaped when you are looking for a literal opening brace character. The code rdrtx1 suggested may work on some systems, but is not portable and (according to the standards) produces undefined behavior.

Third, since you are looking for a single occurrence of the string {5:{, there is no need to use gsub() (which looks for and attempts to find and replace as many occurrences of a matching pattern as it can find).

Try this instead:
Code:
for (i=msgtkn_20_lnno; i<msg_lnno; i++) {
	if( msg_arr[i] ~ /^[{]5:[{]/ ) {
		sub("[{]5:[{]", "5:", msg_arr[i]);
	}
	print msg_arr[i];
}

Note that I also moved the i++ into the for statement instead of hiding the loop increment in your print statements and I moved the print statement out of both branches of the if statement so it is more obvious that the same print is needed whether or not a match is found.
# 7  
Old 08-27-2019
P.S. You might also note that the following code will produce the same results as the code shown in post #6:
Code:
for (i=msgtkn_20_lnno; i<msg_lnno; i++) {
	sub("^[{]5:[{]", "5:", msg_arr[i]);
	print msg_arr[i];
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. UNIX for Dummies Questions & Answers

Array inside sed

Hi guys Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one. Ow 1230 16.000000 -0.834000 16.083957 1.751652398 -17.20094528 -4.450623277 Hw 1231 ... (6 Replies)
Discussion started by: saleheen
6 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line containing a string

hi experts , I have an input like following. R sfst 1000.0000 $ new time step for mass scaled calculation R dt2ms -4.000E-7 $ friction value for blank R mue ... (10 Replies)
Discussion started by: hamnsan
10 Replies

7. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

8. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

9. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

10. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies
Login or Register to Ask a Question