What is the easiest way to add a string on the beginning of every line of the file from the command line?

511

Solution 1

You can use sed:

sed -i 's/^/your_string /' your_file

Thanks to Stephane and Marco's comments, note that the -i option isn't POSIX. A POSIX way to do the above would be

sed 's/^/your_string /' your_file > tmp_copy && mv tmp_copy your_file

or perl:

perl -pi -e 's/^/your_string /' your_file

Explanation

Both commands perform a regex substitution, replacing the beginning of a line (^) with your desired string. The -i switch in both commands makes sure the file is edited in place (i.e. the changes are reflected in the file instead of printed to stdout).

sed should be available on any POSIX-compliant OS and perl should be available on most modern Unices except perhaps for the ones that have gone through the effort of removing it.

Solution 2

:|paste -d'foo ' - - - - input > output

(just kidding, though you'll probably find it's the fastest of all the solutions posted here :-b).

The canonical way is:

sed 's/^/foo /' < input > output

However, it's not easily adapted to arbitrary strings. For instance,

sed "s/^/$var /"

Only works if $var doesn't contain, &, \, / nor newline characters.

In that regard,

export var
awk '{print ENVIRON["var"], $0}'

or

perl -pe '$_="$ENV{var} $_"'

would work better.

Solution 3

You can avoid the problems of in-place editing with the stream tools by using a tool that normally does in-place editing - an editor!

ex sample.txt -c "%s/^/foo /" -c wq

There is an additional advantage that the commands are easy and obvious to anyone who is versed in the one true editor.

Solution 4

I present a solution using awk prepending the string “foo”.

awk '{ print "foo", $0; }' input > output

awk is cross-platform and available on any POSIX system. It does not do in-place editing. If you want to edit a file without creating a second one, you will have to use a temporary file. See Joseph's sed answer, it shows the syntax. Another hack is to use the following syntax, which is basically creating a temporary file with the same file name as the original file.

{ rm file; awk '{ print "foo", $0 }' > file; } < file

Solution 5

You can use perl to do this:

$ perl -pi -e 's/^/mystring /' afile.txt

Example

Create a sample file.

$ seq 5 > afile.txt

$ cat afile.txt
1
2
3
4
5

Run the above command:

$ perl -pi -e 's/^/mystring /' afile.txt

$ cat afile.txt
mystring 1
mystring 2
mystring 3
mystring 4
mystring 5
Share:
511

Related videos on Youtube

Frank
Author by

Frank

Updated on September 18, 2022

