LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-27-2005, 05:37 AM   #1
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Rep: Reputation: 15
Script to insert string in first line of a file


Hi
i need a script to store each new string in first line of file. not to append at the end.

I know echo " Hello" >> file.
This will append "Hello" string in file. But i want this string to be inserted at the first line in the file.

Thanks in advance.
minil
 
Old 12-27-2005, 06:36 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#!/bin/bash
insert()
{
	mv "$1" "${1}.bak" 2>/dev/null || touch "${1}.bak"
	cat /dev/stdin "${1}.bak" >"$1"
}

echo "First" | insert test.txt
echo "Second" | insert test.txt
echo "Third" | insert test.txt
 
Old 12-28-2005, 05:22 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Code:
#!/usr/bin/perl -p     # -p acts like sed default print line

print "thing\n" if $. == 1; # print if line num is 1
to actually do it to the file and
create a .bak backup:

Code:
perl -i.bak insert_string.pl  file
one line:
Code:
perl -i.bak -pe 'print "thang\n" if $. == 1;'
 
Old 12-28-2005, 06:04 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
here is a simpler command without using intermediate files

Code:
echo "1i\nhello\n.\nwq" | ex -s <file>
 
Old 12-28-2005, 06:12 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Behind the scene, 'ex' also uses intermediate files.
 
Old 12-28-2005, 06:19 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by Hko
Behind the scene, 'ex' also uses intermediate files.
Did you mean the temporary files used for crash recovery with ex editor?
 
Old 12-28-2005, 06:23 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
it's nice to have a .bak when it goes wrong.
which it probably will if it's important data
 
Old 12-28-2005, 06:39 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by kshkid
Did you mean the temporary files used for crash recovery with ex editor?
I know next to nothing about "ex", but in order to insert data into a file, the file always must be rewritten from the point of insertion.

Thinking about it a second time, "ex" does not necessarily use an intermediate file. But in that case it should keep the entire original file in memory, and still rewrite the file from the point of insertion (in this case: the entire file).

But as you pointed out, "ex" maintains a recovery file anyway. So I guess it very well possible it uses that instead of keeping everityhing in memory.
 
Old 12-28-2005, 08:03 AM   #9
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
File is too big.. so idont want to create temp files...
 
Old 12-28-2005, 08:06 AM   #10
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
there were chances when I tried with some 600 MB file ultimately ex editor failed.

Not sure how it is going to fix in your case.
 
Old 12-28-2005, 08:23 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
not much you can do about that chum.
whether you see them or not, somewhere there may well be temporary storage.


since when is space is more important than data?
you'll risk junking a file for the sake of a .bak?

you can do the perl -i without the .bak, though of course it's putting it's
temp storage somewhere.
 
Old 12-28-2005, 01:04 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Depending on what you are trying to do exactly, but it may be a solution to append the lines to the file. then when you want to read it, use it, or do further processing reverse the lines in the file with the "tac" utility.
 
Old 12-30-2005, 05:04 AM   #13
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
$ cat file
1. Line 1
2. Line 2
3. Line 3

$ sed -i '1i\This is the first line.' file

$ cat file
This is the first line.
1. Line 1
2. Line 2
3. Line 3

Using the -i switch with sed causes sed to edit a file "in place" rather than just putting the output on stdout. If you want to keep a backup then supply an extension with the -i switch.

$ sed -i.bak '1i\This is the first line.' file

$ cat file.bak
1. Line 1
2. Line 2
3. Line 3

$ cat file
This is the first line.
1. Line 1
2. Line 2
3. Line 3

HTH
 
Old 01-02-2006, 11:56 PM   #14
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
but " -i " option in sed is available in versions of GNU-SED
and I believe its not available in any other distribution.
 
  


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
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM
Substitute String or line in a file dimsh Linux - Newbie 4 09-21-2005 03:26 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration