LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-11-2012, 09:18 AM   #1
dpnctl
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Rep: Reputation: 0
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
:-)
 
Old 12-11-2012, 09:29 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,242
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 12-12-2012, 04:27 AM   #3
dpnctl
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 12-12-2012, 04:49 AM   #4
dpnctl
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
I did some awk and regenerated output I required, thank you a lot for your quick help
 
Old 12-12-2012, 07:56 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,242
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 12-12-2012, 05:24 PM   #6
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,047

Rep: Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953
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 ...
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
scripting linux@cpu-computers.com Linux - Newbie 7 11-13-2009 10:02 PM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
Scripting in C - need help hifivoyager Linux - Newbie 9 02-06-2009 08:47 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration