LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-05-2011, 10:47 AM   #1
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Rep: Reputation: 1
A shell script to search and replace strings on certain logic


Need help on a shell script that do the following.

Inputs

A file A that contains the following.

PHP Code:
OA ABC02
Blade 
#7 Status:
    
Unit Identification LEDOn
OA ABC02
Blade 
#1 Status:
    
Unit Identification LEDBlinking
OA BCA03
Blade 
#2 Status:
    
Unit Identification LEDOn
OA CDE03
Blade 
#10 Status:
    
Unit Identification LEDOn 
--------------------------------------------

A file B that contains the following

PHP Code:
[B]ABC02[/B]
a120
a2g2
a32s
a45e
a53a
a6ea
a74r
a8rd
a9df
a10d
a11d
a12r
a13r

[B]BCA03[/B]
b1aa
b11a
b14a
b5ds
b67d
b7ds
b8df
b9gh
b10g
b11f
b12e
b13f

[B]CDE03[/B]
c165
c277
c377
c433
c323
c090
c434
c832
c844
c145
c133
c132
c153 
I now need to replace the number that comes after the # (Blade #7 Status) on the first file by the name on the 7th line after the heading (ABC02)of that paragraph in the second file.




So that the final output should read.

OA ABC02
Blade #a74r Status:
Unit Identification LED: On
OA ABC02
Blade #a120 Status:
Unit Identification LED: Blinking
OA BCA03
Blade #b11a Status:
Unit Identification LED: On
OA CDE03
Blade #c145 Status:
Unit Identification LED: On



Any help would be highly appreciated

Last edited by rockie321; 05-19-2011 at 07:44 AM.
 
Old 05-06-2011, 02:21 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What have you tried so far? What is your skillness in shell programming? It would be better to see where you're stuck and try to answer specific questions, instead of creating a new script by scratch!
 
Old 05-06-2011, 02:22 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Well its crude, but something like this might get you going:
Code:
#!/bin/bash

while read -r line
do
    [[ $line =~ ^OA ]] && GROUP=${line#* }

    if [[ $line =~ ^Blade ]]
    then
        NUM=${line#*#}
        NUM=${NUM% *}
        RESULT=$(grep $GROUP -A$NUM fileB | tail -1)
        echo "Blade #$RESULT Status:" >> tmp
        continue
    fi

    echo "$line" >> tmp
done<fileA

mv tmp fileA
 
Old 05-18-2011, 09:00 AM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
rockie321,

Welcome to LQ. Hope your time here helps you as much as mine has helped me.

Please put code, command line output, config files, etc. inside [CODE] tags, aka "Code:" blocks.

It will make your posts easier to read, & that will get you more, faster, better answers. -- Help us help you.
BTW, You can edit your post(s) to do this retroactively. Please do this ASAP; well, ASAC (as soon as convenient).

I have been trying to follow this thread & contribute, but it's too difficult to read w/o the "Code:" blocks.

Thank you, & again, welcome.
 
Old 06-14-2011, 08:42 AM   #5
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
Thumbs up

Thanks again Grail. Got exactly what was needed
 
Old 06-16-2011, 04:04 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
rockie321,

Thanks for trying to comply w/ my request for "Code:" blocks; unfortunately, you used "PHP Code:" blocks, which are different, instead.

Would it be too much to ask you to try to fix them?
 
  


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
Shell script to search one file for contents of another and replace text? kmkocot Linux - Newbie 6 10-28-2011 02:09 PM
Search and replace strings in text files in C Completely Clueless Programming 5 09-15-2009 06:51 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM


All times are GMT -5. The time now is 12:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration