LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-28-2010, 05:25 AM   #1
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Looking for a one line alternative


So I have been enjoying trying to find minimal answer results to questions of late using a variety of sed, awk and bash (and happy for other alternatives).

I have a small piece of code and was wondering if anyone has a good one liner to accomplish the same, the ones i currently have don't seem much better than my current code:

Current Code:
Code:
if [[ $(grep -q "foo" $file) -eq 0 ]]
then
    sed -i 's/foo/bar/' $file
else
    echo "bar" >> $file
fi
As you can see the general scenario is:
1. If "foo" in file change it for "bar"
2. Else add "bar" to the end of the file

To give an idea of what I have tried:
Code:
awk 'BEGIN{f=1}/foo/{gsub(/foo/,"bar");f=0}END{if(f)print "bar"}1' file
Any suggestions greatly appreciated
 
Old 04-28-2010, 05:47 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Something along the lines of (not tested but I know you can figure it out from the general idea )
Code:
[[ grep -q foo "$file" ]] && sed -i 's/foo/bar/' "$file" || echo bar >> "$file"
The quoting changes are meant to be both more minimal and more robust.

EDIT: silly mistake! The [[ ]] adds nothing; here's tidier
Code:
grep -q foo "$file" && sed -i 's/foo/bar/' "$file" || echo bar >> "$file"

Last edited by catkin; 04-28-2010 at 08:09 AM.
 
  


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
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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