sed command: change only twice


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command: change only twice
# 1  
Old 09-17-2015
Code sed command: change only twice

Hello, I recently sought help on another thread about how to prefix 2 words in a file with 'pack/'. This is the command:
Code:
sed --in-place 's/"\(libraries\|objects\)"/"pack\/\1"/g'

Background: I have a .json file with the word 'libraries' and 'objects' in it. However, 'libraries' occurs twice; I only need to prefix the first occurrence. In the future, the .json file may contain more than 1 instance of the word 'objects' too, so I need the command to prefix the first word 'libraries' and the first word 'objects.'

However, I now need to know how to make this command only modify the FIRST occurence of the word 'libraries' and 'objects.'

Can anyone modify the command to make it only prefix the word libraries and objects once?

Last edited by vbe; 09-17-2015 at 08:44 AM.. Reason: code tags
# 2  
Old 09-17-2015
Code:
sed -i -e '0,/libraries/ s/\(libraries\)/pack\/\1/' -e '0,/objects/ s/\(objects\)/pack\/\1/' file

# 3  
Old 09-17-2015
Hi,
An example with gnu sed:
my example file:
Code:
$ cat example.txt
One line with "objects" and "libraries"
One line with "objects" and "libraries"
One line with "objects"
One line with "libraries"
One line with "objects" and "libraries"
One line with  nothing
One line with "objects" and "libraries"

Sed commande to modify first "libraries" and 3rd "objects":
Code:
$ sed -ne 'H;${x;s/^\n//;s/"\(libraries"\)/"pack\1/1;s/"\(objects"\)/"pack\1/3;p;}' example.txt
One line with "objects" and "packlibraries"
One line with "objects" and "libraries"
One line with "packobjects"
One line with "libraries"
One line with "objects" and "libraries"
One line with  nothing
One line with "objects" and "libraries"

Regards.
# 4  
Old 09-17-2015
Hi, I forgot to mention that there are other occurrences of the word libraries and objects, but they are followed by the word 'Location'. For example:

Code:
"minimumVersion":2,"librariesLocation":"libraries","objectsLocation":"objects",

should change to
Code:
  "minimumVersion":2,"librariesLocation":"pack/libraries","objectsLocation":"pack/objects",

Sorry for the confusion, I should have been clearer.

Balajesuri's answer works, except it also adds the /pack prefix to the second occurrence of 'libraries.'



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read a preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 09-17-2015 at 11:58 AM.. Reason: Code tags
# 5  
Old 09-17-2015
Ok, I corrected my solution that should work (replace just first libraries and first objects) -Not Tested- :
Code:
$ sed -ine 'H;${x;s/^\n//;s/"\(libraries"\)/"pack\/\1/1;s/"\(objects"\)/"pack\/\1/1;p;}' example.txt

You can correct balajesuri solution, you must just add double quote.
Regards.
# 6  
Old 09-17-2015
In case the multiple occurrences are in one line
Code:
sed '
s|"\(libraries\)"|"pack/\1"|
s|"\(objects\)"|"pack/\1"|
' example.txt

# 7  
Old 09-17-2015
Thanks disedorgue, that answer worked!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command not working for me to change text in a file

UNIX gurus I need your help with the following (The server is an AIX box). I have a text file with the following information: ******************************************************** SOME LINES case :WORD1 SOME LINES :WORD2 SOME LINES :WORD3 SOME LINES esac SOME LINES... (7 Replies)
Discussion started by: curiousmal
7 Replies

2. Shell Programming and Scripting

sed colour change

Hi, I am trying to write a script which will email a backup report from the server, The contents of the email will be: ---------------------- ---- -- ---- ----- ---- ------- ---- ------- ------- | | | | |Chnge|Wkng| | | | | | ... (6 Replies)
Discussion started by: Bdoydie
6 Replies

3. Shell Programming and Scripting

sed change in all files

I have a list of property file can each file has name=value kind of key value pair can replace all = with say ; in each file using sed or any other command (4 Replies)
Discussion started by: Vinayshell
4 Replies

4. Shell Programming and Scripting

Format of SED command to change a date

I have a website. I have a directory within it with over a hundred .html files. I need to change a date within every file. I don't have an easy way to find/replace. I need to change 10/31 to 11/30 on every single page at once. I tried the command below but it didn't work. Obviously I don't know... (3 Replies)
Discussion started by: ijustsawmars
3 Replies

5. Shell Programming and Scripting

How to use sed command for change special lines?

Hi, I want to add a character "#" in this few lines with sed command Initial: ### CACCIA: DEBUT ### if $(grep -wqi "$2" /etc/passwd); then chuser -R files registry=files $2 fi ### CACCIA: FIN ### Result with sed command: ### CACCIA: DEBUT ### #if $(grep -wqi "$2"... (4 Replies)
Discussion started by: khalidou13
4 Replies

6. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

7. Shell Programming and Scripting

Using sed command to change end of line

I am looking to change a data file into a javascript string and this is the code that I am using: sed -i '' -e 's/^/str += "/' -e 's/$/";/' file.xml The first part -e 's/^/str += "/' works as intended, but the second part -e 's/$/";/' adds an additional newline to my file, so that instead of... (3 Replies)
Discussion started by: figaro
3 Replies

8. Shell Programming and Scripting

how to change the content thru sed.....

Hi Everybody, Another headache form myside, i have a file name noname.txt having content as CS007=NEW and i have to write a scipt which will take the parameters as update,delete... with the 1st field of noname.txt (i.e. here CS007) whose objective is to change the 2nd field of noname.txt... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

9. Shell Programming and Scripting

SED command to change ./ to a space

Hello, i'm trying to remove the ./ from this file using this SED command: sed 's/.///g' all3.lst > all4.lst Does anyone know the correct format for this? For example, i'm trying to convert ./_ABMSTR.TXT.out to: _ABMSTR.TXT.out Thanks... (11 Replies)
Discussion started by: bobk544
11 Replies

10. Shell Programming and Scripting

sed command to change 2nd field

Hi I am a beginner to sed command, here I have a question about using sed to add a few characters into a token of a string. For example, I have a file, sqw:qqq:123124:uiqe dfd:ccc:12390:dfjis cde:aaa:21311:dfjsid and, I want the output to be, sqw:qqq:123124:uiqe... (4 Replies)
Discussion started by: Julius
4 Replies
Login or Register to Ask a Question