Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

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

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
ryanW
Asked:
ryanW
  • 7
  • 5
1 Solution
 
apresenceCommented:
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
 
ryanWAuthor Commented:
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
 
apresenceCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ryanWAuthor Commented:
is SSH

i just wan to read into the variable...

TestVal=
" 1
  2
  3
  4
  5
  6
"


experted result=
" 4
  5
"
0
 
apresenceCommented:
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
 
apresenceCommented:
Also, I thought you wanted lines 4-6, not just 4-5?
0
 
ryanWAuthor Commented:
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
 
apresenceCommented:
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
 
apresenceCommented:
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
 
ryanWAuthor Commented:
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
 
apresenceCommented:
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
 
ryanWAuthor Commented:
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
 
TintinCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now