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 ?
Asked
Active
Viewed 2,386 times
1
-
1Check if this helps https://superuser.com/a/121424/728737 – Abhi Jun 16 '17 at 06:47
-
1Similar question: https://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file – Gov Jun 16 '17 at 06:47
-
2Place the line in a new file and append the existing one using shell redirection which can also append (`>>`). That way you don't have to open the file in an editor at all. – arkascha Jun 16 '17 at 06:48
-
Why to use pipe or use subshell when sed can do it without all that clutter. – P.... Jun 17 '17 at 08:03
4 Answers
1
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

CWLiu
- 3,913
- 1
- 10
- 14
1
Using bash
and sponge
:
cat <(echo "This is the new line #1") bigfile | sponge bigfile

agc
- 7,973
- 2
- 29
- 50
-
@yanachen, Click the URL in my answer, or install the `moreutils` package, (*i.e.* in *Debian/Ubuntu/etc.* run `apt install moretutils`.) – agc Jun 16 '17 at 07:10
1
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.

P....
- 17,421
- 2
- 32
- 52
0
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

Pooja
- 1,254
- 14
- 17