LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2019, 03:10 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
check and add in order bash


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 .

Is there a simple way to do this ?

Last edited by pedropt; 05-12-2019 at 03:11 PM.
 
Old 05-12-2019, 03:20 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
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!
 
Old 05-12-2019, 03:33 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 05-12-2019, 03:36 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by pedropt View Post
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?
 
Old 05-12-2019, 03:54 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Ok , solved my issue .

Code:
#!/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 .

Last edited by pedropt; 05-12-2019 at 03:56 PM.
 
1 members found this post helpful.
Old 05-12-2019, 04:00 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
That is a good solution!

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 ""

Last edited by astrogeek; 05-12-2019 at 04:07 PM.
 
1 members found this post helpful.
Old 05-12-2019, 04:36 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
18
27
57
4
67
... this code ...
Code:
fold -w1 $InFile |sort -u |tr -d "\n" >$OutFile
... produced this OutFile ...
Code:
1245678
Daniel B. Martin

.

Last edited by danielbmartin; 05-12-2019 at 04:39 PM. Reason: Tighten the code, slightly.
 
3 members found this post helpful.
Old 05-12-2019, 06:02 PM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Great codes both of you , thanks a lot .
 
Old 05-13-2019, 02:53 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Try not to over complicate issues with notting a negative:
Code:
[[ ! -z "..." ]]
Simply use the positive test:
Code:
[[ -n "..." ]]
 
Old 05-15-2019, 07:16 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,818

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Or test the exit status
Code:
  if grep -q $i "$file"
  then

Last edited by MadeInGermany; 05-15-2019 at 07:18 PM. Reason: Dup proposal removed
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can I set the cups output order to always print in reverse order Thane Ubuntu 3 07-09-2018 09:49 PM
Logi Sales Manager on Ncurses (invoice, invoicing, orders, order, sale order, sales order...)? Xeratul Linux - Software 0 03-25-2017 02:45 PM
Copy files in order alphabetical order nuxguy Linux - General 13 11-07-2014 05:32 AM
How can this list be sorted in date order of YYYY-MMM-DD order. Glenn D. Linux - Software 4 06-26-2014 12:08 AM
System suddenly changing disk order from BIOS order hscast Fedora 2 02-16-2010 09:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration