LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  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,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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



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
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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