LinuxQuestions.org
Help answer threads with 0 replies.
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 04-11-2014, 04:23 PM   #1
mamunm
LQ Newbie
 
Registered: Apr 2014
Posts: 12

Rep: Reputation: Disabled
sed command for matching the end character of a line and replacing the whole line.


I have a file with multiple lines that end with either 0 or 1. now i want replace those line having 1 at last. how can i do that?
Some more question:
1)i want to replace all elements below a certain limit by a threshold value.
2)In a file i have the following line in regular interval:
energy=25
energy=35
energy=45
After each iteration it writes that value. Now i want to extract the last line.
It would be a great help if anyone kindly answer these questions.
Thanks
 
Old 04-11-2014, 06:22 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
It certainly is possible to replace
(any number of characters)(one or more specific characters)(End-of-Line marker)
with something else. The following will replace any line ending with the character X with foo:
Code:
sed "s/.*X$/foo/"
With regards to your other questions:

1) I'm not sure I understand exactly what you want to do. Can you be a bit more specific?

2) The last line of a file can easily be retrieved with tail:
Code:
tail -n 1 < filename

Last edited by Ser Olmy; 04-11-2014 at 06:23 PM.
 
1 members found this post helpful.
Old 04-11-2014, 06:28 PM   #3
mamunm
LQ Newbie
 
Registered: Apr 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thank you very very much. it worked nicely. actually second question is not a serious issue. i think a if statement can do that. The main thing is i want to replace all the value less than 100 by 100.
 
Old 04-11-2014, 11:32 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
Quote:
The main thing is i want to replace all the value less than 100 by 100.
Try awk instead of sed
Code:
bash-4.2$ cat en1.txt
energy=25
energy=35
energy=45
energy=125
energy=135
energy=145

bash-4.2$ awk 'BEGIN{FS=OFS="="} {if ($2<100 && $NF>1) {print $1,"100"} else  {print} }' en1.txt 
energy=100
energy=100
energy=100
energy=125
energy=135
energy=145
 
1 members found this post helpful.
Old 04-12-2014, 01:19 AM   #5
mamunm
LQ Newbie
 
Registered: Apr 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
I am actually a new user of Linux. For editing i was basically using vi and sed command. Thanks for introducing awk to me.
 
  


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
sed replace end of line with character grob115 Programming 13 02-14-2013 06:52 PM
Sed command to skip first character of line which is slash se_ala Programming 6 08-05-2011 03:50 AM
sed add a character to the end of each line keenboy Linux - General 2 08-05-2010 01:36 PM
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-27-2008 12:51 AM
Problem with grep matching to end of line icedown Linux - Newbie 12 09-23-2007 06:43 AM


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