How do I replace a string with another string in all files? For example, ~/foo directory has 100s of text file and I would like to find out xyz string and replace with abc. I need to to use sed or any other tool to replace all occurrence of the word under Linux and Unix-like operating systems. How do we replace words in files?

The sed command is designed for this kind of work i.e. find and replace strings or words from a text file under Apple OX, *BSD, Linux, and UNIX like operating systems. The perl can be also used as described below to replace a string with another string/word in all files.

How to use sed command to replace a string with another string

The syntax is as follows:
sed -i 's/old-word/new-word/g' *.txt
GNU sed command can edit files in place (makes backup if extension supplied) using the -i option. If you are using an old UNIX sed command version try the following syntax:
sed 's/old/new/g' input.txt > output.txt
The - option also know as replace in place. We can use old sed syntax along with bash for loop as follows:

[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
   sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
   echo "Error: Cannot read $f"
/bin/rm "$TFILE"

A Note About Bash Escape Character

A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). This is useful when you would like to deal with UNIX paths. In this example, the sed command is used to replace UNIX path “/nfs/apache/logs/rawlogs/access.log” with “__DOMAIN_LOG_FILE__”:

## Our path
## Escape path for sed using bash find and replace 
# replace __DOMAIN_LOG_FILE__ in our sample.awstats.conf
sed -e "s/__DOMAIN_LOG_FILE__/${_r1}/" /nfs/conf/awstats/sample.awstats.conf  > /nfs/apache/logs/awstats/awstats.conf 
# call awstats
/usr/bin/awstats -c /nfs/apache/logs/awstats/awstats.conf

The $_r1 is escaped using bash find and replace parameter substitution syntax to replace each occurrence of / with \/.

Replace one substring for another string in shell

Say you need to find ‘/foo/bar’ and replace with ‘/home/bar’. Here is what we do in sed:
sed 's+/foo/bar+/home/bar+g' input
sed 's+/foo/bar+/home/bar+g' input > output
sed -i'.Bak' 's+/foo/bar+/home/bar+g' input

We used the + instead of / as delimiter to avoid errors. See “how to use sed to find and replace text in files in Linux / Unix shell” for more info.

Replace strings in files with bash string manipulation operators only

No need to use the sed or Perl. We can replace the first occurrence of a pattern/string with a given string. The syntax is for bash version 4.x+:

# Find and replace all occurrences #

For instance, replace a string ‘Unix’ with ‘Linux’:

x="I love Linux"
echo "${x/Linux/Unix}"

Here is another bash string manipulation example that replaces all occurrences of ‘Linux’ with ‘Unix’:

message="I love Linux. Linux is awesome but FreeBSD is better too. Try it out."
echo "OLD: $message"
echo "NEW: $updated"

Bash Shell: Replace a String With Another String In All Files Using sed and Perl -pie Options
See “How To Use Bash Parameter Substitution Like A Pro” for more bash kung fu.

Replacing a string in a file in Bash variable when using sed

Often our shell scripts have variables, and we need to pass those variables to sed. For instance:

# Examples that shows how to use variables as replace and search values 
# using both sed and Bash 
sed -i'.backup' "s+${find}+${replace}+g" input.txt
## or test it on string ##
string='AWS config for shared storage location: /nfsroot'
sed "s+${find}+${replace}+g" <<<"$string"
## how about pure bash string manipulation with variables? ##
string='AWS config for shared storage location: /nfsroot'
echo "Current string: $string"
echo "Updated string: ${string/${find}/${replace}}"
Replace one substring for another string in shell script

How to replace an array of values using sed

Let us use the cat command to see our current template file that contains AWS RDS info config for PHP app:
cat config_template.txt
Herer is what I have:

define('RDS_EC2_DB_NAME', '__NAME__');
define('RDS_EC2_DB_USER', '__USER__');
define('RDS_EC2_DB_PASSWORD', '__PASSWORD__');
define('RDS_EC2_DB_CLUSER_IP', '__HOST__');

