LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   help on scripting (http://www.linuxquestions.org/questions/programming-9/help-on-scripting-4175440976/)

dpnctl 12-11-2012 09:18 AM

help on scripting
 
hi All,
one output of my script, will have a format like this
server_name_1
date ID1
date ID2
date ID3
server_name_2
date ID1
date ID2
date ID3
server_name_3
date ID1
date ID2
date ID3

This format being common, is it possible to write a script like this:

from server_name_1 till server_name_2 all "date ID" entries should be modified something like this (which will be a command to perform an action)

ls server_name_1 date ID1

best regards
:-)

konsolebox 12-11-2012 09:29 AM

If those are your patterns you could do it like this:
Code:

#!/bin/bash
while read line; do
    if [[ $line == server_name* ]]; then
        server_name=$line
    elif [[ $line == date* ]]; then
        [[ -n $server_name ]] && echo "ls $server_name $line"
    fi
done

Code:

bash script.sh < input > output

dpnctl 12-12-2012 04:27 AM

special characrters in my content
 
thanQ very much for quick reply, however, the result is not as expected, I am sorry for some incomplete information -- missed the forward slash.

content of my file:
/server_name_1/client_1
date ID1
date ID2
date ID3

Now this file will be input to another bash script (which I am seeking help on), upon running which the output should be like this:

ls /server_name_1/client1 date ID1
ls /server_name_1/client1 date ID2
ls /server_name_1/client1 date ID3

dpnctl 12-12-2012 04:49 AM

I did some awk and regenerated output I required, thank you a lot for your quick help

konsolebox 12-12-2012 07:56 AM

You just have to tweak the pattern a bit if that's the case.
Code:

#!/bin/bash
while read line; do
    if [[ $line == /server_name* ]]; then
        server_name=$line
    elif [[ $line == date* ]]; then
        [[ -n $server_name ]] && echo "ls $server_name $line"
    fi
done


sundialsvcs 12-12-2012 05:24 PM

Also bear in mind that you have literally dozens of programming language tools at your disposal in Linux, and the same "shebang" facility that you used to specify #!/bin/bash can be used to invoke any of them. The commands will all magically work. So, the moment you start getting into anything that's even the slightest bit complicated ... think PHP, Perl, Python, Ruby, Haskell ...


All times are GMT -5. The time now is 06:44 AM.