LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Editing specific line in a textfile using Bash scripting. (https://www.linuxquestions.org/questions/linux-newbie-8/editing-specific-line-in-a-textfile-using-bash-scripting-4175558289/)

JamesQQ 11-07-2015 09:44 PM

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.

syg00 11-07-2015 09:53 PM

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.

berndbausch 11-07-2015 09:57 PM

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.

JamesQQ 11-07-2015 10:03 PM

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?

berndbausch 11-07-2015 11:18 PM

Quote:

Originally Posted by JamesQQ (Post 5446297)
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.

grail 11-08-2015 02:45 AM

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


All times are GMT -5. The time now is 06:44 PM.