Profile

Scrapbook photo 1
Scrapbook photo 2
Scrapbook photo 3
Scrapbook photo 4
Scrapbook photo 5
Josh Morgan
Works at Sun Automation Group
Attends Anne Arundel Community College
Lived in Baltimore, Md.
183 followers|24,896 views
AboutPostsPhotosYouTube

Stream

Josh Morgan

Shell scripts  - 
 
using this line in a script to find and replace a word in a file:

grep -lr "$oldWord" $somefile | xargs sed -i "s/$oldWord/$newWord/g" $somefile

I want to see if $oldWord exists in the file, but I think the line I am using is checking the exit status of sed instead of grep.

if [ $? -eq 1 ]
then
      echo "String doesn't exist in file"
fi

Any suggestions ?
12
3
jRome Martin's profile photowally thurston's profile photoDiego Espitia's profile photoEvaldo Araújo's profile photo
17 comments
 
I think that there may be a misunderstanding of xargs' function. You shouldn't need to use $somefile in the sed command; the output of grep (left side of the pipe) should be transferred to the arguments of sed (on the right side of the argument). I like to think of xargs as meaning (transfer (xfer) arg[ument]s)

Since you're using the '-r' recursive flag with grep, I'm assuming that you don't know what the file name is in the first place. In that case, you can use: 

grep -lZr "$oldWord <path_name> | xargs -r0 sed -i "s/$oldWord/$newWord/g"

if you do know what the file or file(s) is/are, then I would just use a single sed command: 

sed -i "/$oldWord/ s/$oldWord/$newWord/g" $somefile_1 [$somefile_2 ... $somefile_n]
Add a comment...

Josh Morgan

Shared publicly  - 
 
Starting an Introduction to Linux/Unix class for summer. Any thoughts on this text?
1
Add a comment...

Josh Morgan

Shared publicly  - 
1
Add a comment...

Josh Morgan

Shared publicly  - 
 
What a #Ravens game today!
1
Add a comment...

Josh Morgan

Shared publicly  - 
1
Add a comment...

Josh Morgan

Shared publicly  - 
1
Add a comment...

Josh Morgan

Shared publicly  - 
 
Nice Knot!
1
Add a comment...
Have him in circles
183 people
Doris Spencer's profile photo
Mahmood Toosi's profile photo
AZ Social Media Wiz LLC's profile photo
Read Books Online Free's profile photo
The Definitive Guides To Google+'s profile photo
Sarah Williams's profile photo
James Wilkins's profile photo
zenon pawelski's profile photo
Bill Davis's profile photo

Josh Morgan

General Discussion  - 
 
Taking a Introduction to Linux/Unix class for summer. Any thoughts on this text we will be using?
21
1
David C's profile photoJosh Morgan's profile photoJay Vollmer's profile photoTimm Drossart's profile photo
10 comments
 
The Sobell books are always worthwhile.
Add a comment...

Josh Morgan

commented on a video on YouTube.
Shared publicly  - 
 
Buzzfeed, world's biggest life waster. I love it!
4
Add a comment...

Josh Morgan

Shared publicly  - 
 
I won candy on the #googlebirthday doodle! Score: 145
1
Add a comment...

Josh Morgan

Shared publicly  - 
1
Add a comment...

Josh Morgan

Shared publicly  - 
1
Add a comment...
People
Have him in circles
183 people
Doris Spencer's profile photo
Mahmood Toosi's profile photo
AZ Social Media Wiz LLC's profile photo
Read Books Online Free's profile photo
The Definitive Guides To Google+'s profile photo
Sarah Williams's profile photo
James Wilkins's profile photo
zenon pawelski's profile photo
Bill Davis's profile photo
Work
Occupation
I build other peoples ideas with my hands.
Employment
  • Sun Automation Group
    Mechanic, 2007 - present
Places
Map of the places this user has livedMap of the places this user has livedMap of the places this user has lived
Previously
Baltimore, Md.
Links
Other profiles
Contributor to
Story
Tagline
Ego maniac with an inferiority complex
Education
  • Anne Arundel Community College
    Computer Information Systems - Programming, Analysis/Design, 2012 - present
Basic Information
Gender
Male