Reading a variable line by line with while loop

Ray Parrish crp at cmc.net
Tue Dec 1 17:42:00 UTC 2009


Ray Parrish wrote:
> 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
>   
Hello again,

I like the fact that it only took three lines of code to sort that out 
and reduce the redundancy, but I have decided the order of the original 
bash history is important, and am working on a way to test each line as 
read in the loop to see if it already exists in the variable.

I need to complete the following if statement somehow.

BashHistory=`cat ~/.bash_history`
echo "$BashHistory" | (while read ThisCommand; do
      ThisCommand=${ThisCommand// /__};
      # If this input string does not already exist in $History
      if [ ! $ThisCommand in $History ]
         then
                History="$History $ThisCommand";
      fi
 done;
echo "History - $History")

Anyway, that's what I'm working on now, so thanks again for all the help 
you people have been being for this novice bash scripter. 8-)

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