ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi guys , i am not sure if this is the correct topic name to use , however if you have a better name to give to it then let me know and i will change the topic name for the doubt i have here .
Problem :
i have a file with the following numbers :
Quote:
18
27
57
4
67
I need the code to check all these lines and in the end give me this output :
Quote:
1245678
All the output numbers exist in the read file and they are in order and not repeated .
This means that the code will check every single character of each line and start put them in order as output .
I can think of a couple of quick ways using simply a shell, or with awk (any scripting language would do actually).
Either would essentially set up an indexed array with the digits 0 through 9 as index, then process each numeric character so as to increment the corresponding element until end of file is reached. At end of file simply write out the index of each non-zero array element in order 0 through 9.
Give that a try and see what you can come up with!
best way i could think doing it is to grep each number between 1 and 8 with a "for" statement and if grep output returns true then start adding those numbers in an output variable .
best way i could think doing it is to grep each number between 1 and 8 with a "for" statement and if grep output returns true then start adding those numbers in an output variable .
Sure, you could do that with grep, but will the numbers always be between 1 and 8?
#!/bin/bash
path=$(pwd)
file="$path/list"
outvar=""
if [[ ! -f "$file" ]]
then
echo "Output file does not exist"
exit 1
fi
for i in 1 2 3 4 5 6 7 8
do
chknmb=$(grep $i < "$file")
if [[ ! -z "$chknmb" ]]
then
outvar="$outvar$i"
fi
done
echo "Output is : $outvar"
create a file called "list" with random numbers in multiple lines between 1 and 8
my test file
Quote:
28
47
24
478
2
456
48
24
1
Quote:
Sure, you could do that with grep, but will the numbers always be between 1 and 8
Yup , that is what i need , eventually you can set up between 0 and 9 , nothing more can be done because there are no spaces between numbers and i don't need it .
You don't really need the file redirection, or chknum or outvar, if you only want the numbers:
Code:
#!/bin/bash
path=$(pwd)
file="$path/list"
if [[ ! -f "$file" ]]
then
echo "Output file does not exist"
exit 1
fi
for i in 1 2 3 4 5 6 7 8
do
if [[ ! -z "$(grep $i "$file")" ]]
then
echo -n $i
fi
done
echo ""
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.