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.

  • 1
    Why did you add grep identifier to your pipeline? – Jesse_b Aug 8 '18 at 21:53
  • 1
    None of the commands in the pipeline seem to align with your stated aim of removing a string after a space - if you want to delete from the space up to the next ; delimiter that can be as simple as sed 's/ [^;]*//' file.log – steeldriver Aug 8 '18 at 21:56
  • @Jesse_b I invoke the grep "Identifier" because that lines contains the fields that I want to delete. – Mareyes Aug 8 '18 at 21:59
  • @Mareyes: I don't see the string "identifier" in your sample input at all – Jesse_b Aug 8 '18 at 22:06
  • @Jesse_b The identifier can be any field in the file. Thre's not be specific one. – Mareyes Aug 9 '18 at 0:13
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
  • This is the only answer one I needed, only I have a doubt... "fname" is the field name?? In this case "Node"? Edit: I obtain the same fields. – Mareyes Aug 9 '18 at 0:34
  • fname is just a variable to hold the split string (In this case I was thinking first name) – Jesse_b Aug 9 '18 at 0:40
  • Dude, it works. Thank you! I'll study more about awk, is powerfull tool! – Mareyes Aug 9 '18 at 0:55
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'

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.