Solved

SHELL SCRIPT:-How read the variable line by line!!! urgent

Posted on 2010-09-01
13
463 Views
Last Modified: 2013-12-25
Example:-
The variable name is "StringVal"

StringVal=
"1^BGood^Bok good boy^BY2k^B1392830^B
 2^BGood^Bok good man^BY2i^B1392822^B
 3^BGood^Bnok good girl^BY2p^B1392811^B
 4^BGood^Bnok good woman^BY2j^B1392866^B
 5^BGood^Bok good boy^BY2r^B1392899^B
 6^BGood^Bnok good boy^BY2n^B1392855^B
 7^BGood^Bok good boy^BY2s^B1392833^B
 8^BGood^Bnok good girl^BY2w^B13900000^B"



I wan to get the line 4 to line 6

getResult=
"4^BGood^Bnok good woman^BY2j^B1392866^B
 5^BGood^Bok good boy^BY2r^B1392899^B
 6^BGood^Bnok good boy^BY2n^B1392855^B"


Anyone know what is the command for Shell script?
0
Comment
Question by:ryanW
  • 7
  • 5
13 Comments
 
LVL 6

Expert Comment

by:apresence
ID: 33574465
You can do this with sed.  See attached ssh session.
$ cat>test6.in
"1^BGood^Bok good boy^BY2k^B1392830^B
 2^BGood^Bok good man^BY2i^B1392822^B
 3^BGood^Bnok good girl^BY2p^B1392811^B
 4^BGood^Bnok good woman^BY2j^B1392866^B
 5^BGood^Bok good boy^BY2r^B1392899^B
 6^BGood^Bnok good boy^BY2n^B1392855^B
 7^BGood^Bok good boy^BY2s^B1392833^B
 8^BGood^Bnok good girl^BY2w^B13900000^B"
$ cat test6.in | sed -ne '4,6p'
 4^BGood^Bnok good woman^BY2j^B1392866^B
 5^BGood^Bok good boy^BY2r^B1392899^B
 6^BGood^Bnok good boy^BY2n^B1392855^B
$ 

Open in new window

0
 

Author Comment

by:ryanW
ID: 33574533
I am trying many times... but not work...
can you please to write more clearly and simple? i am new in shell script~~~
hihihi

Thank you
0
 
LVL 6

Expert Comment

by:apresence
ID: 33574550
How are you connecting to the remote system?  SSH or telnet?  Please post the output of your session including setting/retrieving the variable, and any manipulation you man be doing.

Please post whatever source code you have as well.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:ryanW
ID: 33574589
is SSH

i just wan to read into the variable...

TestVal=
" 1
  2
  3
  4
  5
  6
"


experted result=
" 4
  5
"
0
 
LVL 6

Expert Comment

by:apresence
ID: 33574774
Ryan, I'm sorry but what you provided adds no additional clarity.  You posted this to the CGI Scripting zone, so I assume you have some sort of CGI script.  Please post the code here.

How you do what you want to do is highly dependent upon which language your CGI script is written in, how you initialize your variables, etc.  This is why I am asking for the code.
0
 
LVL 6

Expert Comment

by:apresence
ID: 33574779
Also, I thought you wanted lines 4-6, not just 4-5?
0
 

Author Comment

by:ryanW
ID: 33574800
Here is my code... i need to get line 4 to line 6
then insert to database


#!/bin/bash

main()
{

SHY="Data^B2876478^BICN^B403291762523^B^B^B^BA^B02361074764^BES842^BFX013^B403291762523^BY^B^B ^BY^BSEL^BS^BJSPA^B^BREADY^BMRRDY
Data^B2876849^BICN^B405618282338^B^B^B^BA^B02361074764^BES842^BFX013^B405618282338^BY^B^B ^B^BSEL^BU^B^B^BREADY^BMRRDY
Data^B2875061^BICN^B405773988490^B^B^B^BA^B02361074764^BES842^BFX013^B405773988490^BY^B^B ^BY^BHIN^BU^B^B^BREADY^BMRRDY
Data^B2876630^BICN^B408616830491^B^B^B^BA^B02361074764^BES842^BFX013^B408616830491^BY^B^B ^BY^BSUK^BU^B^B^BREADY^BMRRDY
Data^B2876312^BICN^B408740336210^B^B^B^BA^B02361074764^BES842^BFX013^B408740336210^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
Data^B2876303^BICN^B408740336221^B^B^B^BA^B02361074764^BES842^BFX013^B408740336221^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
Data^B2876535^BICN^B411854019009^B^B^B^BA^B02361074764^BES842^BFX013^B411854019009^BY^B^B ^BY^BQUU^BU^B^B^BREADY^BMRRDY
Data^B2876315^BICN^B412054864133^B^B^B^BA^B02361074764^BES842^BFX013^B412054864133^BY^B^B ^BY^BSEL^BU^B^B^BREADY^BMRRDY
Data^B2869812^BICN^B412544151994^B^B^B^BA^B02361074764^BES842^BFX013^B412544151994^BY^B^B ^BY^BQTW^BU^B^B^BREADY^BMRRDY





}

main
0
 
LVL 6

Expert Comment

by:apresence
ID: 33574810
I'm not sure if this is what you want or not, but here's an attached script that demonstrates how to use the code.

Output:
$ ./test6.sh
4^BGood^Bnok good woman^BY2j^B1392866^B
5^BGood^Bok good boy^BY2r^B1392899^B
6^BGood^Bnok good boy^BY2n^B1392855^B
$
#!/bin/sh
cat > test6.in << EOF
1^BGood^Bok good boy^BY2k^B1392830^B
2^BGood^Bok good man^BY2i^B1392822^B
3^BGood^Bnok good girl^BY2p^B1392811^B
4^BGood^Bnok good woman^BY2j^B1392866^B
5^BGood^Bok good boy^BY2r^B1392899^B
6^BGood^Bnok good boy^BY2n^B1392855^B
7^BGood^Bok good boy^BY2s^B1392833^B
8^BGood^Bnok good girl^BY2w^B13900000^B
EOF
sed -ne '4,6p' test6.in

Open in new window

0
 
LVL 6

Expert Comment

by:apresence
ID: 33574822
I saved your bash script to test6.bash on my system, and it looks like you are missing your closing '"':