Comments

  • Frank
    Frank over 1 year

    In a file I have this code to set some cookies

    setcookie("token", "value", time()+60*60*24*100, "/");
    setcookie("secret", "value", time()+60*60*24*100, "/");
    setcookie("key", "value", time()+60*60*24*100, "/");
    

    I want to know how I can check if these cookies were set on the same file, preferably just after they're set. I have tried this

    if(!isset($_COOKIE['token']) || !isset($_COOKIE['secret']) || !isset($_COOKIE['key']){
    
    //do something
    
    }
    

    but it doesn't work..

    • Mark Baker
      Mark Baker about 12 years
      Cookies are only set when they are sent back to the browser, and can only be tested by PHP when the browser issues a new request... so "No! they don't exist in the script in which they were set"
    • AndrewR
      AndrewR about 12 years
    • Marc B
      Marc B about 12 years
      Superglobals are set when the script is started up, then PHP does NOT change them to reflect anything you've done within the script.
  • Frank
    Frank about 12 years
    Is there any way I could see if the cookie was set then?
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    sed -i is not POSIX. It's GNU. FreeBSD sed has a similar option, but you need sed -i '' there.
  • Jeff Hewitt
    Jeff Hewitt over 10 years
    @Marco I'm aware that -i creates a temporary copy (that's why I said the changes are reflected in the original file); I meant that you get the same semantics as in-place substitution. Please check that the updated answer is POSIX-compliant.
  • Rahul Patil
    Rahul Patil over 10 years
    Why it's working faster than awk ?
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    It's only going to work faster than awk on very small files. The behavior varies across echo implementations and it strips leading and trailing blanks and processes backslashes specially.
  • Rahul Patil
    Rahul Patil over 10 years
    that's something interesting... :P
  • Rahul Patil
    Rahul Patil over 10 years
    @StephaneChazelas Yes. you are 100% correct just tested... paste.ubuntu.com/6210934
  • Matt
    Matt over 10 years
    prefix () { while IFS= read -r REPLY; do printf "%s%s\n" "$1" "$REPLY"; done; } ought to be more correct.
  • kurtm
    kurtm over 10 years
    Perl seems to be universal nowadays, but for a long time it wasn't. And I'm betting there are a couple rare UNIX variants out there that steadfastly refuse to have perl as part of the base. Something to be aware of.
  • kurtm
    kurtm over 10 years
    reducto# wc -l foo ` 914 foo` reducto# { rm foo ; awk '{ print "prepend" , $0 }' > foo } < foo > This doesn't seem to work. I was trying because the redirecting to original made me nervous, but it won't even start.
  • terdon
    terdon over 10 years
    Why the {}? I think @Matt was suggesting you make it into a named function, what's the point of {} without a function name?
  • terdon
    terdon over 10 years
    @kurtm see the discussion here. Apparently, AIX does not have Perl by default and nor do embedded systems.
  • kurtm
    kurtm over 10 years
    @terdon Yeah. I suspected AIX wouldn't, but I haven't really worked with AIX, so couldn't say for sure. And yeah, perl is a bit bloated for embedded systems. That's why I put the warning on there. I remember the days when perl was only ever an add-on...
  • David Sainty
    David Sainty over 10 years
    Perl is not at all universal. It's popular in Linux distributions to include it in the base, but that's about it.
  • kurtm
    kurtm over 10 years
    @DavidSainty The BSDs also seem to include it and I'm fairly certain Solaris does too (I'm not touching Solaris any more, so I'm not checking). But that's why I said "seems".
  • slm
    slm over 10 years
    @DavidSainty - it's universal enough. The Q was tagged with Bash so I find it extremely hard to imagine a system that has Bash but not Perl. At any rate, here's one of 5 answers, several of the others show how to use sed, I showed how to use Perl....not looking for a holy war on the matter.
  • David Sainty
    David Sainty over 10 years
    @kurtm which BSDs have Perl in the base distribution?
  • kurtm
    kurtm over 10 years
    @DavidSainty Apparently only OpenBSD. I guess I made the assumption since the Open folks tend to be conservative about what they allow in base. OpenBSD definitively does have it in base.
  • Keith Thompson
    Keith Thompson over 10 years
    Strictly speaking, the changes are reflected in a new file with the same name as the original.
  • Keith Thompson
    Keith Thompson over 10 years
    @slm: "I find it extremely hard to imagine a system that has Bash but not Perl". I have several such systems sitting on my desk. (They happen to be embedded systems.)
  • Keith Thompson
    Keith Thompson over 10 years
    @sim: It's bash 3.1.17, at least on the one I'm logged into at the moment.
  • slm
    slm over 10 years
    @KeithThompson - Yeah this is a pretty ancient system, it's almost a 10 year old release, tldp.org/LDP/abs/html/bashver3.html. Shall I qualify my statement that I can't imagine a system with a release of Bash in the last 5 years 8-).
  • Jakob Bennemann
    Jakob Bennemann over 9 years
    Note that since the user is asking about every line of the file, you could replace 1,$ with simply %. This makes the command :%s/^/string/.
  • mikeserv
    mikeserv almost 9 years
    @slm - i don't think there were too many embedded systems - at least not the kind you'd keep on your desk - capable of running a bash - v3 or otherwise - 5 or 6 years ago. However, there are many systems - even built today - which have a bash v3 installed by default (like Mac OSX), because later releases are GPL v3 which is very unfriendly to to commercialization - especially of the embedded variety - of any kind (some people say GPL v3 woldn't have happened at all were it not for TiVo). And so I doubt Keith's devs were so ancient as all that.