Sponsored Content
Top Forums Shell Programming and Scripting string manipulation with tabs Post 302288790 by chebarbudo on Wednesday 18th of February 2009 03:09:12 AM
Old 02-18-2009
Thanks vgersh99 for your help.
Unfortunately, this doesn't work with bash and I'm not allowed to install other shells.
But I came up with this new solution:
Code:
patricio@videos:~$ cat myscript
for record in $(mysql ndtv -e 'SELECT CONCAT(id, "-", ip) FROM terminal ORDER BY ip' | sed '1d'); do
    ip=${record##*-}
    id=${record%%-*}
    echo -en "================================================================================\15"
    echo -n "Terminal $id ($ip) "
    if ping -c1 -w1 $ip > /dev/null 2>&1; then
        echo
        ssh root@$ip "$1";
    else
        echo 'UNREACHABLE '
    fi
done
patricio@videos:~$ ./myscript 'hostname'
Terminal 3 (172.16.100.162) ====================================================
terminal3
Terminal 4 (172.16.121.151) ====================================================
terminal4
Terminal 5 (172.16.121.152) ====================================================
terminal5
Terminal 19 (172.16.121.153) ===================================================
terminal18
Terminal 17 (172.16.121.154) UNREACHABLE =======================================
Terminal 6 (172.16.146.151) ====================================================
terminal6
Terminal 7 (172.16.146.152) ====================================================
terminal7
Terminal 14 (172.16.150.151) UNREACHABLE =======================================
patricio@videos:~$

Fot some reason that I don't understand, if I keep using while, only the first line is parsed:
Code:
patricio@videos:~$ cat myscript
mysql -u ndtv -pndtv ndtv -e 'SELECT CONCAT(id, "-", ip) FROM terminal ORDER BY ip' | sed '1d' | while IFS= read -r record; do
    ip=${record##*-}
    id=${record%%-*}
    echo -en "================================================================================\15"
    echo -n "Terminal $id ($ip) "
    if ping -c1 -w1 $ip > /dev/null 2>&1; then
        echo
        ssh root@$ip "$1";
    else
        echo 'UNREACHABLE '
    fi
done
patricio@videos:~$ ./myscript 'hostname'
Terminal 3 (172.16.100.162) ====================================================
terminal3
patricio@videos:~$

Can you identify the problem?
Santiago
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

string manipulation

Hi, I have searched this long and hard and don't seem to see another post on this issue. I have two strings each with the same characters but in a different order. String1=”word” String2=”dwor” I want to test them to show their similarity. Unfortunately I can't do a sort so that they will... (9 Replies)
Discussion started by: Cactus Jack
9 Replies

2. UNIX for Dummies Questions & Answers

string manipulation

Hi, I have a file with rows of text like so : E100005568374098100000015667 D100005568374032000000112682 H100005228374060800000002430 I need to grab just the last digits(bolded) of each line without the proceeding text/numbers. Thanks (5 Replies)
Discussion started by: james6
5 Replies

3. Shell Programming and Scripting

String manipulation

Hi, i am just gettin exposed to UNIX. Could anyone of u help me out with dis problem..? i have a variable 'act' which has the value as follows, echo $act gives -0- -0- -----0---- 2008-06-04 -0- -0- echo "$act" | awk '{print ($act)}' gives, -0- -0- -----0---- 2008-06-04 -0- -0- I... (2 Replies)
Discussion started by: jerrynimrod
2 Replies

4. UNIX for Dummies Questions & Answers

String manipulation

I am doing some training for a job I have just got and there is an exercise I am stuck with. I am not posting to ask a question about logic, just a trivial help with string manipulation. I would appreciate if somebody could at least give me a hint on how to do it. Basically, the intelligent part... (8 Replies)
Discussion started by: Dantastik
8 Replies

5. Shell Programming and Scripting

string manipulation

i have a file that contains a pattern like this: ajay 1234 newyork available kumar 2345 denver singh 2345 newyork ajay 3456 denver kumar 3456 newyork singh 3456 delhi available ajay 4567 miami kumar 4567 miami singh 4567 delhi i want to search for each line... (5 Replies)
Discussion started by: ajay41aj
5 Replies

6. Homework & Coursework Questions

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. Homework... (0 Replies)
Discussion started by: shashwat2691
0 Replies

7. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

8. Shell Programming and Scripting

String Manipulation

I'm making a little game in Perl, and I am trying to remove the first instance of a character in an arbitrary string. For example, if the string is "cupcakes"and the user enters another string that contains letters from "cupcake" e.g: "sake"the original string will now look like this (below)... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

9. Shell Programming and Scripting

String manipulation.

If a have a variable with a first and last name. and say the variable looks like this... FIRST LAST how could process the variable to look like First .L bash 3.2 (osx) (3 Replies)
Discussion started by: briandanielz
3 Replies

10. UNIX for Beginners Questions & Answers

How to check string contain multiple tabs or spaces?

str contains tabs and multiple spaces str="hello world. How are you?" I want to check string start with hello world, and my code is: if ]world"* ]]; then echo "found" else echo "not found" fi Not work Other solution may work is to replace all tabs and... (4 Replies)
Discussion started by: cmdcmd
4 Replies
COLORS(3)						   libbash colors Library Manual						 COLORS(3)

NAME
colors -- libbash library for setting tty colors. SYNOPSIS
colorSet <color> colorReset colorPrint [<indent>] <color> <text> colorPrintN [<indent>] <color> <text> DESCRIPTION
General colors is a collection of functions that make it very easy to put colored text on tty. The function list: colorSet Sets the color of the prints to the tty to COLOR colorReset Resets current tty color back to normal colorPrint Prints TEXT in the color COLOR indented by INDENT (without adding a newline) colorPrintN The same as colorPrint, but trailing newline is added Detailed interface description follows. Available colors: Green Red Yellow White The color parameter is non-case-sensitive (i.e. RED, red, ReD, and all the other forms are valid and are the same as Red). FUNCTIONS DESCRIPTIONS
colorSet <color> Sets the current printing color to color. colorReset Resets current tty color back to normal. colorPrint [<indent>] <color> Prints text using the color color indented by indent (without adding a newline). Parameters: <indent> The column to move to before start printing. This parameter is optional. If ommitted - start output from current cursor position. <color> The color to use. <color> The text to print. colorPrintN [<indent>] <color> The same as colorPrint, except a trailing newline is added. EXAMPLES
Printing a green 'Hello World' with a newline: Using colorSet: $ colorSet green $ echo 'Hello World' $ colorReset Using colorPrint: $ colorPrint 'Hello World'; echo Using colorPrintN: $ colorPrintN 'Hello World' AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <gil@ran4.net> SEE ALSO
ldbash(1), libbash(1) Linux Epoch Linux
All times are GMT -4. The time now is 08:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy