Sed insert text at first line of empty file

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed insert text at first line of empty file
# 1  
Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help?
# 2  
I think sed only works on lines. A zero-byte file has no lines, so that could be the problem.
# 3  
What are my alternatives? Examples?
# 4  
Why don't you use echo or is this a homework assignment?
# 5  
I don't want to use echo because I want the first line to be a comment, and the rest to be values that a script reads from. If the user deletes the commented line like they're not supposed to, i want the script not to fail and to replace the line. I want sed to say:
 sed -i 's/^$/#CUSTOM/' infile

but only to the first line. I don't want #CUSTOM to be all over the place where there's new lines.

my commands go in order: touch infile; sed (add #CUSTOM to beginning of this file). I don't want each iteration of the script to add #CUSTOM to the beginning. ONLY if line one is empty or does not contain #CUSTOM
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #637
Difficulty: Easy
As of January 2017, Apple's App Store had over 2.2 million apps for the iPhone.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

sed - insert text if column empty

Hi, I want to insert the text 'Unknown' in 2 specific columns in a csv file (actually | separated) if the column is blank. Its always the same columns. I have tried using sed: sed "s/||/|Unknown|/g" but there are occasion where other fields are blank and they need to be left blank. This... (4 Replies)
Discussion started by: ksexton
4 Replies

4. Shell Programming and Scripting

insert text into empty file

I have an awk script to extract data from several files and create output in the following format as a csv file: xxxx 01/04/12 0001 0 When data is present, I have a file. When no data is available in the input files, I would still like to create a file that looks like this: xxxx... (1 Reply)
Discussion started by: banjo25
1 Replies

5. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

6. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

7. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

8. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

9. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

10. Shell Programming and Scripting

SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed? (12 Replies)
Discussion started by: MBGPS
12 Replies

Featured Tech Videos