Bash script - add/edit to file and save - sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - add/edit to file and save - sed?
# 1  
Old 08-20-2015
Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated.

My example:
Code:
sed -i '14 i\
# add these lines
add these lines to the file
add these line to the file'
/my/file/location | tee /my/file/location


This is suppose to insert the lines after "sed -i '14 i\," into line 14, overwrite and save.

Last edited by Nvizn; 08-20-2015 at 03:43 PM..
# 2  
Old 08-20-2015
Why the tee?
# 3  
Old 08-20-2015
I'm open to any suggestion. Doesn't need to be sed.

---------- Post updated at 03:43 PM ---------- Previous update was at 02:06 PM ----------

Resolved using awk and writing the output, to a temp file.

Last edited by Nvizn; 08-20-2015 at 04:16 PM..
# 4  
Old 08-20-2015
Could you please show us how you used awk to make this work so other people reading your thread can learn from your experience?
# 5  
Old 08-20-2015
Try escaping the <new line> characters in the insertion text:
Code:
sed -i '14 i\
# add these lines\
add these lines to the file\
add these line to the file
' /my/file/location


Last edited by RudiC; 08-20-2015 at 05:34 PM..
# 6  
Old 08-20-2015
Code:
a="/file/location"
b="/file/locationb"
awk -v n=14 -v s="my text
my text
my text" 'NR == n {print s} {print}' $a > $b && mv $b $a

This User Gave Thanks to Nvizn For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select and save file in new directory

I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :). bash # select file printf "please select a file to analyze with entered gene or genes \n" select file in $(cd... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed. #!/usr/bin/expect -f... (2 Replies)
Discussion started by: jbrass
2 Replies

3. Shell Programming and Scripting

Save output into file bash scripting

Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 .............. 17 i wonder how to save the output into a single string and into a file. i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order,... (3 Replies)
Discussion started by: shdin271
3 Replies

4. UNIX for Dummies Questions & Answers

Cannot save edit on cron on Solaris 10

Hi Everyone, I have edited my cron using 'crontab -e'. When I try to save the changes using ':wq!' The file closes but the changes are not saved. I get the following messages: "/tmp/crontabJFainH" 45 lines, 2996 characters trail.log crontab: error on previous line; unexpected character found... (11 Replies)
Discussion started by: Scarlet
11 Replies

5. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

6. Shell Programming and Scripting

Run a bash script, display on the screen and save all information in a file including error info

Hi all, How to: Run a bash script, display on the screen and save all information in a file including error information. For example: I have a bash script called test.sh now I want to run the test.sh and display the output on the screen and save the output including error info to a file. ... (1 Reply)
Discussion started by: Damon sine
1 Replies

7. Shell Programming and Scripting

what is the switch to let sed edit and save file

I remember there is a sed switch i can use to edit and save the file at the same time, but i cannot recall it at all. so instead of -> sed 's/A/B/' file > file-tmp -> mv file-tmp file what can i do to just let sed edit and save the "file" (4 Replies)
Discussion started by: fedora
4 Replies

8. Shell Programming and Scripting

How search,edit and save the file

Hi All, I want to edit a file using shell script..For ex...a file called /etc/passwd..here I am searching for "ftp" if it is there just change it to "tftp" without using any temporary file. (3 Replies)
Discussion started by: Vichu
3 Replies

9. Shell Programming and Scripting

Edit a file and save the changes

I have an xml file that gets created as a part of daily build. I have to modify some lines in this file, uncomment some lines, comment some line, add 2 new lines in the file every time. Is there an easier automated way to do this using perl, bash, or sh. I would appreciate it if someone can point... (1 Reply)
Discussion started by: saurabh1982
1 Replies
Login or Register to Ask a Question