Sunday, June 28, 2009

How to add a word or letter at the end of each line in shell script

Let's use student.txt as below and now we want to add "Std_ID;" on each line in the student.txt of course without double quote.
$ cat student.txt
024434
022403
021437

1)Using sed -i:

$ sed -i 's/$/ Std_ID;/' student.txt

The s means substitute
The $ in this particular regex (regular expression) means end of the line.
The " Std_ID;" is the substituting word.

$ cat student.txt
024434 Std_ID;
022403 Std_ID;
021437 Std_ID;

2)Simple sed command.
$ cat student.txt
024434
022403
021437

$ sed 's/.*/& Std_ID;/' student.txt
024434 Std_ID;
022403 Std_ID;
021437 Std_ID;

3)Using awk:
$ cat std.txt
024434 Std ID
022403 Std ID
021437 Std ID

$ awk '{print $0," Std_ID;"}' student.txt>std.txt

$ cat std.txt
024434 Std_ID;
022403 Std_ID;
021437 Std_ID;
Related Documents
How to add a line to the first in a file using shell script

No comments:

Post a Comment