adding a line to a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding a line to a text file
# 1  
Old 06-10-2011
adding a line to a text file

I have a tab delimited text file,
Code:
id   name       distance
1    3325167    0.334561754018
2    3290488    0.389444269458
3    3288794    0.392312701782
4    3347602    0.392532202097
5    3295355    0.394394169485

I need to add a line after the header line. The first and third field of that added line with both be the value 0 (hard coded). The middle field will be from a bash variable.
Code:
id   name       distance
0    $BASH_VAR  0.0
1    3325167    0.334561754018
2    3290488    0.389444269458
3    3288794    0.392312701782
4    3347602    0.392532202097
5    3295355    0.394394169485

I have looked at solutions in both sed and awk and can't seem to find the right thing.

I tried,
Code:
awk 'BEGIN {FS="\t"} {if (NR==2) print "0", "\t", $NAME, "\t", "0.0"; else print $0; }' TEMP > $OUTFILE

But that garbles the values of the inserted and existing lines.

LMHmedchem
# 2  
Old 06-11-2011
Code:
% BASH_VAR=hello 

% cat testfile   
id   name       distance
1    3325167    0.334561754018
2    3290488    0.389444269458
3    3288794    0.392312701782
4    3347602    0.392532202097
5    3295355    0.394394169485

% sed '1a\       
0    '$BASH_VAR'  0.0' testfile
id   name       distance
0    hello  0.0
1    3325167    0.334561754018
2    3290488    0.389444269458
3    3288794    0.392312701782
4    3347602    0.392532202097
5    3295355    0.394394169485

If you need right columns try:
Code:
BASH_VAR=`printf '%-9s' hello`

This User Gave Thanks to yazu For This Post:
# 3  
Old 06-11-2011
I need tab delimited, so I got it to work with,

sed '1a\0\t'$BASH_VAR'\t0.0' TEMP > $OUTFILE

what is the meaning of the 1a\ notation? Does that mean to add the line at position 1? I thought you generally needed double quotes to preserve the value of the $VAR.

LMHmedchem
# 4  
Old 06-11-2011
Quote:
Does that mean to add the line at position 1?
Yes.
Quote:
I thought you generally needed double quotes to preserve the value of the $VAR.
Here it can work, but it won't in common case because sed and shell metachars can conflict (especially backslashes). So usual practice to embed shell variables in sed command is:
Code:
sed '...'$VAR'...'

Sorry for my English.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding text to the first line of a shell script

the first line of every unix script written in an interpreted language always has a "#!<path-to-the-language>" is there a way to include other text in that first line without it affecting the ability of the script to run??? for instance, if i change the following line: #!/bin/sh echo blah... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Adding a text in the beginning of a line

Hi, I am doing something like below: cat file1>file3and cat file2>>file3 I wanted to check if there is a way to write a custom message(hardcoded message)something like below at the beginning of each line then PIPE delimitiation and then followed by remaining record. cat file1... (7 Replies)
Discussion started by: Saanvi1
7 Replies

3. UNIX for Dummies Questions & Answers

[Help] Adding text to a variable line in a file

Hey guys, I need to write a script that will add a specific text at the end of a specific line (of a text file). but the line is a variable this is my text file : device_2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=30000 ansible_ssh_user='root' device_2 ansible_ssh_host=127.0.0.1... (1 Reply)
Discussion started by: OdedOvdat
1 Replies

4. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

5. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

6. Shell Programming and Scripting

Adding text in final line

Dear Friends, I have a flat file where last line of it has word D$mhtt I want to add a space and back slash after it. Also wanna add -S "J" in the last line. Following example will make it clear. I have this in the last line of file D$mhtt I want D$mhtt \ -S "J" Please... (5 Replies)
Discussion started by: anushree.a
5 Replies

7. Shell Programming and Scripting

adding new line after finding specific text

hello i need some help here are the contents of my file. test.txt this is filename 1.mp3 http://www.url.com/filenamehashed filename 2.mp3 http://www.url.com/fileamehashed something_else.zip http://www.url.com/filenamehashed so this file has 100 of these lines filename url I would... (9 Replies)
Discussion started by: mscice
9 Replies

8. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

9. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies

10. Shell Programming and Scripting

Adding Text To each line of a file

How would I add text to the beginning of each line in a text file in a script right after the file is created from another text file. (4 Replies)
Discussion started by: cubs0729
4 Replies
Login or Register to Ask a Question