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.
185 followers|81,152 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
Carlos Broncano's profile photoMi-e Foame's profile photoAxel Engeland (Jejerod)'s profile photojRome Martin'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...
Have him in circles
185 people
Sameer Shaikh's profile photo
Jeannie Walker's profile photo
Miyamoto Musashi's profile photo
Bill Davis's profile photo
Pilar Revilla Mendez's profile photo
Chelsea Bynum-McKellum's profile photo
Justin Williams's profile photo
Diamond House's profile photo
Ma Ig'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
Robert Main's profile photoDavid C's profile photoJosh Morgan's profile photoJay Vollmer'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!

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...

Josh Morgan

Shared publicly  - 
 
Nice Knot!
1
Add a comment...
People
Have him in circles
185 people
Sameer Shaikh's profile photo
Jeannie Walker's profile photo
Miyamoto Musashi's profile photo
Bill Davis's profile photo
Pilar Revilla Mendez's profile photo
Chelsea Bynum-McKellum's profile photo
Justin Williams's profile photo
Diamond House's profile photo
Ma Ig's profile photo
Education
  • Anne Arundel Community College
    Computer Information Systems - Programming, Analysis/Design, 2012 - present
Basic Information
Gender
Male
Apps with Google+ Sign-in
  • Vainglory
Story
Tagline
Ego maniac with an inferiority complex
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