LinuxQuestions.org
Review your favorite Linux distribution.
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 10-08-2011, 09:49 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
BASH replace line


Hello. How I could replace lines like this. For example: I type a text and I want to replace this text with .txt file 3rd line. I hope you understand.
 
Old 10-08-2011, 10:09 AM   #2
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
no!?
did you forget the example?
 
Old 10-08-2011, 10:37 AM   #3
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Example
text file
Quote:
John Smith 123456 john@mail.com
Carl Johnson 45964 carl@mail.com
Piece of my script
Quote:
grep -n "$r" contacts.txt
echo Enter number of contacts you want delete
read nr
echo "New name"
read nam
echo "New surname"
read sur
echo "Number"
read num
echo "E-Mail"
read el
E.g. I want change contacts.txt second line with new name, surname, number and mail
 
Old 10-08-2011, 11:09 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
string manipulation in bash.
 
Old 10-08-2011, 11:17 AM   #5
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
I can't find what I need
 
Old 10-08-2011, 11:25 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Well, what exactly do you need then? Your requirements are still a bit vague to me. How about giving us a real, clear example of what you're trying to do?

If you want to edit lines from the file itself, you'll probably want to use sed.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
 
Old 10-08-2011, 11:41 AM   #7
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
contacts.txt
Quote:
John Smith 123456 john@mail.com
Carl Johnson 45964 carl@mail.com
Quote:
grep -n "$r" contacts.txt
echo Enter number of contact you want delete
read nr
echo "New name" ( I enter "Ben")
read nam
echo "New surname" (I enter Gilmore)
read sur
echo "Number" (I enter 123)
read num
echo "E-Mail" (I enter ben@mail.com)
read el
in txt file line number 2 i want change with new name surname number and email. I mean this command echo
Quote:
$nam,," "$sur, " "$num, " ",$el >> contact.txt;;
writes all this information into txt file, I need to replace this new info for example with 2nd line in txt.
Now txt file have to be
Quote:
John Smith 123456 john@mail.com
Ben Gilmore 123 ben@mail.com
 
Old 10-08-2011, 12:14 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your requirement seems to include two principle elements:
  • text replacement in a file
  • line-number oriented identification of the text to replace.
Given these characteristics, I would point to sed as the tool of choice to solve this problem. A sed script can prescribe the line number, or range of line numbers, in which to perform it actions. One of the most basic actions it can perform is text substitution.

One the whole, you seem to be developing a small database application, and I would suggest that this will fairly quickly grow to exceed the capabilities of comfortable programming in bash. I suggest you consider a more complete programming language for this project.

--- rod.

Last edited by theNbomr; 10-08-2011 at 12:15 PM.
 
Old 10-08-2011, 12:37 PM   #9
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Can you write me an example?
 
Old 10-08-2011, 12:54 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
recnum=2
fname=Ben
sname=Gilmore
number=123
email=ben@mail.com
sed "$recnum s/.*/$fname $sname $number $email/" LQtrintukaz.txt
Add the '-i' switch to the sed commandline arguments when you're satisfied that it works as desired, and the editing will be performed 'in-place'.

--- rod.
 
Old 10-08-2011, 01:26 PM   #11
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thank, but there is only one problem. When i need enter name, surname, number and email and if I don't type anything and skip everything by pressing ENTER it deletes all contacts from contacts.txt. Do you know how to fix this problem?

Last edited by trintukaz; 10-08-2011 at 01:28 PM.
 
Old 10-08-2011, 02:41 PM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
That's because if $recnum is null, the substitution applies to all lines in the file. Use a test for null entries in your data input routine.

--- rod.
 
Old 10-08-2011, 11:35 PM   #13
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Sorry. I don't understand.
 
Old 10-08-2011, 11:53 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
You need to take the time to learn how the tools you're using work.

Sed's basic expression syntax looks like this:

[address1[,address2]] commands

"s///" is the command for substitution, for example.

The address range restrict which lines sed will run the commands on, the simplest range being a single line number. If you don't give any addresses, then the commands operate on every line.

This is all explained in the links I gave you earlier.

$recnum is a variable containing a line number. If $recnum is empty, then there's no address for sed to operate on, and so the commands will be applied to all the lines in the file. As theNbomr said, you need to apply some kind of test first to ensure that the variables all hold the kind of values you need before running the sed command.
 
Old 10-09-2011, 12:26 AM   #15
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
If there is possibility to make script to not edit line if I Don't enter $recnum?
 
  


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
bash: replace a line in text file freeindy Programming 10 09-08-2011 12:08 PM
Bash search line and replace hex to binary ekim Programming 5 07-09-2011 01:24 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Perl script to replace a line after finding a previous line Mark1986 Programming 1 02-28-2011 05:09 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 PM.

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