My task is to replace ‘__NAME__‘ and other place holder values using bash script:

# Build php config file for my app
echo '<?php' > "$output"
cat "$input" >> "$output"
# Define a new bash array. Format is:
# [search_string]=replace_value
declare -A rds_secrets
# Do find and replace on $output while reading
# AWS RDS connection info from rds_secrets() bash array
for s in "${!rds_secrets[@]}"
	sed -i "s/${find}/${replace}/g" "$output"
# Close PHP config file brackets
echo '?>' >> "$output"
How to Use sed to Find and Replace String in Files

How to replace a string in a File in Perl

The perl -pi -e syntax to find and replace a string is as follows:
perl -pi -e 's/old-word/new-word/g' input.file
perl -pi -e 's/old/new/g' *.conf


  • -p : act like sed
  • -i : edit file in place
  • -e : One line Perl programe

When Perl executed, it will look all files matching *.conf. Then it will run the replacement pattern ‘s/old/new/g‘. In other words, we used Perl regular expression to eplaces all occurrences of old with new.

Summing up

In this tutorial, we learned how to use sed, bash, and Perl to find and replace strings in a single or bunch of files. See man pages for more info:

man bash
man sed
man perl
man perlre

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 66 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
66 comments… add one
  • Jeff Schroeder Nov 27, 2007 @ 19:36


    Glancing at this I see a typo:
    sed “s/$OLD/$NEW/g” “$f” > $TFILE && mv TFILE “$f”

    Should be:
    sed “s/$OLD/$NEW/g” “$f” > $TFILE && mv $TFILE “$f”

    Also, you didn’t seem to mention that sed can auto-create backup files using syntax like this:
    sed -i.bak ‘s/old-word/new-word/g’ filename.txt

    That will edit filename.txt and copy the original to filename.txt.bak

  • 🐧 nixCraft Nov 27, 2007 @ 19:44


    thanks for the heads up!

  • Bhargav Dec 3, 2007 @ 7:25


    I don’t know normal sed has -i.bak option but perl provides the backup of the original file.

    perl -p -i.bak -e ‘s/AAA/aaa/’ file_name

  • Stijn Gruwier Nov 2, 2009 @ 13:25

    Just a small remark:
    perl -pie ‘s/old-word/new-word/g’ input
    modifies the input file, so no need for ‘ > output’ like with sed.

  • Eric Dec 18, 2009 @ 8:34

    Thanks for the great tip. Heres how I did it using a pipe to provide file paths:

    find . -name '*.xaction' | xargs sed -i 's/test-data/live-data/g'

  • yazid erman Jan 25, 2010 @ 12:07

    Thank you, this was useful!

  • Matthew Feb 4, 2010 @ 14:08

    Recursively replace a word in multiple files in multiple directories?
    I couldn’t find a recursive option in sed, so …

    grep -ilr ‘old-word’ * | xargs -i@ sed -i ‘s/old-word/new-word/g’ @

    Which will find each file with the old-word in it, complete with path, then pipe through xargs and run sed.
    Does the trick.

    • Mike Jul 31, 2011 @ 19:56

      @Matthew, that worked perfectly for recursive find and replace. Thanks.

    • Todd Cesere Mar 8, 2012 @ 17:05

      I had to do about 5 things to make this work in OSX Snow Leopard. NOTE THAT COPYING DIRECTLY MIGHT NOT WORK. This is because the single quote characters could turn into curly quotes after posted. What I ended up with is

      grep -ilr ‘old-word’ * | xargs -I@ sed -i ” ‘s/old-word/new-word/g’ @

      1. Curly single quotes had to become straight single quotes.
      2. Lowercase -i doesn’t appear in xargs in this OSX version, had to use uppercase -I (and spent a long time trying to figure out that @ wasn’t the problem because it didn’t have some special meaning).
      3. OSX didn’t like the missing backup suffix, it interpreted this to mean that replacement pattern was the suffix and that @ was the command. Now that I think of it, how does any OS get that the pattern isn’t the suffix? Maybe the those were accent marks that were changed into curly single quotes?

      Ok, only three things, but it felt like an eternity trying to figure it out. Actually, it was probably over an hour.

      • Wilmer Henao Apr 6, 2013 @ 0:02

        Well… thanks for your post. Your hour made it like a 5 min. work to me. Seems like ubuntu has the same issue. I just had to replace the curly apostrophes ;)

    • Jon Sep 4, 2014 @ 22:28

      Thanks Mathew

  • Joe Feb 25, 2010 @ 22:37

    I’m a linux newbie.
    I’m trying to change a string which is a windows path file (e.g. C:\Documents and Settings\path) into a unix path-file (e.g. /home/path). The file containing the string I want to change is a textual file (a bibtex file: file.bib). This files containes the addresses of other files, and they appear as: “C\:\\Documents and Setting\\path\\”). I’m trying with the “sed” command but apparently unix do not like the “\\”. Someone knows the solution?

  • Suvankar Mar 30, 2010 @ 7:58

    This wont work when I try to use it for replacing a work having “/” in it.
    I use a script to change password for other machine. But once it a while the problem comes like
    New password is “dkCHQ4/uTfSIU” old paswd “14eMXYEwUD7Cc”

    sed ‘s/14eMXYEwUD7Cc/dkCHQ4/uTfSIU’ /etc/passwd > $newpassfile
    it throws error.

    • Estêvão Samuel Procópio Dec 4, 2011 @ 21:47

      You can change the / from the sed option to any char you want. Try this

      sed ‘s|14eMXYEwUD7Cc|dkCHQ4/uTfSIU’ /etc/passwd > $newpassfile

      I think it’s better to understand then escape all the ‘/’s in the string like Mattheu suggested.

  • Matthew Mar 30, 2010 @ 14:06

    @Suvankar, you need to escape backslashes with forward slashes.

    sed ’s/14eMXYEwUD7Cc/dkCHQ4\/uTfSIU’ /etc/passwd > $newpassfile

  • daljeet Aug 26, 2010 @ 12:47

    i need to remove the SOP in the code. My code contains a call to another function immediately after SOP call. please suggest how can i find the first occurance of ‘;’ in the line and remove the text till first occurance of ‘;’

    System.out.println(“06”); String sDebitTxnmemo= cm.getString(“********”);

  • NIM Aug 27, 2010 @ 2:43

    replace OldString NewString — *.txt

  • Robert Baldwin Oct 5, 2010 @ 21:42


  • Nahas Oct 15, 2010 @ 17:01

    i want search for a word in a file and if found ,i want to add a word at the begning of that line where the word is found….. is there any command for that?

  • captnrexx Nov 26, 2010 @ 12:41

    Hi Nahas,
    something like this will do the job

    cat the.file|perl -pne ‘s/^(thewordtofind)/TheWordToAdd$1/’

  • ashraf Jan 17, 2011 @ 12:40


    I have a template file, and would like to replace some words in that file with words taken from another file like:
    Hi $1 I miss being with you at $2
    Johan London
    So the output will be a new file contains
    Hi Johan I miss being with you at London.

    • 🐧 nixCraft Jan 17, 2011 @ 14:24

      Try sed

      sed -e "s/\$1/$var1" -e "s/\$2/$var2" file1 > file3

      You can read data into variable using cut, sed, awk, and any other shell builtin:

      data="$(cat file2)"
      set -- $data
      sed -e "s/\$1/$var1" -e "s/\$2/$var2" file1 > file3
  • Sinoy Xavier Mar 9, 2011 @ 6:43

    Hi All,
    We had a situation to find and replace a word in huge number of files. But if we create a new file with the sed command, it may create space issue. So we are asked to remove the old file once this replacement happens and retain only the modified file.
    You may make use of the following script:
    > vi

    OLD="No State"
    for f in `ls \home\Folder\FileName*.csv`
       echo "processing $f `date`" >>Change.log
       sed "s/$OLD/$NEW/g" $f > 1.csv && mv 1.csv $f
       echo "processed $f `date`" >>Change.log

    > nohup sh (run this in background)
    > tail -f Change.log (To track changes)

  • chris Mar 9, 2011 @ 11:05

    Thank you for these useful tips. Same question as Suvankar with a twist:
    sed -i ‘s/$A/$B/g’ f.txt
    throws an error.
    I tried escaping with A=\/path1 to no avail.
    Any further tip for such a case? Thanks
    PS Also, what is the difference between using single and double quotes above? Thanks again.

    • 🐧 nixCraft Mar 9, 2011 @ 11:23

      sed -i ‘s/$A/$B/g’ f.txt

      Should be as follows:

      sed -i "s/$A/$B/g" f.txt

      You need to escape / so that sed will not confuse with s/ command. See the difference between single and double quote here:

      a] Enclosing characters in single quotes preserves the literal value of each character within the quotes
      b] Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘`’ retain their special meaning within double quotes.

  • chris Mar 10, 2011 @ 11:51

    That works perfectly (even with spaces in the path). Thank you.
    So the single quotes in A=’\/path1′ prevent interpreting the escape character until it is used within double quotes, right?

    Now my script has matured a bit. Here is where I am at. This attempts to delete files in a tree if they are absent from another directory tree.

    ########## Try to declare path variables in various ways
    A='\/tank\/tests 1\/a'
    B='\/tank\/tests 1\/b'
    AA="/tank/tests 1/a"
    BB="/tank/tests 1/b"
    ########## This works, but most paths are hard-coded
    echo \# Running this file will recursively erase files and directories in  > "/tank/tests 1/b/diff.txt"
    echo \# $BB                                                               >> "/tank/tests 1/b/diff.txt"
    echo \# and not in                                                        >> "/tank/tests 1/b/diff.txt"
    echo \# $AA                                                               >> "/tank/tests 1/b/diff.txt"
    diff -r "/tank/tests 1/a" "/tank/tests 1/b" | grep "/tank/tests 1/b" >> "/tank/tests 1/b/diff.txt"  # lis differences
    sed -i "s/Only in /cd \"/" "/tank/tests 1/b/diff.txt"         # replace    Only in   with   cd "
    sed -i "s/: /\" \&\& rm -r \"/" "/tank/tests 1/b/diff.txt"    # replace    :         with   && rm -r "
    sed -i "/#/!s/$/\"/" "/tank/tests 1/b/diff.txt"               # add final  "         except for comments
    ######### This is one of many attempts to use soft-coded paths
    #outfile="/tank/tests 1/b/diff.txt"
    #diff -r $AA $BB | grep "$BB" >> $outfile
    #sed -i "s/Only in /cd \"/g" $outfile

    Any clue how to soft-code the paths? I have run out of ideas to try.
    Thanks again.

  • Shameel May 19, 2011 @ 9:53

    I have a problem could anyone tell me a way to open the access.log file which does not have permission to read or write. I need to read that file for monitoring purpose. I cannot use chmod command to change the permission as it says permission denied. I came to know that there is a shell command to open it. It will be great help if anyone give any idea in perl or php to read that information.

    thanks in advance

  • erik Jul 27, 2011 @ 6:58

    Use sudo chmod. If you don’t have super user access, then you might not be able to open this file.

  • rahul joshi Aug 12, 2011 @ 10:05

    I could not understand the following line can u explain its functionality.

    if [ -f $f -a -r $f ]; then //explain only this one whole script are below

    [ ! -d $BPATH ] && mkdir -p $BPATH || :
    for f in $DPATH
    if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
    sed “s/$OLD/$NEW/g” “$f” > $TFILE && mv $TFILE “$f”
    echo “Error: Cannot read $f”

  • Vick Aug 12, 2011 @ 21:40


    I have a dictionary file:

    And on the processing file, i have:

    Need to replace values in processing file with corresponding values from dictionary file. e.g. sme -> replace

    Would appreciate on this.


  • Tarun Aug 13, 2011 @ 20:24

    Hi All,
    I am facing an issue wherein i need to replace a word with another one. But my case is a bit different here. Only a part of the word is constant which I know. The other half is variable ( too many possibilities ). I need to change the complete word with a new word. For example
    The possible old words may be
    abc123ad, abc123bc, abc123ak and so on….
    i need to replace any of the above in my file with a new word.

    Please help :(.
    Thanks in advance!!!

    • Tarun Aug 13, 2011 @ 20:35

      I guess i have found it…..
      sed ‘s/abc123[aa-zz]*/new_word/g’ filename

  • Dennis Gearon Sep 5, 2011 @ 4:46


    If any of the files fed through xargs are binary, both sed and perl will change the timestamps of the files that all have the string, binary files or text files, but no changes are made (in at least the one file *I* was looking at at the top of the file directory hierarchy)

    So filter out binary files.

  • Sameer Sep 23, 2011 @ 16:05

    Hi All,
    I want to replace a string in a file and redirect the output to a new file,but this is slightly tricky.The input file contains data as A|B|C|D|E|F|G|H|I|K , now i want to replace alphabet “F” to “:” and redirect the output file to a file.The final output must look like this:A|B|C|D|E|:|G|H|I|K. Please help it’s urgent

  • Ty Sep 30, 2011 @ 23:30

    It means i create a database using mysql and create a website using joomla so when i click a link from the web i can directly retrieve the data from mysql server.

  • Mohamed Youshah Oct 23, 2011 @ 11:52

    I need help immediately. I need to monitor multiple web servers access.log file. I have completed the coding for single server completely but the problem we are facing is let us imagine two servers and The server has database now how to fetch the information from being in that server and insert in that database. I want to create a link between two servers that it does not ask for password everytime fetch the data by itself as i am running cron job. I have access to both the server as i am the admin of it. I am doing coding in php.

  • inukaze Nov 4, 2011 @ 3:56

    Ok , sorry for this stupid question but someone can tell me how i can “delete” “/home/” for example i use

    echo $HOME

    i need to delete “/home/” to get “username” without “whoami” or something like that

    • Red Apr 5, 2012 @ 11:15

      u can use basename $HOME

      • Red Apr 5, 2012 @ 11:16

        Sorry for the delayed reply :P dint check da date

  • Anderson Venturini Nov 11, 2011 @ 23:49

    Great tutorial! Worked 100%! Thanks a lot!

  • Stuart Jan 25, 2012 @ 17:47

    please can someone help me with the following?

    I have a text file which i have produced which is a count of how many times a pattern has occurred. This looks like this:

    network_errors 3
    vfstab_errors 2
    fs_errors 10

    I want to write a loop that extracts the results 3,2,10 and appends this result to the relevant entry in a csv file.

    the csv file looks like this:


    I want to be able to do append 3,2,10 to the entry in cvs file so in this example it would look like:

    Please note though the count file I am using as input will have different results each day so I want to be able to append the result to the csv file whatever the value is

    Any help would be greatly appreciated. Thanks

    • Morg. May 16, 2012 @ 10:51

      wrong tool for the job mate, use a db. If you still want to do it like that … open file, split by line, foreach line split by space, then replace ‘(firstpart).*’ with ‘$1(,secondpart)’ in your summary file.

      Still a very bad idea as your data will not be easy to extract as it’s not even proper CSV.

      Much better but still bad would be having a correct csv file like this >

      Then you can have a much simpler update script, with no replace or whatever, just get the d* out of each line of your file and add commas.

  • Stuart Jan 25, 2012 @ 17:50

    I think this could be done in a loop and using sed to append the relavent results to each line in the csv file but I am not too sure how to do it

  • Bhakar Hazarika Feb 24, 2012 @ 7:38

    Hi All

    I have got a file in dis format (tabular format in rows and columns)

    1 2 3 4 5
    aa aa aa aa aa
    bb bb bb bb bb
    cc cc cc cc cc

    I want 2 replace a particular column value with a special character
    For ex i want 2 replace column 2 first character with $
    output file:

    1 $ 3 4 5
    aa $a aa aa aa
    bb $b bb bb bb
    cc $c cc cc cc

    Thanx in advance

  • Bhakar Hazarika Feb 24, 2012 @ 7:41

    Hi All

    I have got a file in dis format (tabular format in rows and columns)

    1 2 3 4 5
    aa aa aa aa aa
    bb bb bb bb bb
    cc cc cc cc cc

    I want 2 replace a particular column value with a special character
    For ex i want 2 replace column 2 first character with $
    output file:

    1 $ 3 4 5
    aa $a aa aa aa
    bb $b bb bb bb
    cc $c cc cc cc

    Thanx in advance

    Hi All

    I have got a file in dis format (tabular format in rows and columns)

    1 2 3 4 5
    aa aa aa aa aa
    bb bb bb bb bb
    cc cc cc cc cc

    I want 2 replace a particular column value with a special character
    For ex i want 2 replace column 2 first character with $
    output file:

    1 $ 3 4 5
    aa $a aa aa aa
    bb $b bb bb bb
    cc $c cc cc cc

    Thanx in advance

  • Kenny Hendrick May 30, 2012 @ 23:44

    You made it too easy.

    Thanks for taking the time to share this.

  • Pet Jun 4, 2012 @ 9:36

    Hi all!
    I have following problem: I need to replace string containing specified letters (e.g. May12) in one file, using string from another file containing same letters (May12).
    Any1 got ideas how it can be done?
    Thx in advance.

  • S Singh Jun 12, 2012 @ 16:51

    Hey how can i implement it for [dot]sql files?
    Also it would be a great help if someone can help me implement it with [dot]asp files in windows…
    Thanks in advance

  • Venkatesh Jun 19, 2012 @ 11:07

    Can any one help me on this..

    write a script that detects when Full GC starts in Ven file.
    Once Full GC starts,
    the script should start capturing in a separate file, about the list of objects being scanned and memory occupied by them, in docs.log or cach.log file; till Full GC completes its execution.

  • inquisitive Aug 15, 2012 @ 18:09


    And thank you for putting up a nice info page for all the newbies, like me (and not only).
    In addition to all the comments, there is a problem I’m facing. I’ll simplify and try to make it clear:

    – There are a ton of text files in recursive folders.
    – All of them have lines of text (EOL doesn’t matter) — imagine a large poem.
    – Many of them have certain lines containing some strings — let’s say OLD1 and OLD2.

    What needs to be done is to search all the files for the occurence of OLD1 and OLD2, replace it with NEW1 and NEW2 but(!), in addition, for every OLD1 replaced by a NEW1 there has to be added a new line below the second containing the string BLA.

    I bet it’s confusing so I’ll give a small example:

    This is the old file:

    aaa OLD1 sss
    qqq www eee
    zzz OLD2 xxx

    This is the new file, after replacing and adding:

    aaa NEW1 sss
    qqq www eee
    zzz NEW2 xxx

    I hope I managed to explain. If you can help me, it would be really great.
    Thank you in advance for any possible try.

  • Venky Sep 3, 2012 @ 6:13

    I have a question?
    what is the mechanism of script to execute as generic UIDs. If we execute as root will it cause anything..
    Please reply fast i’m in urgent.

  • Arvan Apr 9, 2013 @ 21:54

    Hello Experts,
    Need your quick help to tune the linux script. Where i am trying to replace the text from inside the file contain. which is like below example.
    But when $NEW conatins like (‘)…. etc then i am getting some error

    ./ line 3: syntax error near unexpected token `(‘

    But while passing simple string its getting successfully. Please refer the below linux script. Please guide me asap. Appreciate your help.
    for f in `ls *.kbs`
    echo “processing $f `date`” >>Change.log
    sed “s/$OLD/$NEW/g” $f > 1.kbs && mv 1.kbs $f
    echo “processed $f `date`” >>Change.log

  • Iura Jun 2, 2013 @ 14:09


    How to replace string using regex in gzipped files? How to organize multithread process for this task? I would like replace text in few bigger gzipped files.

  • Dennis gearon Jun 2, 2013 @ 17:47

    Because it is zipped, you only real choice is to unzip it.
    You CAN extract a single file, make changes, and ‘update’ it, ‘replace’ it. BUT what tar REALLY does is just add it to the end. The tar program then uses ALL versions of a file for any operation, and the LAST one overwrites the others. So your archive can get REALLY large, if you go through a lot of the files.

    The recommended way is to totally extract it, make changes, re-tar/zip it.

  • Iura Jun 3, 2013 @ 2:34


    I have one solution, but I don’t know how to use it to process files in multithreading mode.
    gzip -dc old_file.gz | head -100 | | gzip > new_file.gz

    Each gzip file contains one text file. I have less 20 files. File size in GZIP is 1 GB, and file size in UNGZIP is 20GB.

    Can you help me?

  • Dennis gearon Jun 4, 2013 @ 2:02

    why are you choping the last 100 characters off of the file? What is you rprogram ‘’ do? This page is about replacement of strings in side of files. Is that what you are doing? About multithreading, the only two things I know about multithreading relative to this is: A/ There are several programs that do multithreading compression. If you want gzip, though, you probably have to use B/ Multithreading compressing on a laptop CAN and often DOES cause them to overheat and crash. Personal experience.

    When I backup the raw partition of my laptop and compress it, I max out the memory speed, the four cores, and the hard drive speed. It crashes in 30 to 120 minutes. I have to only use 2 of the four cores, and have recently cleated the heatsink of lint if I want multithreading to work.

  • Iura Jun 4, 2013 @ 19:18

    This is example of regular expressions which I would like to use.

    	print STDOUT $file_line;
  • swaminath Jul 21, 2013 @ 11:05

    thanks a lot

  • Ajit Aug 9, 2013 @ 11:06

    I have multiple files in a direcotry which contain a string like enduserchk-End User Check. Test the DSL, ATM layers from the TAM to the user.—–;
    I wont to delete this string from the files.There are around 500 files like this.Can some one tell me how this can be done using a script.This files are on aix machine

  • hala Jun 10, 2014 @ 3:28

    hi everybody
    how can i replace between : jsakhd : in multiple files using sed or anything else
    thanks a lot

  • YnG Sep 29, 2015 @ 18:04

    If you want to replace strings with “/” slash sign type them as \/ (backslash slash).

  • Ali Oct 27, 2015 @ 21:00

    I would like to replace a old line with new line, in a file using sed, can someone help here?

    old line:- /usr/bin/enaix64 -d

    new line:- /usr/bin/enaix64 -d -p /usr/bin -l 4445

    from a file test123

    I would like to use sed instead vi

    sed XXXXXXXX test123 –>like this

    • Donald Jan 28, 2021 @ 11:50

      I would approach this in two steps since your “source” string is contained in the “target” string.

      First sed replace to something that doesn’t appear in your text eg. “@#_NOTHEREIPROMISE”
      and then sed replace “@#_NOTHEREIPROMISE” to your final text.

  • Vinay Jan 13, 2016 @ 22:59

    I would need some help in parsing this string as follows:

    Variable “COLUMNS_LIST” is assigned as follows:


    Desired Output is:
    ‘CD’, ‘NM’, ‘GRP_CD’, ‘GRP_NM’

    Appreciate the help!


  • zccqzy Jan 29, 2021 @ 7:18

    Is that a typo?
    man perlre
    must be
    man perl

    • /dev/ttyS0 Jan 29, 2021 @ 7:50

      Naa. That is a Perl regular expressions man page. Why not try it out on your system? It is not that hard and you could have avoided typing comment ;)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum