LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2013, 05:41 AM   #1
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Rep: Reputation: Disabled
insert 15 lines to an file using an script.


Want to insert 15 lines to an file using an script(ksh).

wanted to insert the data from 9th line onwards
15 lines will be

<FIELD_METADATA>externalName="DATE"
externalName="SYMBOL"
externalName="CQI"
externalName="COUNTRY"
externalName="EXCHANGE"
externalName="COMSTK_SUBSCR_EXCH"
externalName="RQI"
externalName="RIC"
externalName="COMPRIC"
externalName="MPV"
externalName="ISPRIMARY"
externalName="ROUNDLOT"
</FIELD_METADATA>
 
Old 09-25-2013, 06:17 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
sed will do it

Code:
sed '9 i \<FIELD_METADATA\>externalName="DATE"\
externalName="SYMBOL"\
externalName="CQI"\
externalName="COUNTRY"\
externalName="EXCHANGE"\
externalName="COMSTK_SUBSCR_EXCH"\
externalName="RQI"\
externalName="RIC"\
externalName="COMPRIC"\
externalName="MPV"\
externalName="ISPRIMARY"\
externalName="ROUNDLOT"\
\<\/FIELD_METADATA\>' Input
assumes you want line 9 to be the start, either use "10 i", or "9 a" if you want the start to be line 10
 
1 members found this post helpful.
Old 09-25-2013, 07:14 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
If the 15 lines you want to insert are in a file (date.file in this example) you can do the following:
Code:
sed '9 r data.file' infile
 
2 members found this post helpful.
Old 09-26-2013, 02:00 AM   #4
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
OOps its not working
 
Old 09-26-2013, 02:13 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Vthimmap View Post
OOps its not working
Please provide some details. What have you tried? What is the expected output? Where the lines you want to add come from? What is the current output? Did you receive error messages?
 
Old 09-26-2013, 02:22 AM   #6
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
I made it very simple

#!/bin/ksh
cp PAT_QD_Futures_NAMR PAT_MDQD_EMINI.test
sed '9 r new_txt' PAT_MDQD_EMINI.test

When i run the script , it displays the content of the file PAT_MDQD_EMINI.test[ Does not give any error messge]

The script can be in ksh, sh bash, tcsh

Unix details
mkd-s05.ny:541 ~> uname -aSunOS mkd-s05.ny.itginc.com 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Fire-V440
 
Old 09-26-2013, 03:23 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
I see 2 issue's:

1 - This seems to be done on a (open)Solaris box. My example is based on GNU sed, make sure to use that one if possible. It might be in /usr/local/bin and/or called gsed.

2 - The example given by me doesn't make in-place changes, add the -i flag:
Code:
sed -i '9 r new_txt' PAT_MDQD_EMINI.test
If you use a sed version that does not include the -i option, do the following:
Code:
sed '9 r new_txt' PAT_MDQD_EMINI.test > /tmp/PME.test
mv /tmp/PME.test > PAT_MDQD_EMINI.test
BTW: GNU versions of packages for Solaris can be downloaded from here: Sunfreeware.com
 
Old 09-26-2013, 05:08 AM   #8
Vthimmap
Member
 
Registered: Oct 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
Thanks a lot

Its working now.
 
Old 09-26-2013, 05:18 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome

BTW: Can you put up the [SOLVED] tag (upper right hand corner or use the Thread Tools menu).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 05:37 AM
script to insert no. of lines in a file to the start of the file LarsB Linux - Newbie 3 09-17-2009 09:22 PM
SHELL Script to insert lines after certain Intervals rahulruns Linux - General 9 02-26-2008 02:21 AM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 08:48 AM


All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration