sed for string manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed for string manipulation
# 1  
Old 03-21-2017
sed for string manipulation

I have a file which contains contents like below

Code:
proxy.config.cluster.mc_group_addr	224.0.1.37
proxy.config.log.logging_enabled	3
proxy.config.log.squid_log_enabled	1

Need to modify to
Code:
'proxy.config.cluster.mc_group_addr': '224.0.1.37'
'proxy.config.log.logging_enabled': '3'
'proxy.config.log.squid_log_enabled': '	1'

tried various sed like
Code:
 sed 's/[A-Za-z]*[0-9]*[.]

But didnt help..



Moderator's Comments:
Mod Comment You have enough posts to know about code tags. Please use them, thanks.

Last edited by zaxxon; 03-21-2017 at 08:53 AM..
# 2  
Old 03-21-2017
perl -npi -e "s/^(\S+) (.+)/'\1' a2/" tmp.dat
# 3  
Old 03-21-2017
Hello esham,

Could you please try following and let me know if this helps you.
Code:
sed  's/\(.[^ ]*\)\( \)\(.*\)/'"'"'\1'"'"': '"'"'\3'"'"'/g'   Input_file

If you are happy with above code's output then change sed to sed -i.

Thanks,
R. Singh
# 4  
Old 03-21-2017
Code:
sed -r "s/^|$/'/g; s/	/': '/" file
'proxy.config.cluster.mc_group_addr': '224.0.1.37'
'proxy.config.log.logging_enabled': '3'
'proxy.config.log.squid_log_enabled': '1'

Be aware that the second s ubstitute command has a <TAB> in its pattern...
And, Can't see how and why you have a leading space in front of the 1 but not the 3 .
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-21-2017
Facebook

You might want to consider the following, which is built on RudiCs idea. It removes the necessity to have consistent separators between "fields". Replace "<b>" and "<t>" with literal blank and tab characters. It furthermore removes an (maybe unnecessary) ambiguity about trailing and leading blanks RudiCs solution would have tripped over:

Quote:
Originally Posted by RudiC
Code:
sed -r "s/^|$/'/g; s/	/': '/" file

Code:
sed 's/^[<b><t>]*/&\'/;s/[<b><t>]*$/'&/;s/[<b><t>:]*/\'&\'/g' file

I hope this helps.

bakunin
# 6  
Old 03-21-2017
got it working..

Code:
sed -r "s/^|$/'/g" records.config  | sed "s/\s/': '/g"
'proxy.config.log.squid_log_enabled: '1'
'proxy.config.log.logfile_dir: '/tmp/logs'
'proxy.config.log.squid_log_is_ascii: '1'
'proxy.config.log.squid_log_name: 'foo'


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-21-2017 at 02:56 PM.. Reason: Added CODE tags.
# 7  
Old 03-21-2017
There's a single quote missing in your output. And, why a pipe with two sed invocations?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulation with the string using sed

hello All, When I run find command on certain directory I may get one of the following output depending on configuration A. ./rel/prod/libpam.a B. ./rel/fld/libpam.a C. ./deb/detail/libpam.a D. ./deb/err/libpam.a I want to get output as below A. rel/prod B.... (2 Replies)
Discussion started by: anand.shah
2 Replies

2. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

3. Shell Programming and Scripting

string manipulation

Hi, I have the followoing details in one file: opt/tra/domain/test/new/filename1 training/ear/help I need to manipulate the string in the following manner: filename1= opt/tra/domain/test/new/filename1 help=training/ear/help last string is the name and equal sign and then... (2 Replies)
Discussion started by: ckchelladurai
2 Replies

4. Shell Programming and Scripting

String Manipulation

Hi, I have a file in the following format 123|shanwer|15DEC2010|bgbh|okok|16JAN3000|okok| I want the following to be in following format 123|shanwer|12\15\2010|bgbh|okok|01\16\3000|okok| SED/PERL/AWK Gurus could you please help me with this? Thanks Shankar (8 Replies)
Discussion started by: Shan2210
8 Replies

5. Shell Programming and Scripting

sed string manipulation in shell script

Hello, I have 1000 of sql queries and i need to push column value in query. e.g. SET INSERT_ID=1 INSERT INTO test (id,name) VALUES ('a'); SET INSERT_ID=2 INSERT INTO test (id,name) VALUES ('b'); SET INSERT_ID=3 INSERT INTO test (id,name) VALUES ('c'); SET INSERT_ID=4 INSERT INTO test... (12 Replies)
Discussion started by: mirfan
12 Replies

6. Shell Programming and Scripting

string manipulation

Hi all, see i have a script that takes few arguments. first one is command we do on file, next is file (mostly txt file with lot of data) third is destination where we do something with data in file. Since im new in scripting, and im learning as i go, i need some hint how to manipulate that... (3 Replies)
Discussion started by: ajemrunner
3 Replies

7. UNIX for Dummies Questions & Answers

String manipulation

I am doing some training for a job I have just got and there is an exercise I am stuck with. I am not posting to ask a question about logic, just a trivial help with string manipulation. I would appreciate if somebody could at least give me a hint on how to do it. Basically, the intelligent part... (8 Replies)
Discussion started by: Dantastik
8 Replies

8. Shell Programming and Scripting

sed string manipulation

hi I am using sed to split a string this string is 11byteabc I would like to just get the numeric digits. echo "11byteabc" | sed 's/*// returns 11byteabc only solution that works is repeating number of times for the letters which is pointless grateful for any suggestions thanks (4 Replies)
Discussion started by: speedieB
4 Replies

9. Shell Programming and Scripting

How to use sed for string manipulation

Hi, I would like to know How to use use sed for manipulating string for the following situation. Basically my objective is to check validity of the filename in my shell script. I am getting a parameter like this for my shell script. Check my folder is having some space. $1=/root/krishna... (2 Replies)
Discussion started by: hikrishn
2 Replies

10. UNIX for Dummies Questions & Answers

string manipulation

Hi, I have a file with rows of text like so : E100005568374098100000015667 D100005568374032000000112682 H100005228374060800000002430 I need to grab just the last digits(bolded) of each line without the proceeding text/numbers. Thanks (5 Replies)
Discussion started by: james6
5 Replies
Login or Register to Ask a Question