Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I understand that sed is a command to manipulate text file.

From my Googling, it seems -i means perform the operation on the file itself, is this correct?

What about '1d'?

share|improve this question
up vote 25 down vote accepted

In sed:

  • -i option will edit the input file in-place

  • '1d' will remove the first line of the input file

Example:

% cat file.txt 
foo
bar

% sed -i '1d' file.txt 

% cat file.txt        
bar

Note that, most of the time it's a good idea to take a backup while using the -i option so that you have the original file backup up in case anything goes wrong in the process.

For example, if you do:

sed -i.orig '1d' file.txt 

the original file will be kept as file.txt.orig and the modified file will be file.txt.

share|improve this answer
4  
You can also do a "dry run" without the -i to see what happens first, then use -i to actually change the file. – Baard Kopperud Jan 20 at 13:21

1. a )

sed '1d' file.txt

Prints the contents of file.txt; excluding the first line; to the standard output.



2. a )

sed -i '1d' file.txt

Prints the contents of file.txt; excluding the first line; back into file.txt; overwriting it's former self.

In some cases, you may be required to create a backup.


2. b )

sed -i backup.txt '1d' file.txt

Creates a backup of the original, before making changes.

This may be required.


2. c )

sed -i '' 'd' file.txt

Bypasses the backup process.



3. a )

sed '2d' file.txt

Prints the contents of file.txt; excluding the second line; to the standard output.
(Specifying any number will remove the corresponding line).

Also compatible with the -i flag.


3. b )

sed '1!d' file.txt

Prints the contents of file.txt; excluding all but the first line; to the standard output.
(In other words; only the first line gets printed).

Also compatible with the -i flag.


3. c )

sed '$d' file.txt

Prints the contents of file.txt; excluding the last line; to the standard output.

Also compatible with the -i flag.


share|improve this answer

In sed -h have:

  -i[SUFFIX], --in-place[=SUFFIX]
             edit files in place (makes backup if SUFFIX supplied)

and 'perform the operation on the file itself.' absolute it'is.

And man said: 'Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).'

as your question,

sed -i '1d' file_name

means: delete the first line in file "file_name" at place and backup to file. (just like edit file and delete first line directly. )

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.