Inserting text into a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting text into a new file
# 1  
Old 08-24-2012
Inserting text into a new file

Hi all,

I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template.

Can someone tell me why this won't work,
Code:
touch $1 | sed -e '1i\Some test code here'

Sorry I'm quite new to all this!

Just as a side question, I know that $ is the regex for the last line, but what is the symbol for the first line in a file?

Thanks in advance!


Andrew
# 2  
Old 08-24-2012
Because the pipe "pipes" the output of one process to the input of another. In this case, touch $1 will create/modify time stamp of the file and will not produce any output to be used by the other (sed) command/process.

Why don't you try cat with here-string? (I am suggesting this as replacement for your command line).
cat >> $1 <<<"Some test code here"
# 3  
Old 08-24-2012
You can just use echo to write to a (new) file:

Code:
echo "Some test code here" > filename

If you want to append to a file, use >> instead of >.
# 4  
Old 08-24-2012
Code:
 
echo "Some test code here" > filename

is the simplest of all the commands.
Also $ in sed "s/" is the regex for the end of line. and ^ is the begin line.
for example:
Code:
 
bash-3.00$ cat filename
Some test code here
bash-3.00$ cat filename|sed "s/$/9/"
Some test code here9
bash-3.00$ cat filename|sed "s/^/9/"
9Some test code here

Is that what you meant to ask?

Last edited by Shellslave; 08-24-2012 at 01:07 PM.. Reason: mistake
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting IDs from a text file into a sequence alignment file

Hi, I have one file with one column and several hundred entries File1: NA1 NA2 NA3And now I need to run a command within a mapping aligner tool to insert these sample names into a sequence alignment file (SAM) such that they look like this @RG ID:Library1 SM:NA1 PL:Illumina ... (7 Replies)
Discussion started by: nans
7 Replies

2. UNIX for Dummies Questions & Answers

Inserting information from old text file into a new text file

Hello, I'm trying to take information from a list of hundreds of subject ids (where each line is a new subject id), and insert each line into a new text file that contains the pathnames for each subject. To clarify, all subject have a similiar path name (e.g., C:\data\SUBJECT_ID\) that contains... (4 Replies)
Discussion started by: invisibledwarf
4 Replies

3. Shell Programming and Scripting

Inserting text in file names while copying them.

I'm trying to find a Bourne shell script that will copy files from one directory using a wild card for the file name (*) and add some more characters in the middle of the file name as it is copied. As an example: /u01/tmp-file1.xml => /u02/tmp-file1-20130620.xml /u01/tmp-file2.xml => ... (6 Replies)
Discussion started by: Tony Keller
6 Replies

4. UNIX for Dummies Questions & Answers

Inserting text into a file with awk or sed

Hello, I've been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I'm stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction. #!/bin/bash... (3 Replies)
Discussion started by: Schubi
3 Replies

5. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

6. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

8. Shell Programming and Scripting

Inserting text and modifying the file

I am in a dire need of doing this job , please help from shell script or perl script. It will be highly appreciated. Please have a look at the following INPUT file; The first 14 rows are not of interest but I want them to be included in the output file as they are. From the row 14... (3 Replies)
Discussion started by: digipak
3 Replies

9. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

10. Shell Programming and Scripting

inserting subscriber no in text file using KSH....

:confused:Dears , I have text file I need to insert the subscriber number at position 32, and need to keep the next field at position 53 (no increasing of the record lenght), I mean I just want to replace the spaces at position 32 with subscirber number . for example A B A ... (1 Reply)
Discussion started by: atiato
1 Replies
Login or Register to Ask a Question