1

I have many log files generated like the bellow:

Date;Time;Brand;User;Node;Location
01/01/01;666;blabla;George Armani;Springfield

I want to delete the String Armani but my command is not working:

cat file.log | grep identifier | cut -d ";" -f | sort | uniq | grep -o '^\S*' > newfile.log

I'm following the next post

Is the another way to make it?

PS. There are strings in the same field with different length.

jesse_b
  • 37,005
Mauricio Reyes
  • 359
  • 2
  • 5
  • 19

3 Answers3

1

To delete the text 'Armani' from the file, use this:

$ sed 's/Armani//' myfile
Date;Time;Brand;User;Node;Location
01/01/01;666;blabla;George ;Springfield
$

To edit it inplace:

$ sed -i 's/Armani//' myfile
$ cat myfile
Date;Time;Brand;User;Node;Location
01/01/01;666;blabla;George ;Springfield
$

If you want to delete all occurrences of 'Armani' within a single line, add a g, e.g. sed 's/Armani//g'

steve
  • 21,892
1

The cat is pointless so that can go. Identifier is not in your sample input so let's assume you meant blabla. Your request to delete the users last name seems completely unrelated to your example command so let's just change that

grep blabla file.log | perl -pe 's/ [^ ;]+;/;/g' > newfile.log

If that does not give you what you want you should rewrite your question so that it is clear.

user1133275
  • 5,574
1

Using awk:

awk -F\; 'OFS = ";"{ if ($4 ~ " "); split($4, fname, / /); $4=fname[1]; print }' input

This will split the 4th column by whitespace and then set it to the first element.


Should work regardless of what is in the 4th column:

$ cat input
Date;Time;Brand;User;Node;Location
01/01/01;666;blabla;George Armani;Springfield
02/02/02;777;blabla;Jesse_b;South Park
03/03/03;888;blabla;test name;somewhere

$ awk -F\; 'OFS = ";"{ if ($4 ~ " "); split($4, fname, / /); $4=fname[1]; print }' input
Date;Time;Brand;User;Node;Location
01/01/01;666;blabla;George;Springfield
02/02/02;777;blabla;Jesse_b;South Park
03/03/03;888;blabla;test;somewhere
jesse_b
  • 37,005