Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-18-2015, 02:51 AM   #1
Registered: May 2013
Posts: 200

Rep: Reputation: 1
Replace a string using sed

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

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

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

Old 09-18-2015, 02:56 AM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,635

Rep: Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535Reputation: 3535
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, 08:14 AM   #3
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:
        "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):



# 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

# 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:
$ 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,


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 06: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 11:05 AM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:06 AM.

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