Append string at start of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append string at start of line
# 8  
Old 04-04-2011
Quote:
Originally Posted by db2cap
Still does not help Smilie
It get executed without any error but no change in the actual file
Post a sample file you're trying to edit. Please use code tags.
# 9  
Old 04-04-2011
Code:
$ ruby -ne '$_="##{$_}" if /xyz/; print' file

# 10  
Old 04-05-2011
Hi,
Please find the sample content of the file. Purpose is to comment all the hotbackup scripts that are scheduled in the cron. This is just a sample, there are 85 databases.

$ crontab -l

Code:
05 02 * * 1-6  /xyz/hotbackup.sh >> /xyz/log/hotbackup.log
45 03 * * 1-6  /xyz/netbackup.sh DAILY
10 02 * * 1-6  /xyz/hotbackup.sh >> /xyz/log/hotbackup.log
10 03 * * 0    /xyz/coldbackup.sh >> /xyz/log/coldbackup.log

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 04-05-2011 at 08:38 AM.. Reason: code tags, please!
# 11  
Old 04-05-2011
Quote:
Originally Posted by vgersh99
Post a sample file you're trying to edit. Please use code tags.
Please provide the appropriate information!
# 12  
Old 04-05-2011
Hi,
Please find the sample content of the file. Purpose is to insert # at the start of line wherever it finds word hotbackup through strem editor.

Contents in file:

Code:
05 02 * * 1-6  /xyz/hotbackup.sh >> /xyz/log/hotbackup.log
45 03 * * 1-6  /xyz/netbackup.sh DAILY
10 02 * * 1-6  /xyz/hotbackup.sh >> /xyz/log/hotbackup.log
10 03 * * 0    /xyz/coldbackup.sh >> /xyz/log/coldbackup.log


Last edited by vgersh99; 04-05-2011 at 08:50 AM.. Reason: code tags - please read the previously posted links on how to use the code tags!
# 13  
Old 04-05-2011
Quote:
Originally Posted by db2cap
but i am not getting the switch to append the string in the file itself instead of redirecting the output
This is to be expected and the reason for that behavior is simple: sed will not modify the file it is reading from, because doing so would destroy the data you want to change in first place.

Interactive editors can change files in place, because they create a copy of the original file and work on this copy *), then overwrite the original version with the edited one. sed on the other hand works in a stream (as its name suggests) and therefore can't use a copy.

If you want to replace a file with its edited version do the following:

Code:
# sed '<your-sed-commands-here>' /path/to/source > /path/to/newfile
# mv /path/to/newfile /path/to/source

I hope this helps.

bakunin

*) For example, this is why "vi" cannot edit files bigger than the available space in /var/tmp. "vi" will use /var/tmp to store the copy and if it can't it will refuse to open the file.
# 14  
Old 04-05-2011
Code:
printf '/hotbackup/s!^!#!g\nwq!\n' | ex -s myFile
OR
{ rm myFile; sed -e '/hotbackup/s!^!#!' > myFile; } < myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

3. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

4. Shell Programming and Scripting

Append string to first line of a file

Hi, Please suggest me to write unix command, HEADER20110101 string append to first line of a file.. Regards Akshu (3 Replies)
Discussion started by: akshu.agni
3 Replies

5. UNIX for Dummies Questions & Answers

Append symbol at the start of each line

hi, i have some values in excel sheet as in below format: 122 144 222 555 666 etc.... i need to get the output in the below manner.. £122 £144 £222 £555 £666 (1 Reply)
Discussion started by: arunmanas
1 Replies

6. Shell Programming and Scripting

how to append a string to next line in perl

hi all , i have a requirement like this.. this just a sample script... $ cat test.sh #!/bin/bash perl -e ' open(IN,"addrss"); open(out,">>addrss"); @newval; while (<IN>) { @col_val=split(/:/); if ($.==1) { for($i=0;$i<=$#col_val;$i++) { ... (2 Replies)
Discussion started by: tprayush
2 Replies

7. Shell Programming and Scripting

Append a string at the end of every line in a file

Hi Friends, I have a file with many lines as shown below. /START SAMPLE LINE/ M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\port\Post.java@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\switchview\View.java@@\main\v6.4.0_pit_a /END SAMPLE LINE/ I... (1 Reply)
Discussion started by: nmattam
1 Replies

8. Shell Programming and Scripting

How to append a string to a specific location in a line

Hi, I have a to modify a line and insert a keyword in the middle to a specific location. My line looks like this FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (ABC, DEF, GHI) I want to change it as FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING... (4 Replies)
Discussion started by: mwrg
4 Replies

9. UNIX for Advanced & Expert Users

append the line with the previous if it not start with 1=

How to append the line with the previous if it not start with 1=. 1=ttt, 2=xxxxxx, 3=4545 44545, 4=66666, 1=ttt, 2=xxxxxx, 3=34434 3545, 4=66666, 5=ffffff 6=uuuuuuu, 7=ooooooo 1=ttt, 2=xxxxxx, 3=311343545, 4=66666 1=ttt, 2=xxxxxx, 5=XAXAXA, 7=FDFD (3 Replies)
Discussion started by: palsevlohit_123
3 Replies

10. Shell Programming and Scripting

append string with spaces to a line

hi i have a file like (every string contains 16 chars) CTL1330000000000 0000 00 008000 0080000000 i need to form a line and write to a file CTL13300000000000000 00008000 0080000000 total chars should be 64 ... (2 Replies)
Discussion started by: Satyak
2 Replies
Login or Register to Ask a Question