LinuxQuestions.org
Review your favorite Linux distribution.
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 11-07-2015, 10:44 PM   #1
JamesQQ
LQ Newbie
 
Registered: Nov 2015
Posts: 2

Rep: Reputation: Disabled
Editing specific line in a textfile using Bash scripting.


Hi,
I have been cracking my head and have been searching all over in google on how to edit specific line in a textfile filled with info.

I know to append something is
echo <text> >> textfile.txt

but what about like lets just say, line 3 of the textfile?

Thanks in advance.
 
Old 11-07-2015, 10:53 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
There are a bunch of ways, but sed is a tool you'll find no end of uses for. Including this.

Will have a reasonable learning curve if you've never used it, but well worth the time.
 
Old 11-07-2015, 10:57 PM   #3
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Do you want to insert something after line 3 of a text file? This requires saving some or all of your original file in temporary files, then combining the temporary files to create your intended result.

One way is splitting the file in two after line 3 (the head and tail commands might help, or the split command, or even csplit depending on your particular need), then inserting your text while gluing them together again.

Or use the sed program and its append command.

Or awk, the swiss army knife for text processing.

I am sure there are more possibilities.
 
Old 11-07-2015, 11:03 PM   #4
JamesQQ
LQ Newbie
 
Registered: Nov 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Im trying to create a booking system. Every time the user select one of the seats it will register to the text file but there will be different date and time.

So for example

The user register for seat A1 to monday 9.30pm.

*textfile*
Monday 8pm - B3,A2
Monday 8.30pm - B1,B2
Monday 9.30pm - A2,A3

how do i add in behind A3 for 9.30PM?

Last edited by JamesQQ; 11-07-2015 at 11:05 PM.
 
Old 11-08-2015, 12:18 AM   #5
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by JamesQQ View Post
Im trying to create a booking system. Every time the user select one of the seats it will register to the text file but there will be different date and time.

So for example

The user register for seat A1 to monday 9.30pm.

*textfile*
Monday 8pm - B3,A2
Monday 8.30pm - B1,B2
Monday 9.30pm - A2,A3

how do i add in behind A3 for 9.30PM?
I think your problem is to find out what the right position in the file is, correct? I.e. you want the file to be sorted by date and time.
In this case I would just add the line at the end (echo registrationstring >> textfile) then sort the file. The sort command should be powerful enough to do it right.

If you have control over the format of the text file, you may want to use a format that is easier to sort, e.g. instead of 8pm, always write 8.00pm or even 20.00.

Alternatively an awk program to find the right position, but that is probably more complex and error prone, and not necessarily more performant.

Last edited by berndbausch; 11-08-2015 at 12:23 AM.
 
Old 11-08-2015, 03:45 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please show the before and after of the text file as some of your terminology is misleading.
 
  


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
[SOLVED] Bash: copy a whole textfile to write inside another textfile thecazz Programming 6 08-04-2013 03:16 PM
Bash scripting help..permission denied on a specific line? Daravon Linux - Newbie 8 02-16-2012 10:45 PM
Editing a specific group of lines by line number? SirTristan Linux - Newbie 5 09-29-2009 08:30 PM
bash scripting - editing files brian0918 Linux - Newbie 2 07-01-2003 03:27 PM
bash scripting - editing files brian0918 Programming 1 06-30-2003 07:16 PM


All times are GMT -5. The time now is 10:04 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