Insert string in first column

Asked

Viewed 522 times

2

I am breaking my head with something very simple, I need to insert a ";" (point and comma) after a Sort/Uniq in a file. As it returns the number of repeated lines, I need this return that is always in the first column to be inserted this ";". I already searched with "CUT, AWK, SED" and I did not get a satisfactory result.

Example:

cat /tmp/filtrado2.txt|  sort| uniq -c| sort -nr

Thank you for helping me.

  • What would this Sort/Uniq, a string ?

  • Sort/Uniq is the command I set. I did a Sort and then Uniq, as the example I mentioned above. After the result by them, which is generated in the first column, wanted to insert the ";" in it.

  • You can post the contents of your file ?

  • Yes, before "Mar" has the value 1 which is the return of Sort + Uniq, and the string ";" should come right after this value. Remember, the default is only the column, because this value 1 does not always repeat, can be 1000, 45678, 2, and etc.

  • 1 Mar 3 00:01; 113.78.57.53; Unknown; <alonso4t@xxxx>; BS TOYS AND SAMBA TOYS - 3D FILES

  • A desired input and output file helps. It can probably be done without using anything else that awk.

Show 1 more comment

2 answers

1


Just use a regular expression in sed to select any number: [0-9]+

Also, use the operator & retrieving the text that married the search expression.

Would look like this:

cat /tmp/filtrado2.txt | sort | uniq -c | sort -nr | sed -r 's/[0-9]+/&;/'
  • Good. It worked. A doubt, it takes any number and inserts the ";". As he knows that it is only the numbers in the first column as it has numbers in other places of the text and it does not insert this ";".

  • What will determine this is the regular expression used in sed. &" guards the text that matches the expression. So, what the above command will do is replace the text that married the expression with the text itself, plus the character set that you put next to the '&'.

0

Try it like this:

cat /tmp/filtrado2.txt | sort | uniq -c | sort -nr | sed 's/1/1;/'
  • He added ";" at the end of the line and not in the column containing the value 1

  • You want me to replace 1 with ; or insert ; where does 1 contain? I may have been confused.

  • I would like to add the ";" after the 1, leaving : "1;"

  • I edited the command, test again.

  • Hello Douglas, actually, in this way I had already succeeded, the problem is that there is an exact pattern. The value 1 was just an example, but this first column can be from the value 1 to 1 million, so it needed something to default to the first column and not its value.

  • 1

    Really, I’ll think of something better.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.