user2117336
user2117336

Reputation: 69

Changing a line in a file on UNIX

I have created a file named "asd.txt" on a UNIX based system.

I added four lines by using echo command.

Now, I would like to change the first line of this file.

I am not allowed to use any text editors, such as vi.

I have to do this by using only command line. Can anyone help?

Thanks.

Upvotes: 0

Views: 4845

Answers (2)

squiguy
squiguy

Reputation: 33360

Here is how you could do it with sed.

sed '1 s/search/replace/' asd.txt

If you are feeling up to it and have GNU sed, use the -i switch to do it in place.

If you want to replace the entire first line how about doing this?

echo "Here is my new first line" && sed '1d' asd.txt

For both of these commands you can redirect the output to a new file using the > operator.

Upvotes: 2

KAction
KAction

Reputation: 2017

#!/bin/bash
cat <(echo "Replacement") <(tail -n +2 foo.txt)

Upvotes: 0

Related Questions