SED - insert space at the beginning of line and multi replace command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - insert space at the beginning of line and multi replace command
# 1  
Old 07-12-2012
SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command
Code:
 
sed 'pattern c\
new line
' <file1 >file 2

I got two questions

1. how do I insert a blank space at the beginning of new line?

2. how do I use this command to execute multiple command using the -e options (if possible)? as I want to replace multiple lines in a single file.

Thanks
Robert

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 07-12-2012 at 11:15 AM..
# 2  
Old 07-12-2012
Could you please provide an example of what input you have and what output you expect, people may then be able to provide more accurate suggestions.
Thanks

... by the way, at the beginning of which lines would you like to insert a space ? all ? only some ? which ?
# 3  
Old 07-12-2012
for example

file1 contain this line
Code:
 
 name/date/record/serial (note a single space at beginning of line)

and I am using the sed command
Code:
sed 'name c\
name/date/record/$newserial
' <file1 >file2

to change the line with variable $newserial

Problem is it won't keep the space at the beginning of the line, and I want to keep it (for tidy sake).

Secondly I want to do multiple change in a single file but dont' know how to combine the c\ with the -e command in SED, so that I won't have to write multiple SED command in the script which would require to generate a new output file everytime the SED c\ command is called.

hope that clear things up.

Thanks

Last edited by vbe; 07-12-2012 at 11:16 AM.. Reason: code tags
# 4  
Old 07-12-2012
Just put a space before the new text and it will be preserved (the bolded, red underscore is where the space would go):
Code:
sed '
/pattern1/ c\
_newtext1
/pattern2/ c\
_newtext2
' file

On an unrelated note, if you want to expand the value of a shell variable into the new text, you cannot use single quotes; you must use double quotes instead.

Regards,
Alister
# 5  
Old 07-12-2012
I tried it but it doesn't work.....
# 6  
Old 07-12-2012
Which shell and operating system are you using? Post the exact code that you tried in the exact way that you invoked it. Post the exact erroneous output and error message that you received. Otherwise, we can't be of any help.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use sed to insert character in the beginning of file path?

I need to manipulate one Database file on Solaris 11 in which contains more than 5000 lines of data file path like this: '/data1/oradata/DBNAME/system01.dbf', '/data7/oradata/DBNAME/undotbs1_01.dbf', '/data1/oradata/DBNAME/sysaux01.dbf', '/data28/oradata/DBNAME/userdata01.dbf', ... (6 Replies)
Discussion started by: duke0001
6 Replies

2. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

3. Shell Programming and Scripting

Insert output from a file to beginning of line with sed

Hi I've been trying to search but couldn't quite get the answer I was looking for. I have a a file that's like this Time, 9/1/12 0:00, 1033 0:10, 1044 ... 23:50, 1050 How do I make it so the file will be like this? 9/1/12, 0:00, 1033 9/1/12, 0:10, 1044 ... 9/1/12, 23:50, 1050 I... (4 Replies)
Discussion started by: diesel88
4 Replies

4. Shell Programming and Scripting

sed to replace a line with multi lines from a var

I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file. myVar=`cat myfile` sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file myfile: cat dog boy girl mouse house test.file: football hockey Replace_Flag baseball ... (4 Replies)
Discussion started by: bblondin
4 Replies

5. UNIX for Advanced & Expert Users

Insert 1 space using the command sed

Hi I want to use sed to insert one space after the 10'th character in every line. The lines are on this format: 2012-01-1012:30:55|7323456|65432 2011-02-0313:11:06|1223|3456 ...... ...... Does anyone know sed well enough to acomplish this? If there is any other way around this... (7 Replies)
Discussion started by: ic12
7 Replies

6. Shell Programming and Scripting

sed : replace space and end-of-line

Hi ! I'm rather new with sed ... learned a lot already by googling etc ... The following script should replace all spaces and ends-of-lines with "something (see below). #!/bin/bash i=0 while read line do fam="H`printf "%06d" $i`" echo $line | sed -e 's//\t'$fam'\n/g' i=$(($i+1))... (7 Replies)
Discussion started by: jossojjos
7 Replies

7. Shell Programming and Scripting

how to replace new line ( \n ) with space or Tab in Sed

Hi, how to replace new line with tab in sed ... i used sed -e 's/\n/\t/g' <filename > but its not working (1 Reply)
Discussion started by: mail2sant
1 Replies

8. Shell Programming and Scripting

gnu sed replace space with new line

please help in making sed singleline command i need to insert dos new line (CR LF) before " 34 matching device(s) found on \\cpu1." " 6 matching device(s) found on \\cpu7." " 102 matching device(s) found on \\mainserver." the problem is that sometimes there are both CR LF before strings and... (1 Reply)
Discussion started by: xserg
1 Replies

9. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

10. Shell Programming and Scripting

Replace space, that is not in html tags <> with new line using sed

Hi, I am working on transforming html code text into the .vert text format. I want to use linux utility sed. I have this regexp which should do the work: s/ \(?!*>\)/\n/g. I use it like this with sed: echo "you <we try> there" | sed 's/ \(?!*>\)/\n/g' ... The demanded output should be: you <we... (5 Replies)
Discussion started by: matt1311
5 Replies
Login or Register to Ask a Question