yanachen
yanachen

Reputation: 3753

How to edit the head of the huge txt file in linux shell?

I tried to use vim to open the huge file to edit the head of it. I just want to add a line in the head. But opening it is a problem. Any good way to edit the head of it ?

Upvotes: 1

Views: 2895

Answers (4)

Pooja
Pooja

Reputation: 1280

Create file with headers only name it as 1.txt and place file in same directory as original file say for example your file is name as 2.txt

use cat command as:

cat 1.txt 2.txt > 3.txt

This will add headers from 1.txt and contents from 2.txt into 3.txt

Upvotes: -1

P....
P....

Reputation: 18411

sed '1 i\New HEADER' bigfile
New HEADER
line 1
line 2
line 3

Use sed -i flag to make changes persistent inside the file.

Upvotes: 1

CWLiu
CWLiu

Reputation: 4043

Since you don't give the input file, I would assume that the input as followed, and add the test string "head added!!" to the head of each line.

$ cat test 
line 1
line 2
line 3

$ awk 'NR==1{$0="tested line 1\n"$0}1' test
tested line 1
line 1
line 2
line 3

Modify "tested line 1"in the command awk 'NR==1{$0="tested line 1\n"$0}1' to your own input test

Upvotes: 1

agc
agc

Reputation: 8446

Using bash and sponge:

cat <(echo "This is the new line #1") bigfile | sponge bigfile

Upvotes: 1

Related Questions