Insert row without deleting previous data using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert row without deleting previous data using sed
# 1  
Old 12-05-2011
Insert row without deleting previous data using sed

Hello,

I want to add a new row to a file to insert data without deleting the previous data there.

Example:

file
Code:
a
b
c
d

Output
Code:
a
b
newtext
c
d

I want to be able to choose any row I want to change and without deleting anything else. basically i want to add the new row and move everything else down so I'm able to add data to the new row.

this is what i have tried so far

Code:
sed '3s/^/newtext\n/' file > Output

# 2  
Old 12-05-2011
Try doing this
Code:
sed '3inewtext' file > output

# 3  
Old 12-05-2011
Yea I was trying that also but it gives a warning:
Code:
command i expects \ followed by text

---------- Post updated at 06:13 PM ---------- Previous update was at 06:04 PM ----------

I tried something different here is the code but

Code:
sed '3 s/^/newtext\n/' file > Output

but it gives this

Code:
a
b
newtextnc
d

note the "n" between newtext and c

I want

Code:
a
b
newtext
c
d

---------- Post updated at 06:47 PM ---------- Previous update was at 06:13 PM ----------

I can't make c to go to the next row. after replacing newtext
# 4  
Old 12-05-2011
umm my solution seems to work fine with GNU sed, try this:

Code:
sed '3i\
test' infile > outfile

# 5  
Old 12-05-2011
i get the error message

Code:
extra characters after \ at the end of i command

it could be that Im using a different version or something. =/ any way of knowing that?

or is it possible to use awk? I don't want to open my 4 GB data file and go to the x row and enter the data manually. Opening the data file that big will take forever to open or sometimes it will not open on my computer. =/
# 6  
Old 12-05-2011
Try this...
Code:
awk 'NR==3{print "newtext"}1' input_file > out_file

If solaris, use nawk!

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 12-06-2011
Chubler thanks I guess it didn't work for me. Thank you for your time.

And thanks Ahamed it works fine. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

2. Shell Programming and Scripting

In php, Moving a new row to another table and deleting old row

Hi, I already succeed moving a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). But it can't delete the old row. Please help me with the script. my php script: INSERT INTO... (2 Replies)
Discussion started by: jazzyzha
2 Replies

3. Shell Programming and Scripting

Moving new row and deleting old row to another table

Hi, I want to move a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). I already use this script but doesn't work as I expected. CHECK_KEYWORD="$( mysql -uroot -p123456 smsd -N... (7 Replies)
Discussion started by: jazzyzha
7 Replies

4. Shell Programming and Scripting

remove row if string is same as previous row

I have data like: Blue Apple 6 Red Apple 7 Yellow Apple 8 Green Banana 2 Purple Banana 8 Orange Pear 11 What I want to do is if $2 in a row is the same as $2 in the previous row remove that row. An identical $2 may exist more than one time. So the out file would look like: Blue... (4 Replies)
Discussion started by: dcfargo
4 Replies

5. Shell Programming and Scripting

Can sed be used to insert data at specific column?

I'm trying to use sed to insert data at a specific column, let's say my data looks like this: 0553 1828 0552 1829 0550 1829 0549 1830 0548 1831 what I want is this: timein 0553 timeout 1828 timein 0552 timeout 1829 timein 0550 timeout 1829 timein 0549 timeout 1830 timein 0548... (5 Replies)
Discussion started by: mswartz
5 Replies

6. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

7. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

8. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

9. Shell Programming and Scripting

sed, insert data from a file to another?

Hello, I have 2 files. File1 has data I wrote, and File2 is a file created by an application. I would like to insert the data from File1 into File2, but it has to be inserted at a certain location on File2. I know I need to search for "</jsp-param> </jsp-descriptor>" But I don't know... (4 Replies)
Discussion started by: ctcuser
4 Replies

10. Shell Programming and Scripting

Value of previous row using awk

I would like to know value for previous row, in addition to current row. How would I will get value for previous row? How can I perform this with awk? (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question