string manipulation with tabs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string manipulation with tabs
# 1  
Old 02-17-2009
Question string manipulation with tabs

Hi there,
I'm using the result of an sql query in a script:
Code:
santiago@videos:~$ mysql ndtv -e 'SELECT id, ip FROM terminal' | sed '1d'
3       172.16.100.162
4       172.16.121.151
5       172.16.121.152
6       172.16.146.151
7       172.16.146.152
14      172.16.150.151
17      172.16.121.154
19      172.16.121.153

But I'd like to separate each column. And the following doesn't work:
Code:
santiagoo@videos:~$ cat myscript
mysql ndtv -e 'SELECT id, ip FROM terminal' | sed '1d' | while IFS= read -r line; do
    echo "Terminal ${line%%\t*} (${line##*\t})"
done
santiago@videos:~$ ./mycript
Terminal 3      172.16.100.162 (3       172.16.100.162)
Terminal 4      172.16.121.151 (4       172.16.121.151)
Terminal 5      172.16.121.152 (5       172.16.121.152)
Terminal 6      172.16.146.151 (6       172.16.146.151)
Terminal 7      172.16.146.152 (7       172.16.146.152)
Terminal 14     172.16.150.151 (14      172.16.150.151)
Terminal 17     172.16.121.154 (17      172.16.121.154)
Terminal 19     172.16.121.153 (19      172.16.121.153)

What I expected was:
Code:
Terminal 3 (172.16.100.162)
Terminal 4 (172.16.121.151)
Terminal 5 (172.16.121.152)
Terminal 6 (172.16.146.151)
Terminal 7 (172.16.146.152)
Terminal 14 (172.16.150.151)
Terminal 17 (172.16.121.154)
Terminal 19 (172.16.121.153)

Why can't I use tabs in string manipulations?
Since ${line%%\t*} doesn't work, what's the correct syntax?

Thanks for your help
Santiago
# 2  
Old 02-17-2009
Code:
#!/bin/ksh

ndtv -e 'SELECT id, ip FROM terminal' | sed '1d' | while read -r f1 f2
do
  printf "Terminal %s\t(%s)\n" "${f1}" "${f2}"
done

# 3  
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question