Ahmad Fikri Ahmad Fikri - 1 year ago 79
Bash Question

Shell Script (.sh file) to read a line and copy specific value to another text file in Android

I'm not so good in english, and I'm new in this

I need a shell script (.sh file) that can read a line and copy the specific value of the line, for example :

I have test.txt which is containing

One: 850456

Two: 15192

Inside test.txt show me 2 lines, i just need the
in the first line, and the number automatically written to another text file. If you can, the another text file is created from this .sh file too

Another problem, if the number is random, there is a way to copy it?

I don't know what is the different between Linux Shell Script and Android Shell Script. But this one, i need to run it from Android

Answer Source

If the file is in the format as you have shown:

one: 234234234
two: 23423423423
three: 23453453635
four: 2342342342423

Then you can use the below command easily without the need for a script

cat test.txt | cut -d":" -f2 | sed 's/ //g'

The output would be all the numbers from the file above. If you want to save this result to a file then use the below command.

cat test.txt | cut -d":" -f2 | sed 's/ //g' > result.txt

This will overwrite the file each time with the results.

For first line only you can use something like below

head -n 1 test.txt | cut -d":" -f2 | sed 's/ //g' > result.txt

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download