LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-21-2008, 12:27 PM   #1
Frayed
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
Question bash to edit one line of a file


Hi - how would I use bash scripting to edit one line of a file? I have a file thats is several hundred lines long. Each line ends in a '%' character. I want to remove the '%' character from the last line only. Thanks.
 
Old 04-21-2008, 12:36 PM   #2
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
edit:
Ignore this, nothing to see here, move along. Spurious PEBKAC interrupt detected.(Note to self: Engage brain for more than 0.3 seconds before posting).

Proceed directly to osor's post below.

endedit.



how about:

Code:
echo -e "\n" | dd bs=1 count=1 of=$file seek=$(( $(stat -c %s $file) - 2))
I'm assuming you want to retain the newline, this replaces the last two bytes (i.e. "%\n") with "\n".

Last edited by ararus; 04-25-2008 at 11:56 AM.
 
Old 04-21-2008, 01:03 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Code:
sed -ie '$s/%$//' filename
 
Old 04-21-2008, 02:06 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I strongly recommend the sed solution (or one using awk or another tool)

"dd" is the ultimate power tool and will do major damage with just a small typo. I would NOT use it where other tools are available
 
Old 04-21-2008, 09:22 PM   #5
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
Just learning sed but shouldn't it be.

Code:
sed -ie 's/%$//' filename
instead of

Code:
sed -ie '$s/%$//' filename

I've never see the "$" which normally "Matches the end of a line" in front
of the s-command for substitution.
 
Old 04-21-2008, 09:26 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by davimint View Post
I've never see the "$" which normally "Matches the end of a line" in front
of the s-command for substitution.
This is the address for the substitution. In an address context, $ means perform the command on the final line of input.
 
Old 04-21-2008, 09:31 PM   #7
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
Wink

Osor

Once again I read a post wrong, but thanks for the explanation. Gosh, I need to buy a book on "Sed" I don't think I'll every understand it Someone always shows me something I have not seen before.

Someday I'll get it.

Last edited by davimint; 04-21-2008 at 09:36 PM.
 
Old 04-21-2008, 10:24 PM   #8
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
Quote:
Originally Posted by davimint View Post
Gosh, I need to buy a book on "Sed" I don't think I'll every understand it Someone always shows me something I have not seen before.
I have O'Reilly's sed & awk, but there's plenty of info online, for example - here
 
Old 04-22-2008, 07:13 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
The Grymoire link is my favorite tutorial--look up addresses and address ranges.
 
  


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 shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
Packard Bell M5..Minimal Bash like line edit Tomcat1965 Linux - Laptop and Netbook 0 10-20-2006 02:10 AM
how can i EDIT file by BASH Guru Mind Programming 27 03-28-2006 03:38 AM
Line wrap errors with ssh / xterm / bash and up-arrow to edit brycen Linux - Software 0 09-22-2004 06:01 PM
how to edit a file when not in X (ie from the command line) ludwig W Linux - Newbie 12 04-22-2003 04:00 AM


All times are GMT -5. The time now is 11:19 PM.

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