Solved

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

Posted on 2010-09-01
13
453 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now