LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-16-2017, 09:01 PM   #1
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 10 (Buster)
Posts: 3,373

Rep: Reputation: 138Reputation: 138
replace any line in all files that contains "string <something>" with "string something else"


I have a directory of files; each of which will have a line with some string "xxx" which may or may not be followed by some text e.g. "xxx a b c" or just "xxx". What I'd like to do is to replace these lines, regardless of whether they have text, with a specific string; which is something like "xxx 1 2 3".

There's probably something out there in internet land that does what I want, but I just wasn't able to understand it when I ran across it.

Thanks in advance for any suggestions not of the form "RTFM and learn how to use sed and regular expressions". =) I do this so infrequently that it's enough that know that it can be done and can actually do some of the simpler stuff from examples I already have.
 
Old 04-17-2017, 07:07 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,793

Rep: Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713Reputation: 1713
Quote:
Originally Posted by Quakeboy02 View Post
I have a directory of files; each of which will have a line with some string "xxx" which may or may not be followed by some text e.g. "xxx a b c" or just "xxx". What I'd like to do is to replace these lines, regardless of whether they have text, with a specific string; which is something like "xxx 1 2 3".

There's probably something out there in internet land that does what I want, but I just wasn't able to understand it when I ran across it.

Thanks in advance for any suggestions not of the form "RTFM and learn how to use sed and regular expressions". =) I do this so infrequently that it's enough that know that it can be done and can actually do some of the simpler stuff from examples I already have.
Then you will be happy to know that this CAN be done with MANY tools, including but not,limited to SED, GSAR, PERL, Python, PHP, Pascal, C, C++, and possibly even advanced shell scripting (bash perhaps).
 
Old 04-17-2017, 10:43 AM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,129

Rep: Reputation: 42
Quote:
Originally Posted by Quakeboy02 View Post
I have a directory of files; each of which will have a line with some string "xxx" which may or may not be followed by some text e.g. "xxx a b c" or just "xxx". What I'd like to do is to replace these lines, regardless of whether they have text, with a specific string; which is something like "xxx 1 2 3".

There's probably something out there in internet land that does what I want, but I just wasn't able to understand it when I ran across it.

Thanks in advance for any suggestions not of the form "RTFM and learn how to use sed and regular expressions". =) I do this so infrequently that it's enough that know that it can be done and can actually do some of the simpler stuff from examples I already have.
Is "xxx" necessarily in the beginning of a line?

If yes, you need this regex:

/^xxx.*$

If not, this one:

/xxx.*$

Things you want to understand in these regexes, one symbol per line

^
. (or \., it depends on the tool or way you write the regex, it may need to be escaped)
* (or \*, it depends on the tool or way you write the regex, it may need to be escaped)
$

After you can find what you need, you use some "()" or "\(\)" to remember the parts you need and put it in the replacement expression with something like:

s/^\(xxx\).*$/\1 1 2 3/ge

I have made a test file:

Code:
qweqewqeqw xxx lkjlklkj
xxx oiuoiuoiu
xxx 
lasjkd as lkadsj k jalsd ja
aslkd a lkadsj dlaj dlask jda
a xxx
xxx
Note the fourth line has a space in the end!

And the replacement command with sed, as I could imagine you want. See these lines of my terminal, showing the commands I used (lines starting with "$") and their output (other lines).

Code:
$ cat xxx

qweqewqeqw xxx lkjlklkj
xxx oiuoiuoiu
xxx 
lasjkd as lkadsj k jalsd ja
aslkd a lkadsj dlaj dlask jda
a xxx
xxx


$sed 's/^\(xxx\).*$/\1 1 2 3/g' xxx 

qweqewqeqw xxx lkjlklkj
xxx 1 2 3
xxx 1 2 3
lasjkd as lkadsj k jalsd ja
aslkd a lkadsj dlaj dlask jda
a xxx
xxx 1 2 3

$
 
Old 04-17-2017, 11:14 AM   #4
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 10 (Buster)
Posts: 3,373

Original Poster
Rep: Reputation: 138Reputation: 138
Hi dedec0,

Thanks! I'll see what I can do with this and get back to you. Yes, the string is at the beginning of the line. Well, it might follow a tab or some blanks, but that's a mere detail.

What is the backslash 1? I think I can figure out the rest.
 
Old 04-17-2017, 12:06 PM   #5
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 10 (Buster)
Posts: 3,373

Original Poster
Rep: Reputation: 138Reputation: 138
OK, I'm marking it as solved. Your example worked perfectly. I created a simple shell script as below, and it changed the files exactly as needed. Thanks again!

Code:
#!/bin/sh
for file in abc/*.def
do
sed -i 's/\(something\).*$/\1 \/etc\/somewhere\/someval/g'  $file
done
 
Old 04-17-2017, 02:48 PM   #6
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,129

Rep: Reputation: 42
Great to know it worked! (:

But there is something missed here: the spaces you may have before the "xxx" must be written! "^" Is the imaginary place before the first thing in the line (include spaces!). To have optional spaces at the start of lines, use something like this:

Code:
$ sed 's/^\s*\(xxx\).*$/\1 1 2 3/g' filename
\s is any kind of space: spaces, tabs, newlines, ...

\1 means to write "the first thing we asked to remember". In this command, we have put the "xxx" among the special parenthesis, so that is what we are asking to repear. Put another \1 after the 2 and you will see it clearly.

Read a fairly small (but not too much) regex tutorial. It should give you many more ideas. And do not forget to learn about the special parts I have pointed in my first post.
 
Old 04-17-2017, 03:30 PM   #7
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 10 (Buster)
Posts: 3,373

Original Poster
Rep: Reputation: 138Reputation: 138
Hi. As it turned out, there wasn't anything in front of the string I needed to change. As far as I can tell from spot checking the script I made changed over 1600 files to the correct value, including some that had already been manually set. After the spot check, I made those the active files, and everything seems to be OK.
 
  


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
Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml" Hiroshi Programming 5 02-18-2017 03:22 AM
[SOLVED] Replace a string of blanks with a "tailored" string of equal length danielbmartin Programming 6 10-16-2015 11:07 AM
[SOLVED] Replace an "entire" line containing a particular string with a new line kushalkoolwal Programming 4 03-12-2010 04:04 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-04-2008 11:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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