Read columns from delimited file in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read columns from delimited file in UNIX
# 1  
Old 12-20-2010
Tools Read columns from delimited file in UNIX

Hello
I need to read the columns from a flat file delimited by Hex code X02.

The Sample file is
Red^B1000^BJohn
Blue^B2000^BSam
Green^B3000^BDan

Note: Hex code X02 shows as ^B in vi.

I need to read the file and process the columns in each row.

I tried using awk -F command but unable to provide hex or oct codes as delimiter. Please advice.

Thanks in advance.
# 2  
Old 12-20-2010
Code:
awk 'BEGIN {FS=sprintf("%c",0x02)} {print $2}' sample

# 3  
Old 12-21-2010
Or:

Code:
awk -F^B '{print $2}' infile

Where ^B is entered as CTRL-V CTRL-B

In bash/ksh93 you can do this:
Code:
awk -F$'\002' '{print $2}' infile

Note: \002 is octal
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-22-2010
Thanks. I am able to do it using Scrutinizer's first trick.
Code:
 
awk -F^B '{print $2}' infile

The second method using \002 did not work though.

Chuber_XL - Thanks. sprintf seems a brilliant idea. But I used Scrutinizer's more simpler trick.
# 5  
Old 12-22-2010
Your welcome, as a general rule I like to avoid having non-printable characters in my scripts (they don't print properly, copy/paste dosn't work with them and they don't display properly in more/pg).

Seeing \002 or 0x02 in a script makes it much clearer what the original author intended, rather than a blue ^B appearing in the editor, that looks (to me) like a corrupted file.
# 6  
Old 12-22-2010
Chubler - I just tried the sprintf idea. It is not working too. somehow it is not recognizing \002 or 0x02 representation.

I even tried to print 0x02 into a file using below. the file does not show the special character or ^B inside. Any idea what might be wrong?

Code:
 
awk -f"^B" '{print sprintf("%c",0x02)}' HAWQ.dat > newout.txt

# 7  
Old 12-22-2010
My bad, looks like some versions of awk don't support the 0x02 format, try 2 instead:

Code:
$ awk 'BEGIN {FS=sprintf("%c", 2)} {print $2}' sample

By the way od -c is nice to debug this sort of stuff:

on AIX 5.3
Code:
$ awk 'BEGIN { print sprintf("%c", 2)}' | od -c
0000000  002  \n
0000002
$ awk 'BEGIN { print sprintf("%c", 0x02)}' | od -c
0000000    0  \n
0000002

On cygwin:
Code:
$ awk 'BEGIN { print sprintf("%c", 2)}' | od -c
0000000  002  \n
0000002
$ awk 'BEGIN { print sprintf("%c", 0x02)}' | od -c
0000000    002  \n
0000002

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modify comma delimited file columns.

Please help me to update a file which contains date values as below:- From:- "1912108",20161130,"2016-12-01-00.00.00.000000","2016-12-01-08.37.12.000000" "1912108",20161201,"2016-12-02-00.00.00.000000","2016-12-02-08.28.22.000000" To:- "1912108",2016-11-30,"2016-12-01... (7 Replies)
Discussion started by: KrishnaVM
7 Replies

2. Shell Programming and Scripting

Read delimited file

I have a delimited file (,) containing Name, Amount,Type,Address,zip,Tel and Extn. If any of this column information is missing (except TYPE and Extn), I need to print that a spefic column value is missing in my output. Example: row 2 is missing ZIP and the out put should contain NOZIP in... (1 Reply)
Discussion started by: meet_calramz
1 Replies

3. Shell Programming and Scripting

Delete and insert columns in a tab delimited file

Hi all , I have a file having 12 columns tab delimited . I need to read this file and remove the column 3 and column 4 and insert a word in column 3 as "AVIALABLE " Is there a way to do this . I am trying like below Thanks DJ cat $FILENAME|awk -F"\t" '{ print $1 "\t... (3 Replies)
Discussion started by: Hypesslearner
3 Replies

4. Shell Programming and Scripting

Remove few columns from pipe delimited file

I have file as below column1|column2|column3|column4|column5| fill1|fill2|fill3|fill4|fill5| abc1|abc2|abc3|abc4|abc5| . . . . i need to remove column2,3, from that file column1|column4|column5| fill1|fill4|fill5| abc1|abc4|abc5| . . . (3 Replies)
Discussion started by: greenworld123
3 Replies

5. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

6. Shell Programming and Scripting

Awk print all columns in delimited file

text file example 1,222222222222,333,444444444444444 111111111,22,33333333,444 desired output 1 222222222222 333 444444444444444 111111111 22 33333333 444I have a delimeted file which I want to print in a table like format. The... (10 Replies)
Discussion started by: Calypso
10 Replies

7. UNIX for Dummies Questions & Answers

Deleting columns from a space delimited text file

I have a space delimited text file with 1,000,000+ columns and 100 rows. I want to delete columns 2 through 5 (2 and 5) included from the text file. How do I do that? Thanks. (3 Replies)
Discussion started by: evelibertine
3 Replies

8. UNIX for Dummies Questions & Answers

nth Columns in a Tab delimited file

Hi Can anyone help me to identify the nth field from a Tab delimited file? Thanks Subrat (8 Replies)
Discussion started by: subrat
8 Replies

9. Shell Programming and Scripting

Reading columns in tab delimited file

I want to read only one column in "|" delimited file and write that column to a new file. For Ex: Input File 1|abc|324|tt 2|efd|11|cbcb 3||1|fg 4|ert|23|88 Output : I want to read column 3 in diff file. 324 11 1 88 Can anyone give me inputs on this ? (2 Replies)
Discussion started by: net
2 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question