LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2015, 03:51 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 200

Rep: Reputation: 1
Replace a string using sed


I want to replace following string
Code:
"ownerUserInfo": [
      {
        "user_id": "
into this

Code:
"ownerUserInfo": [
      {
        "user_id": ObjectId("
using sed.

I have tried these combinations but it did not work.
Code:
sed 's#"ownerUserInfo": [\n      {\n       "user_id": "#"ownerUserInfo": [\n      {\n       "user_id": ObjectId("#g' mongo.js
Code:
sed 's#ownerUser\(.*\)user_id\(.*\)\"\: \"#ownerUser\(.*\)user_id\(.*\)\"\: ObjectId\(\"' mongo.js
Please help me.

Thanks
 
Old 09-18-2015, 03:56 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,484

Rep: Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097Reputation: 3097
sed is a stream editor - it deals with (single) records, and so doesn't "see" the newlines.
It has commands that allow you to concatenate records (including the newline in need), but it's not a simple "s" command like that. See the doco.
 
Old 09-18-2015, 09:14 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Well, it is apparent that you cannot simply use sed to search and replace:
Code:
        "user_id": "
Because then you would have done so! I guess there are numerous instances of that string.

Anyway... I thought that since you are at first looking for the string 'ownerUserInfo', and that you know (I hope) that the text you want to change appears two lines below that line in the file, you could use sed to start at the line which you want to change, and simply overwrite it with the new information.

Something like this (where unclesamcrazy.txt is the snippet you provided):

Code:
#!/bin/bash

# http://www.linuxquestions.org/questions/linux-newbie-8/replace-a-string-using-sed-4175553839/

# Get the line number of the phrase 'ownerUserInfo'
LINESTART=$(grep -n ownerUserInfo unclesamcrazy.txt | grep -o ^.)
# Since the line we want to change is 2 lines below,
# increment by 2
LINE_TO_CHANGE=$(($LINESTART+2))

# Overwrite that line with the new text
sed "$LINE_TO_CHANGE c\ \t\"user_id\": ObjectId(\"" unclesamcrazy.txt > new_unclesam.txt

exit 0
^That little script produces the following outfile:
Code:
$ cat new_unclesam.txt 
"ownerUserInfo": [
      {
 	"user_id": ObjectId("
I'm sure the awk ninjas will be falling over with paroxysmal laughter at this, admittedly, rather cumbersome approach. But it is the best I can come up with right now.

Another way would be to read the file, line by line, and make the changes after the appearance of your keyword... perhaps. (Or learn awk properly!)

Best regards,
HMW
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed replace string xmickaelx Programming 3 04-07-2011 07:42 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
sed replace string octeto Programming 4 06-06-2007 03:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 11:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration