Wednesday, July 23, 2008

Bash for-loop should ignore space-characters

If you wanna to someting like this:

 

for I in $(zgrep -H “from=<test@google.com>” /var/logs/*.gz 2>/dev/null)
do
  LOG_FILE=”`echo $I | cut -d ‘:’ -f 1`”
  DATETIME=”`echo $I | cut -d ‘:’ -f 2-4 | cut -d ‘ ‘ -f 1-3`”
  MAILID=”`echo $I | cut -d ‘:’ -f 5 | cut -d ‘ ‘ -f 2`”
  RECIPIENT_ADDR=”`zgrep $MAILID $LOG_FILE | grep ‘to=’ | cut -d ‘<’ -f 2 | cut -d ‘>’ -f 1`”
  echo “$DATETIME $RECIPIENT_ADDR”
done

You will notice sooner or later that this doesn’t work this way: The for-loop doesn’t only treat line-breaks as a new element but also space-characters. Well, how can that be solved, fast and easy. This way:

 

zgrep -H “from=<test@google.com>” /var/logs/*.gz 2>/dev/null | while read I
do
  LOG_FILE=”`echo $I | cut -d ‘:’ -f 1`”
  DATETIME=”`echo $I | cut -d ‘:’ -f 2-4 | cut -d ‘ ‘ -f 1-3`”
  MAILID=”`echo $I | cut -d ‘:’ -f 5 | cut -d ‘ ‘ -f 2`”
  RECIPIENT_ADDR=”`zgrep $MAILID $LOG_FILE | grep ‘to=’ | cut -d ‘<’ -f 2 | cut -d ‘>’ -f 1`”
  echo “$DATETIME $RECIPIENT_ADDR”
done

Posted by schmidi2 at 16:31:59 | Permalink | No Comments »

Tuesday, January 15, 2008

cat a file line by line doesn’t work

When you wan’t to compute a file line by line in Bash (or other shell’s ?) with eg. with this command:

for L in $(cat lines.txt); do
   grep “$L” second-file.txt;
done

or

cat lines.txt | while read L; do
   grep “$L” second-file.txt
done

it does ONLY work if you use the UNIX line separator (\n). If you access a file, which was created on a windows system, such commands will not return the expected result. You have to convert the input file first. Do that with the little tool called “dos2unix”.

Posted by schmidi2 at 16:23:44 | Permalink | No Comments »