Reading a variable line by line with while loop

Ray Parrish crp at cmc.net
Tue Dec 1 17:03:35 UTC 2009


Avi Greenbury wrote:
> Ray Parrish wrote:
>
>   
>> Smoot Carl-Mitchell wrote:
>> Thank you! That worked great! Now I wonder is there is another great
>> sed command that will remove redundancy in the bash history, so no
>> command line is repeated in the final results in the $History
>> variable?
>>     
>
> man uniq
>
> which requires you to sort it, too:
>
> man sort
>
> since it checks for repeated (consecutive) lines. If order matters,
> you'll need something more complicated. 
>
> sed can probably be made to do it, but piping through sort and uniq is
> probably nicer.
>
>
> --
> Avi Greenbury
> http://aviswebsite.co.uk ;)
> http://aviswebsite.co.uk/asking-questions
>   
Thank you very much for the pointers! The following amended code is 
working great!

History=""
# Sort the bash history into a temporary file so uniq can eliminate all 
duplicate lines.
sort -d ~/.bash_history >"$DirectoryName/BashHistory.tmp"
# Read that file into a variable, and replace all space characters in 
the file with __ [double underlines]
History=`sed 's/ /__/g' "$DirectoryName/BashHistory.tmp"`
# Write amended history back to temp file.
echo "$History" >"$DirectoryName/BashHistory.tmp"
# Eliminate redundant lines in the History variable.
History=`uniq -u "$DirectoryName/BashHistory.tmp"`

I didn't really want to write it back to file again, but I couldn't 
figure out how to read from the $History variable with the uniq command, 
I got the error that there was an extra parameter when i used the 
following line for uniq.

HIstory=`unig -u $History`
uniq: extra operand `./AddURLtoSitemap.sh'

I would rather keep the data in the variable and work on it there, 
instead of writing it back to file just to read it off the disk again. 
That's a bit inefficient.

Later, Ray Parrish

-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list