$ ./test6.bash
./test6.bash: line 6: unexpected EOF while looking for matching `"'
./test6.bash: line 24: syntax error: unexpected end of file
$
0
 

Author Comment

by:ryanW
ID: 33574824
to read the line is flexible one.. sometimes may read from line 2 to line 10
 or line 45 to line 60
After read, it will store in the variable.
Then i can use the variable to do anything i wan. update to database, or listing to the client page.


i just don know how to command the read line.


0
 
LVL 6

Accepted Solution

by:
apresence earned 500 total points
ID: 33574867
Well, I'm still not completly clear on what you want here.  However, I corrected your bash code, and also added a line number prefix "N:" in front just to help for debugging, and tested.  Here is the output:

$ ./test6.bash
4:Data^B2876630^BICN^B408616830491^B^B^B^BA^B02361074764^BES842^BFX013^B408616830491^BY^B^B ^BY^BSUK^BU^B^B^BREADY^BMRRDY
5:Data^B2876312^BICN^B408740336210^B^B^B^BA^B02361074764^BES842^BFX013^B408740336210^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
6:Data^B2876303^BICN^B408740336221^B^B^B^BA^B02361074764^BES842^BFX013^B408740336221^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
$
#!/bin/bash

main()
{

SHY="1:Data^B2876478^BICN^B403291762523^B^B^B^BA^B02361074764^BES842^BFX013^B403291762523^BY^B^B ^BY^BSEL^BS^BJSPA^B^BREADY^BMRRDY
2:Data^B2876849^BICN^B405618282338^B^B^B^BA^B02361074764^BES842^BFX013^B405618282338^BY^B^B ^B^BSEL^BU^B^B^BREADY^BMRRDY
3:Data^B2875061^BICN^B405773988490^B^B^B^BA^B02361074764^BES842^BFX013^B405773988490^BY^B^B ^BY^BHIN^BU^B^B^BREADY^BMRRDY
4:Data^B2876630^BICN^B408616830491^B^B^B^BA^B02361074764^BES842^BFX013^B408616830491^BY^B^B ^BY^BSUK^BU^B^B^BREADY^BMRRDY
5:Data^B2876312^BICN^B408740336210^B^B^B^BA^B02361074764^BES842^BFX013^B408740336210^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
6:Data^B2876303^BICN^B408740336221^B^B^B^BA^B02361074764^BES842^BFX013^B408740336221^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
7:Data^B2876535^BICN^B411854019009^B^B^B^BA^B02361074764^BES842^BFX013^B411854019009^BY^B^B ^BY^BQUU^BU^B^B^BREADY^BMRRDY
8:Data^B2876315^BICN^B412054864133^B^B^B^BA^B02361074764^BES842^BFX013^B412054864133^BY^B^B ^BY^BSEL^BU^B^B^BREADY^BMRRDY
9:Data^B2869812^BICN^B412544151994^B^B^B^BA^B02361074764^BES842^BFX013^B412544151994^BY^B^B ^BY^BQTW^BU^B^B^BREADY^BMRRDY"
}

main

line=0
IFS=$'\n'
for i in $SHY; do
  line=`expr $line + 1`
  if [ $line -ge 4 -a $line -le 6 ]; then
    echo $i
  fi
done

Open in new window

0
 

Author Comment

by:ryanW
ID: 33575066
Apresence!!!!!!!!!!!!!!!!!!

I want to loudly say to you with THREE words !!!~~~

YOu are Genius....!!! hahahahaha

thanks, it can work... sorry for reply u late.. coz i am testing the program.

HERE is my code
________________________________________
fff="1"
ggg="15"

line=0
IFS=$'\n'
for i in $allData; do
  line=`expr $line + 1`
  if [ $line -ge $fff -a $line -le $ggg ]; then
    result=$result$i"\n"
  fi
done

allData="$result"

echo "DBMS_OUTPUT.PUT_LINE(
$allData
sql done);"
_________________________________________

allData <-- which is select from database... the record may be is hundred or thousand.
Why i need to read for certain line, is because i need to get 15 lines for every page to display.
like got 1000 records in $allData
first time i need to read 1 to 15 lines to display, when user click to next page, it will get read from line 16 to line 31 until last page.

Thank you.. anywhere...
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33575715
You could have simply done
#!/bin/bash
SHY="1:Data^B2876478^BICN^B403291762523^B^B^B^BA^B02361074764^BES842^BFX013^B403291762523^BY^B^B ^BY^BSEL^BS^BJSPA^B^BREADY^BMRRDY
2:Data^B2876849^BICN^B405618282338^B^B^B^BA^B02361074764^BES842^BFX013^B405618282338^BY^B^B ^B^BSEL^BU^B^B^BREADY^BMRRDY
3:Data^B2875061^BICN^B405773988490^B^B^B^BA^B02361074764^BES842^BFX013^B405773988490^BY^B^B ^BY^BHIN^BU^B^B^BREADY^BMRRDY
4:Data^B2876630^BICN^B408616830491^B^B^B^BA^B02361074764^BES842^BFX013^B408616830491^BY^B^B ^BY^BSUK^BU^B^B^BREADY^BMRRDY
5:Data^B2876312^BICN^B408740336210^B^B^B^BA^B02361074764^BES842^BFX013^B408740336210^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
6:Data^B2876303^BICN^B408740336221^B^B^B^BA^B02361074764^BES842^BFX013^B408740336221^BY^B^B ^BY^BJSP^BS^BJSPA^B^BREADY^BMRRDY
7:Data^B2876535^BICN^B411854019009^B^B^B^BA^B02361074764^BES842^BFX013^B411854019009^BY^B^B ^BY^BQUU^BU^B^B^BREADY^BMRRDY
8:Data^B2876315^BICN^B412054864133^B^B^B^BA^B02361074764^BES842^BFX013^B412054864133^BY^B^B ^BY^BSEL^BU^B^B^BREADY^BMRRDY
9:Data^B2869812^BICN^B412544151994^B^B^B^BA^B02361074764^BES842^BFX013^B412544151994^BY^B^B ^BY^BQTW^BU^B^B^BREADY^BMRRDY"

echo "$SHY" | sed -n 4,6p

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question