Seperated a Column from 'ESC' Character seperated file

Hi Experts

I have an escape seperated fields in the unix file. And in the below format file I need to extract the first column. Please help its urgent.

cat -v op.dat | head

24397028^[ABCD1234^[ABCD1234^[ ^[May 24 2011 12:10PM^[^[^[CSGBU
2439707^[ABCD1234^[ABCD1234^[ ^[Oct 25 2011  9:42AM^[^[^[CSGBU

I want to extract the file in below format ( with only first column )

24397028
2439707

thanks.

Welcome to the forum.

Any attempts / ideas / thoughts from your side?

Columns separated by Escape characters! And I thought I have already seen all the weirdnesses of the world!

Extracting one column can be done by the cut command. See man cut. You can specify the column separator with this command.

There are other possibilities too (grep for instance).

How to represent an escape character, depends on the shell you are using. For instance, escape can be represented in bash or zsh by

$'\e'

Try this -

awk -F "^" '{print $1}' op.dat

OSX 10.13.5, default bash terminal.

Last login: Thu Jul 19 13:26:35 on ttys000
AMIGA:amiga~> echo '24397028^[ABCD1234^[ABCD1234^[ ^[May 24 2011 12:10PM^[^[^[CSGBU
> 2439707^[ABCD1234^[ABCD1234^[ ^[Oct 25 2011  9:42AM^[^[^[CSGBU' > /tmp/escape
AMIGA:amiga~> hexdump -C /tmp/escape
00000000  32 34 33 39 37 30 32 38  5e 5b 41 42 43 44 31 32  |24397028^[ABCD12|
00000010  33 34 5e 5b 41 42 43 44  31 32 33 34 5e 5b 20 5e  |34^[ABCD1234^[ ^|
00000020  5b 4d 61 79 20 32 34 20  32 30 31 31 20 31 32 3a  |[May 24 2011 12:|
00000030  31 30 50 4d 5e 5b 5e 5b  5e 5b 43 53 47 42 55 0a  |10PM^[^[^[CSGBU.|
00000040  32 34 33 39 37 30 37 5e  5b 41 42 43 44 31 32 33  |2439707^[ABCD123|
00000050  34 5e 5b 41 42 43 44 31  32 33 34 5e 5b 20 5e 5b  |4^[ABCD1234^[ ^[|
00000060  4f 63 74 20 32 35 20 32  30 31 31 20 20 39 3a 34  |Oct 25 2011  9:4|
00000070  32 41 4d 5e 5b 5e 5b 5e  5b 43 53 47 42 55 0a     |2AM^[^[^[CSGBU.|
0000007f
AMIGA:amiga~> _

There is no escape character, there are however a carat with a left hand square bracket representing an escape - so is it a real escape character [0x]1B you need to be detected or is the carat good enough?

The ^[ is (not only) cat -v 's way to represent the ESC - char. Caret won't be good enough, won't work.
Let's wait for the requestor's attempts before we post valid solutions.

Hi neha_suri06,
If you want quick replies to your queries, and have those replies have a good chance of woking in your environment, it is crucial that you always tell us what operating system you're using, what shell you're using, and show us what you have tried.

Showing us the output from cat -v file gives us ambiguous data about what the actual contents are of the file named by file might be. If your sample input file has fields delimited by ASCII escape characters, we know that it contains at least two lines each containing somewhere between one and seven fields. To help us test code that might work to solve your problem, it is always better to include a copy of the data you want to process (in CODE tags) or upload a copy of the file itself. If there are questions about the data contained in a file, showing us the output from hexdump -C file (if it is available on your system) or od -bc file (which should be available on any UNIX or UNIX-like system) is always better than cat -v file because they both produce unambiguous output.

Refusing to answer questions about what you have tried and what you have considered trying makes it very clear that, despite what you said in your first post, this problem must not really be urgent for you.

Hi chandan.chaman and wisecracker,
As RudiC has already said, if you look closely at post #1 in this thread, you will see that the sample input show in that post is not the actual contents of the file named op.dat ; it is the contents of op.dat after processing by cat -v . From that output, we don't know if there are any circumflex characters in op.dat , but we do know that if there are any, they are data rather than being field delimiters.

If we knew that there are no circumflex characters in the first field in op.dat (which is not stated anywhere in neha_suri06's requirements but might be assumed from the given example), one could try the grossly inefficient:

cat -v op.dat | awk -F'^' '{print $1}'

Much better solutions could be suggested if we knew what operating system and shell neha_suri06 is using and had a better understand of what neha_suri06 had already tried to solve this problem.

Hoping this helps :confused: ,
Don