2

I have an input file in the following form:

something here
somethingElseHere^[%-somestuff here
^[%-somestuff here

Note that ^[ is the escape character \x1b.

So what I'm trying to do is delete everything on the file after the first ^[ occurrence, in a bash script, so I should end up with something like this:

something here
somethingElseHere

My solution was: awk -F "\x1b" {'print $1'} but this is giving this as an output:

something here
somethingElseHere
(empty line here)

So it's adding an extra empty line after the last one. Still if I delete it manually I get an extra byte. I made a C++ program which reads the file until the \x1b character and writes all the read characters in a separate file but with AWK, after deleting the extra line, I still get 1 byte more than doing it with C++.

Edit:

maybe it's the EOL character that gets added when using AWK? I don't add it when using the C++ program.

UPDATE:

I've just tried some of the commands, and most of them work if I open them in vi, but still they have an extra byte and I can't figure out where it comes from.

$hexdump -x file1
0000000    4329    706f    7279    6769    7468    6328    2029    3931
0000010    3939    4d20    6369    6f72    6f73    7466    4320    726f
0000020    6f70    6172    6974    6e6f    610a    6362    6564    6766
0000030    6968    6b6a    6d6c    6f6e    7170    7372    7574    7776
0000040    7978    534d    5020    4c43    4c58    6f46    746e    3020
0000050    3130    a8f8    4955    0a42                                
0000058

$hexdump -x file2
0000000    4329    706f    7279    6769    7468    6328    2029    3931
0000010    3939    4d20    6369    6f72    6f73    7466    4320    726f
0000020    6f70    6172    6974    6e6f    610a    6362    6564    6766
0000030    6968    6b6a    6d6c    6f6e    7170    7372    7574    7776
0000040    7978    534d    5020    4c43    4c58    6f46    746e    3020
0000050    3130    a8f8    4955    0042                                
0000057

In file1, which is the one I'm generating with bash, it adds an extra 0x0a (new line character).

  • awk -F "\x1b" '{print $1} NF > 1 {quit}'? – muru Jan 5 '17 at 8:59
  • I updated the question to explain it better. With the NF > 1 {quit} it didn't work either. – deuseux12 Jan 5 '17 at 9:03
  • you can use printf "%s", $1 to print without a new line. – Rabin Jan 5 '17 at 9:06
  • 1
    @muru I believe this is exit, not quit – xhienne Jan 5 '17 at 9:06
  • awk -F "\x1b" '$1{print $1}' input.txt – Kamaraj Jan 5 '17 at 9:08
3

Here is a solution with GNU sed:

sed -z 's/\x1b.*//'

And below is a solution with awk, based on the OP's comments:

awk -F '\x1b' 'NF > 1 { printf "%s", $1; exit } 1'

You must have a good reason to not want the ending newline but bear in mind that a regular text file must be new-line terminated.

  • This solution works, thanks. Needed to avoid the extra line to calculate a hash. – deuseux12 Jan 5 '17 at 10:10
  • @fedorqui I don't get it, ^[ can occur anywhere. What would break? Can you give a (working) example of a file that would break with any of those commands? – xhienne Jan 5 '17 at 10:18
  • @fedorqui That's exactly what the OP wants. awk must stop at the first \x1b (or ^[) delimiter. I assume you are referring to the first OP's sample input which has three lines. – xhienne Jan 5 '17 at 10:35

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.