Register
It is currently Fri Oct 24, 2014 12:04 pm

[requset] script that search through multiple files to edit.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Fri Nov 26, 2010 7:21 am   

Joined: Sat May 15, 2010 10:43 am
Posts: 8
Hi I'm looking for a script that searches through multiple files for a string and the line where this string is found should be moved three lines down, for example:

Code:

define service{
        use                     generic-service
        host_name               server1
        service_description     check_backup
        check_command           check_backup  ; The command used to monitor the service
        servicegroups           check_mysqlbackup
        }


now what I want is for each time the script finds "host_name " it should move this complete line three rows down like this:

Code:
define service{
        use                     generic-service
        service_description     check_backup
        check_command           check_backup  ; The command used to monitor the service
        servicegroups           check_mysqlbackup
        host_name               server1
        }


I have hundreds of these files and each file have host_name in it multiple time, I would be truly greatful if this could be automated somehow. Thanks in advance!


Top
 Profile  
 PostPosted: Fri Nov 26, 2010 4:12 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
No easy way, I fear. You have to write the whole code that opens each file, reads it line by line and prints the line unless it is the line to move, in which case a counter is initialized and the line is remembered to be printed three lines later. I would probably use perl for that to make sure no host_name gets moved out of the curly brackets etc.


Top
 Profile  
 PostPosted: Sat Nov 27, 2010 2:25 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Seems to work but currently removes the tab formatting, it's something to start from :)

Code:
for file in $(grep -l host_name *)
do
  while read line
  do
    if echo $line | grep -q host_name
    then
      read line2
      read line3
      echo $line2
      echo $line3
      echo $line
    else
      echo $line
    fi
  done < "${file}" > "${file}.new"
done


Top
 Profile  
 PostPosted: Sat Nov 27, 2010 4:21 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Same principle as DarthWavy, except in awk. Keeping any (tab)formatting as-is and possibly quicker than a 'read line' shell loop.
I'm not sure you want to move the host_name line '3 down' though. Perhaps you want to move it to the end of the section, before the closing curly bracket?

Code:
for file in *; do
  awk '{
    if ($1 ~ /host_name/) {
      tmp = $0;
      getline; print;
      getline; print;
      getline; print;
      print tmp;
    } else print;
  }' < "${file}" > "${file}.new"
done


Top
 Profile  
 PostPosted: Sun Nov 28, 2010 3:20 am   

Joined: Sat May 15, 2010 10:43 am
Posts: 8
Patsie wrote:
Same principle as DarthWavy, except in awk. Keeping any (tab)formatting as-is and possibly quicker than a 'read line' shell loop.
I'm not sure you want to move the host_name line '3 down' though. Perhaps you want to move it to the end of the section, before the closing curly bracket?

Code:
for file in *; do
  awk '{
    if ($1 ~ /host_name/) {
      tmp = $0;
      getline; print;
      getline; print;
      getline; print;
      print tmp;
    } else print;
  }' < "${file}" > "${file}.new"
done


Thanks!I think having it right over the closing curly bracket would work great! might be more error proof if the syntax is of in some cases! would be great if you could edit the script, thanks a bunch! I love this site! (and everyone who posted their suggestions!! thanks!! :) ):)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP