0

Could you please let me know how to replace a particular string present in a text file or ksh file in the server with another string ?

For example :-

I have 10 files present in the path /file_sys/file in which i have to replace the word "BILL" to "BILLING" in all the 10 files.

7
  • 1
    Take a look at the sed and awk utilities. Start by reading their man pages. Commented Jul 3, 2015 at 7:39
  • Yes i tried with sed -i 's/BILL/BILLING/g' * but i got the error sed: illegal option -- i Commented Jul 3, 2015 at 7:41
  • You local man page documents the -i flag and your sed command does not? That sounds very strange... Commented Jul 3, 2015 at 7:54
  • Also tried with sed 's/BILL/BILLING/g' Example.txt > Exam.txt but the Exam.txt is empty :( Commented Jul 3, 2015 at 7:54
  • Are you maybe using OS X on a Mac? If so, use sed -i.bak 's/something/else/' Commented Jul 3, 2015 at 9:30

4 Answers 4

1

Works for me:

I created a file 'test' with this content: "This is a simple test". Now I execute this call to the sed command:

sed -i 's/ is / is not /' test

Afterwards the file 'test' contains this content: "This is not a simple test"


If your sed utility does not support the -i flag, then there is a somewhat awkward workaround:

sed 's/ is / is not /' test > tmp_test && mv tmp_test test
Sign up to request clarification or add additional context in comments.

11 Comments

I tried with sed 's/BILL /BILLING /g' Example.txt but it didnt work for me as the file Example.txt is still having the value 'BILL'
I added a workaround for your strange version of sed. Awkward, but working.
Oh, and another hint: your regex notation tries to replace the string BILL , not BILL (note the trailing blank (` `)). If your file does not contain that blank, then the regex will not match and replace...
I'm sorry the space was my typo mistake.. i tried 's/BILL/BILLING/g' Example.txt > Exam.txt But the Exam.txt is blank
Yep, you cannot redirect the output of sed to the file you use as input because of how the utility works. That is why the -i flag was introduced which internally buffers the output. Without that flag being available you have to use the workaround I added to the answer.
|
0

This should work. Please find the testing as well.

$ cat > file1

I am a BILL boy

sed 's/[[:alnum:]   [:cntrl:]   [:lower:]   [:space:]   [:alpha:]   [:digit:]   [:print:]   [:upper:]   [:blank:]   [:graph:]   [:punct:]   [:xdigit:]]BILL[[:alnum:]   [:cntrl:]   [:lower:]   [:space:]   [:alpha:]   [:digit:]   [:print:]   [:upper:]   [:blank:]   [:graph:]   [:punct:]   [:xdigit:]]/BILLING/g' file1>file2

$ cat file2

I am a BILLING boy

3 Comments

This will work irrespective of the position of the occurrence of the word BILL in your file. @NeethuShaji
The txt file is having only BILL nothing else in that. I tried by giving an enter at the end of BILL and it worked... BILL was replaced with BILLING using the command sed 's/BILL/BILLING/g' Example.txt > Exam.txt
But the above code will not require you to put enter :) . @NeethuShaji
0

Using sed:

sed 's/\bBILL\b/BILLING/g' file

For inplace:

sed --in-place 's/\bBILL\b/BILLING/g' file

Comments

0

A little for loop might assist for dealing with multiple files, and here I'm assuming -i option is not available:

for file in $(grep -wl BILL /file_sys/file/*); do
    echo $file
    sed -e 's/\bBILL\b/BILLING/g' $file > tmp
    mv tmp $file
done

Here's what's happening:
  grep -w          Search for all (and only) files with the word BILL
  grep -l          Listing the file names (rather than content)
  $(....)          Execute whats inside the brackets (command substitution)
  for file in   Loop over each item in the list (each file with BILL in it)
  echo $file     Print each file name we loop over
  sed command   Replace the word BILL (here, specifically delimited with word boundaries "\b") with BILLING, into a tmp file
  mv command     Move the tmp file back to the original name (replace original)

You can easily test this without actually changing anything - e.g. just print the file name, or just print the contents (to make sure you've got what you expect before replacing the original files).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.