Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Search partial string in a file and replace the string - UNIX Post 303040814 by sarathy_a35 on Thursday 7th of November 2019 08:03:06 AM
Old 11-07-2019
@ Neo : Windows os only and im using Unix Shell/Bash scripting.
I have applied the below logic to eliminate comma if available in the string and then kept each string in array. Then I tried to search it in the file,but replacing the contents in the file is not working as expected.

For Scenario-1 :
Code:
#/bin/bash
STRING="user::r--,user:ourfrd:r--"
array=`echo $STRING | awk -F ',' '{ s = $1; for (i = 2; i <= NF; i++) s = s "\n"$i; print s; }'`
for word in ${array}
do
        sed -r -e 's/${array}[^{array}]+//g' file.txt
done

For Scenario-2 :
Code:
#/bin/bash
STRING="user::r--"
array=`echo $STRING | awk -F ',' '{ s = $1; for (i = 2; i <= NF; i++) s = s "\n"$i; print s; }'`
for word in ${array}
do
        sed -r -e 's/${array}[^{array}]+//g' file.txt
done

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

2. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

3. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

4. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

5. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

6. Shell Programming and Scripting

How to replace partial string

I have a list of strings in file: 10 10 AAA 120 13 BBBBB 23 11 CCCCC 11 32 DDDDDD I want to replace first column of the text such as: 10, 129, 23, 11 with 11, 22, 33, 44. I can do line by line, but just not sure how to replace partial string without... (1 Reply)
Discussion started by: ford99
1 Replies

7. Shell Programming and Scripting

Search for a string in a file and replace

I have file t1.log Contents of t1.log file Number of records processed: Number of records rejected: Error : xyz .......... abc .......... aaa _] start time : end time : Please let me know how i can remove the contents highlighted in red in the t1.log file. Thanks Sam (3 Replies)
Discussion started by: sam777
3 Replies

8. Shell Programming and Scripting

How to replace partial of string in file?

Hi Guys, I need replace part of string in a file. for example: ABC=123 CDE=122 DEF=456 ABC=123 DED=333 ABC=123 I need replace the value after ABC=, highlighted in red. I want to get following result; ABC=456 CDE=122 DEF=456 ABC=456 DED=333 ABC=456 Anybody can help me this. ... (8 Replies)
Discussion started by: ken6503
8 Replies

9. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies
SNMP3_REAL_WALK(3)							 1							SNMP3_REAL_WALK(3)

snmp3_real_walk - Return all objects including their respective object ID within the specified one

SYNOPSIS
array snmp3_real_walk (string $host, string $sec_name, string $sec_level, string $auth_protocol, string $auth_passphrase, string $priv_protocol, string $priv_passphrase, string $object_id, [string $timeout = 1000000], [string $retries = 5]) DESCRIPTION
The snmp3_real_walk(3) function is used to traverse over a number of SNMP objects starting from $object_id and return not only their val- ues but also their object ids. PARAMETERS
o $host - The hostname of the SNMP agent (server). o $sec_name - the security name, usually some kind of username o $sec_level - the security level (noAuthNoPriv|authNoPriv|authPriv) o $auth_protocol - the authentication protocol (MD5 or SHA) o $auth_passphrase - the authentication pass phrase o $priv_protocol - the privacy protocol (DES or AES) o $priv_passphrase - the privacy pass phrase o $object_id - The SNMP object id. o $timeout - The number of microseconds until the first timeout. o $retries - The number of times to retry if timeouts occur. RETURN VALUES
Returns an associative array of the SNMP object ids and their values on success or FALSE on error. In case of an error, an E_WARNING mes- sage is shown. EXAMPLES
Example #1 Using snmp3_real_walk(3) <?php var_export(snmp3_real_walk('localhost', 'james', 'authPriv', 'SHA', 'secret007', 'AES', 'secret007', 'IF-MIB::ifName')); ?> The above will output something like: array ( 'IF-MIB::ifName.1' => 'STRING: lo', 'IF-MIB::ifName.2' => 'STRING: eth0', 'IF-MIB::ifName.3' => 'STRING: eth2', 'IF-MIB::ifName.4' => 'STRING: sit0', 'IF-MIB::ifName.5' => 'STRING: sixxs', ) SEE ALSO
snmpwalk(3). PHP Documentation Group SNMP3_REAL_WALK(3)
All times are GMT -4. The time now is 04:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy