Seperated a Column from 'ESC' Character seperated file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Seperated a Column from 'ESC' Character seperated file
# 1  
Old 07-19-2018
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.

Code:
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 )

Code:
24397028
2439707

thanks.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 07-19-2018 at 05:27 AM.. Reason: Added CODE tags.
# 2  
Old 07-19-2018
Welcome to the forum.

Any attempts / ideas / thoughts from your side?
# 3  
Old 07-19-2018
Quote:
Originally Posted by neha_suri06
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.
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
Code:
$'\e'

# 4  
Old 07-19-2018
Try this -

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

# 5  
Old 07-19-2018
Quote:
Originally Posted by neha_suri06
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.

Code:
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 )

Code:
24397028
2439707

OSX 10.13.5, default bash terminal.

Code:
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?
# 6  
Old 07-19-2018
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.
# 7  
Old 07-20-2018
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:
Code:
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 Smilie ,
Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in rearranging the content of a file comma seperated

I have a file with the below content a = test1 b = test2 a = test3 b= test4 c = test6 b = test5 d = test7 d = test9 Need the output to be as follows a = test1,test3 b = test2, test5 c = test6 d = test7, test9 (4 Replies)
Discussion started by: iron_michael86
4 Replies

2. Shell Programming and Scripting

SUM semicolon-seperated values from txt-file

Hello, (I'm a shell beginner) how can I sum the bold values of the following txt-file (values.txt) with bash shell. The result of the sum should be written in a new txt file (sum.txt): ... Thanks in advance (5 Replies)
Discussion started by: milu
5 Replies

3. Shell Programming and Scripting

Seperated by columns, merge in a file, sort them on common column

Hi All, I have 4 files in below format. I took them as an example. File 1: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting character H to the initial of all line like HCTOT. CTOT 456787897 Low fever CTOR 556712345 High fever... (2 Replies)
Discussion started by: Mannu2525
2 Replies

4. UNIX for Dummies Questions & Answers

Need help removing leading spaces from one field in comma seperated file

Using awk or sed, I'd like to remove leading spaces after a comma and before a right justified number in field 6. Sounds simple but I can't find a solution. Each field's formatting must stay intact. Input: 40,123456-02,160,05/24/2012,02/13/1977, 10699.15,0 Output:... (5 Replies)
Discussion started by: Scottie1954
5 Replies

5. UNIX for Advanced & Expert Users

grep 7th column from a file seperated with ^

Hi, Can anyone help: I want to get the 7th column of a file seperated by ^ Num:^ 1^ testdevice1^10.1.1.1^PT1X23^true^HD^175^up^false^bad Num:^ 2^ testdevice2^10.1.1.2^ST1X23^true^SN^175^up^false^bad Expected result: HD SN Appreciate your help. (2 Replies)
Discussion started by: sureshcisco
2 Replies

6. Shell Programming and Scripting

Convert comma seperated file to line seperated.

Hi, I have data like this. 1,2,3,4 Output required: 1 2 3 4 I am trying to use tr function but getting error. Help is appreciated. (6 Replies)
Discussion started by: pinnacle
6 Replies

7. Shell Programming and Scripting

Trimming fields for comma or pipe seperated file

I have file like this FileA: abc , "helloworld" , america def,asia, japan ghi, africa, ipl Output Needed: abc,"helloworld",america def,asia,japan ghi,africa,ipl I would like to implement using awk. I want to trim each field for its leading and trailing spaces. (7 Replies)
Discussion started by: pinnacle
7 Replies

8. Shell Programming and Scripting

Removing blank lines from comma seperated and space seperated file.

Hi, I want to remove empty/blank lines from comma seperated and space seperated files Thanks all for help (11 Replies)
Discussion started by: pinnacle
11 Replies

9. UNIX for Dummies Questions & Answers

Problem with tab seperated file in UNIX

Hi All, I am facing a problem while getting the data from an Oracle table Problem: I am getting rows from an Oracle table to a tab seperated file. One of the columns in that table have value with in between spaces like AAAAA AAA It's loading fine to .tab file. But when... (2 Replies)
Discussion started by: pssandeep
2 Replies

10. UNIX for Dummies Questions & Answers

Search and then concat 4m other file (comma seperated)

My query is now a bit simplified. file1.txt names; ID; value1 ; values N; ABC; 1 ; a18 ; ... CDF; 2 ; b16 ; .. ABC; 1 ; c13 ; ...... EFG; 3 ;d12 ; ... file2.txt ID(Unique);smVals; smVal1; smVal N; 1; ...; ...; ...; 2; ..; ..; ..; 3; ..; ..; ..; ... (1 Reply)
Discussion started by: szchmaltz
1 Replies
Login or Register to Ask a Question