There is some code

AGENT=`/usr/sbin/tcpdump -nl -s 0 -A -r $FNAME 2>&1 | strings | grep -i "User-Agent:"` 

as a result of which I get a variable with the contents of http://pastebin.com/NHZfxQeW Next, I want to parse all of this, so that using echo output each user agent to the console line by line and do this:

  FORMATTED_AGENT=$(echo "$AGENT"|tr "User-Agent:" "\n"|sort -u) for i in $FORMATTED_AGENT; do echo $i; done 

those. I want to replace the words User-Agent: with the service symbol of the beginning of a new line, but I get the error tr: неверный порядок границ диапазона «rA» . With the help of sed it also does not work - it is still molded into one line but already in $FORMATTED_AGENT

Question : How to do it correctly if one of the conditions of the problem is non-use of temporary files (well, of the type to write line by line initially to a temporary file and cat to read it)?

The point is to line up line by line what we put in $AGENT but without using anything (such as temporary files). I just don’t know any other way than replacing the duplicate User-Agent: with the start of a line, but maybe you know?

  • What about sed ? It's clear with tr , it's not his job to change words, it works with symbols - Mike
  • @Mike yes I tried !! 1adin It does not work, he sculpts everything into a string. I don't know why !! 1 - user238861
  • And after the sed result (the whole variable) was printed that in it was Mike
  • there is a whole line in it but without the user agent and without \ n an example here ideone.com/xUz5fB - user238861
  • By the way, at the expense of tr - also with symbols. I have the variable $ HER = "1, 2, 3, 4, 5, 6," and so if you try to replace the tr 'comma with \n - the same thing happens - nothing, it just cuts out the comma. - user238861

1 answer 1

The problem is in the settings of the field delimiter when entering into a variable ( IFS ), so that the carriage transfers in the IFS variable do not have to be equal to the carriage translation, namely, it is equal to it by default. But, for in read line by line it also needs IFS separating lines to work. Therefore, we do this:

 IFS='' FORMATTED_AGENT=$(echo $AGENT | sed $"s/User-Agent: /\n/g" | sort -u) IFS=\n for i in $FORMATTED_AGENT; do echo XXX: $i; done 

If it were not for the need for sorting, I would suggest using IFS="User-Agent: "