LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 03: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, 05:22 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

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 05:23 PM.
 
1 members found this post helpful.
Old 04-11-2014, 05: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, 10:32 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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, 12: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:32 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
Open Source Consulting | Domain Registration