Tuesday, June 23, 2009

How to add a line to the first in a file using shell script

There are many ways and techniques to add a line to the first inside a file using shell script. In this post I will discuss some ways.

Let's start with a file to which we will do operation. Name the file as student_info.txt and it contains following data.
$ cat >student_info.txt
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

We are making a copy of student_info.txt so that orginal file is intact and we can play with copying file.

$cp student_info.txt student.txt

Each time we will test on student.txt and then before starting to test another way we will overwrite student.txt by student_info.txt

Note that after cat and ">" sign you can insert data and whenever you finish just press CTRL+D.
Now in the first line of the student_info.txt file I like to add header information in the way, "StdID|Full Name|Department". Several techniques are shown below.

$ cat student.txt
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

1)Using echo, cat, mv method:
This is the simplest method to do the job. In a new file write one line and then append the contents of original file to the new file. Move new file to original one.

$ cat first_method.sh
echo "StdID|Full Name|Department" >student_temp.txt
cat student.txt>>student_temp.txt
mv student_temp.txt student.txt

$ ./first_method.sh

$ cat student.txt

StdID|Full Name|Department
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

2)Using sed command:
$ cp student_info.txt student.txt

With inline based sed command you can either directly modify within file and insert a line to the first or you can take a backup before modifying.

Directly add a line to the header using sed by,
$ sed -i '1iStdID|Full Name|Department' student.txt

$ cat student.txt
StdID|Full Name|Department
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

Before adding line you can create backup file of the original one named student.txt~ using,

$ cp student_info.txt student.txt

$ sed -i~ '1iStdID|Full Name|Department' student.txt


$ cat student.txt~
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

$ cat student.txt
StdID|Full Name|Department
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

3)echo, cat, mv in one line:
$ cp student_info.txt student.txt


Note that if with the cat option no filename is provided, or when FILE is -, then it read standard input. So writing echo "StdID|Full Name|Department" |cat - student.txt will be the desired output and then we redirect output to a file and then move temp with original one.

echo "StdID|Full Name|Department" |cat - student.txt>student.tmp && mv student.tmp student.txt

$ cat student.txt
StdID|Full Name|Department
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

4)Using perl -i:
With perl -i command you can do the job easily. Note that if you use .bk then it will backup your original file.

perl -i.bk -pe 's/^/StdID|Full Name|Department\n/ if($.==1)' student.txt

5)Using awk:
$ cp student_info.txt student.txt


$ cat student.txt
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

$ awk 'NR==1{print "StdID|Full Name|Department"}1' student.txt>student.tmp

$ cat student.tmp
StdID|Full Name|Department
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

5)Using echo -e and cat command:
$ echo -e "StdID|Full Name|Department\n$(cat student.txt)">student.txt

$ cat student.txt
StdID|Full Name|Department
024401|Mohiuddin Rafi|CSE
024201|Mohammad Raju|MCE
024101|Mohammad Jamil|EEE

Related Documents

No comments:

Post a Comment