- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: insert text into a file after a particular lin...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 06:35 AM
12-01-2003 06:35 AM
I have a file which contains around 8 lines of text. I have to insert the contents of this file into another file after say line number 79.
Can anyone please help me out in this.
Thanks,
Andy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 06:47 AM
12-01-2003 06:47 AM
Re: insert text into a file after a particular line
Do vi of your second file, put cursor at line 79 and type ":r filename" where filename is the name of your 8-lines file.
Elena.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 06:51 AM
12-01-2003 06:51 AM
Re: insert text into a file after a particular line
I know how to do it in vi. But I have to do it in a SHELL script.
Thanks,
Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 07:05 AM
12-01-2003 07:05 AM
Re: insert text into a file after a particular line
lineNum=79
rfile=File2Insert
file=File2AddText
ex -s +"$lineNum|r $file|wq" file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 07:21 AM
12-01-2003 07:21 AM
Re: insert text into a file after a particular line
file1=file2insert
file2=text2insert
file3=tmpfile
lineNum=79
sed -ne '1,$lineNum' -e '$lineNum r $file2' -e '$((lineNum+1)),$p' file 1 > file3
mv file3 file1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 07:26 AM
12-01-2003 07:26 AM
Re: insert text into a file after a particular line
sed -ne '1,$lineNum' -e '$lineNum r $file2' -e '$((lineNum+1)),$p' file 1 > file3
should be
sed -ne "1,$lineNum p" -e "$lineNum r $file2" -e "$((lineNum+1)),$ p" file1 > file3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 07:36 AM
12-01-2003 07:36 AM
Re: insert text into a file after a particular line
echo "
Or Perl (in case you want to do something fancy in determining that line number from STDIN:
#!/bin/perl
$line = 0 + shift @ARGV;
$file = shift @ARGV || die "Need line-number, File-name to insert";
open (FILE,"<$file") || die "failed to open insert file $file";
while (<>) {
print;
}
Or Perl from file name on argument list:
#!/bin/perl
$line = 0 + shift @ARGV;
$file = shift @ARGV;
$orig = shift @ARGV || die "Need line-number, File-name to insert, File";
open (ORIG,"<$orig") || die "failed to open original $file";
open (FILE,"<$file") || die "failed to open insert file $file";
while (
print;
}
cheers,
hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 09:09 AM
12-01-2003 09:09 AM
Re: insert text into a file after a particular line
I tried using your sed solution but it does not work. It is not writing anything to the tmpfile.
Can you please look into it.
Thanks,
Andy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 10:39 AM
12-01-2003 10:39 AM
Solution> cat x1
aap
noot
mies
teun
> cat x2
x
x
> export LINE=2
> export FILE=x2
> sed "${LINE}r $FILE" x1
aap
noot
x
x
mies
teun
of course ou need to escape the LINE variable name to make sure the 'r' for read is not eaten up with up.
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 11:06 AM
12-01-2003 11:06 AM
Re: insert text into a file after a particular line
It works absolutely fine now.
Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2003 11:52 PM
12-01-2003 11:52 PM
Re: insert text into a file after a particular line
file2 --- The file on which file1 needs to be added.
LEN=`wc -l file2`
LINENUM=`echo "LINE -1"|bc`
cat file2 |head -$LINENUM >>/tmp/file_new
cat file1 >>/tmp/file_new
cat file2 |tail -`echo "$LEN - 78" |bc` >> /tmp/file_new
A simple way of doing it as i do not know much about sed and awk !
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2003 06:08 PM
12-02-2003 06:08 PM
Re: insert text into a file after a particular line
file1 --- The file to be added
file2 --- The file on which file1 needs to be added.
file3 --- Resulting file
line --- line where file1 needs to be inserted
LEN=`wc -l <$file2` # to prevent the filename in the output, use redirection
PART1=`expr $line - 1`
PART2=`expr $LEN - $PART1`
LINENUM=`echo "LINE -1"|bc`
(
head -$PART1 $file2
cat $file1
tail -$PART2 $file2
) > $file3