Add new line at beginning and end of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add new line at beginning and end of a file
# 1  
Old 06-20-2014
Add new line at beginning and end of a file

Hi,

I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|".
I am looking for some command/option to join these two in single command parameter.

Sample file --
Code:
1
2
3
4

Expected output --
Code:
BEGIN
1
2
3
4
END

Thanks in advanced.

Last edited by vgersh99; 06-20-2014 at 11:43 AM.. Reason: code tags, please!
# 2  
Old 06-20-2014
Try:
Code:
{
  echo BEGIN
  cat yourfile
  echo END
} > newfile

# 3  
Old 06-20-2014
Simple method:-
Code:
echo "BEGIN" > /path/to/file/append_text
cat /your/path/to/yourfile/your_existing_file >> /path/to/file/append_text
echo "END" >> /path/to/file/append_text

# 4  
Old 06-20-2014
With sed:
Code:
sed '1 i\
BEGIN
$ a\
END
' file


Last edited by MadeInGermany; 06-23-2014 at 06:42 AM.. Reason: another newline after END - some sed versions need it
# 5  
Old 06-23-2014
Quote:
Originally Posted by MadeInGermany
With sed:
Code:
sed '1 i\
BEGIN
$ a\
END' file

Is it possible to have the entire command in single line?

Thanks for your time.
# 6  
Old 06-23-2014
Quote:
Originally Posted by bhupinder08
Is it possible to have the entire command in single line?

Thanks for your time.
Yes Smilie
Code:
{ echo BEGIN; cat yourfile; echo END;} > newfile

or
Code:
{ echo BEGIN; cat; echo END;} < yourfile > newfile

---- OR ----

Code:
awk 'BEGIN{print "BEGIN"} 1; END{ print "END" }' yourfile > newfile

GNU sed:
Code:
sed '1s/^/BEGIN\n/; $s/$/\nEND/' yourfile > newfile


Last edited by Scrutinizer; 06-23-2014 at 02:57 AM..
# 7  
Old 06-23-2014
Quote:
Originally Posted by bhupinder08
Is it possible to have the entire command in single line?

Thanks for your time.
Not with a standard sed. (I even found a sed version that needs another newline after the END.)
But a recent GNU sed takes
Code:
sed -e '1iBEGIN' -e '$aEND' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

2. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

3. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

4. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

5. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

6. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

7. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

8. Linux

Add file's date at beginning of every line in file

How would I do that? /Rutger (6 Replies)
Discussion started by: rutgerblom
6 Replies

9. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

10. UNIX for Dummies Questions & Answers

cat a file from end to beginning

Is there an option, for cat, head, tail, or is there any way, to display a file from last line to first? For example, my file looks like this: aaaa bbbb cccc eeee and I would like to print or display it like this: eeee cccc bbbb aaaa thanks (5 Replies)
Discussion started by: jpprial
5 Replies
Login or Register to Ask a Question