help:Reading width separated data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help:Reading width separated data
# 1  
Old 01-08-2009
help:Reading width separated data

Hi,

I'm quite new to unix script, and is having some problems currently.
Say for the following sample project:

The sql result is stored in a file >sql.txt.
The result is shown (sample table):

Code:
ID      Price    Tag    Name
----- ------- -----  --------
A      100      test1  test1
B      200      test2  test2

....and so on

1. Go through every line of the sql result, starting from ID A all the way to the end (row 3 onwards)
2. The result is separated by variable space , ie, width ="5, 4, 5, 7".
3. check value of the Price, Tag of each row, and do processing with it.

I've no gawk, so I cannot use the FIELDWIDTH to set it.
How can I do it with awk/nawk or any other methods?

thanks! appreciate any help and tips

Last edited by Franklin52; 01-08-2009 at 04:51 AM.. Reason: adding code tags
# 2  
Old 01-08-2009
awk refers $1 to the 1st field, $2 to the 2nd field etc. even with multiple spaces, so what is the point?

Regards
# 3  
Old 01-08-2009
Hi,

As in how can I separate the row data into its respective field using the column spacing. There is no clear separator like | or ,. Thus, I cannot use commands like FS="|" to clearly get the column values.

thanks
# 4  
Old 01-08-2009
As I mentioned before, the fields are referred to $1, $2, $3 and $4, in your scenario:

Code:
awk '{print $1, $2, $3, $4}' file

should print the fields of your file, there's no need to use another fieldseparator.
If you want a formatted output you can use the printf command in awk.

Regards
# 5  
Old 01-08-2009
Hi ,

the table i've given above is just a sample..sorry if i'm not clear
As some columns are text field, it is possible that it will contain " " or "," etc, and will not be separated properly.
Code:
ID      Price    Tag    Name
----- ------- -----  --------
A      100      te st1  te, st1
B      200      tes,t2  tes t2

using FS=" " on above will yield [A 100 te st1 te, st1]

regards
# 6  
Old 01-08-2009
You can use the substr function, if the width of the fields are 7, 9, 7 and 7 you get those fields as follow:

To print the fields you can do something like::

Code:
awk 'NR < 3 {next} {
field1=substr($0,1,7)
field2=substr($0,8,9)
field3=substr($0,17,7)
field4=substr($0,24,7)
print field1
print field2
print field3
print field4
}' file

Regards
# 7  
Old 01-08-2009
Hi,

Thanks for your help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

AIX put comma separated data on its own line

In Linux you can do this to put comma separated data on its own line like this. sed 's/ */&\n/g' /tmp/ports sed 's/ */\n/g' /tmp/ports How do you do this in AIX? It is not working. Is there another way to do this? Something like this. 1, 2, 3, 4 To look like this. 1 2 3 4 (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

3. Post Here to Contact Site Administrators and Moderators

How to sum up data in fixed width file with decimal point?

HI Everyone, I have below source file AAA|NAME1|ADDRESS1|300.20 BBB|NAME2|ADDRESS2|400.31 CCC|NAME3|ADDRESS3|300.34 I have requirement where I need to sum up fourth field in above fixed width pipe delimited flat file. When I use below code, it gives me value 1001.00 But I am expecting... (1 Reply)
Discussion started by: patricjemmy6
1 Replies

4. Shell Programming and Scripting

Reading Words separated by comma in line

Hi All, I am facing issue, to read words in line, line as follow and i want to read word at each comma 1,you,are,two So i want read like 1 you are two Thanks (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

5. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

6. Shell Programming and Scripting

Ignore Header and Footer and Sort the data in fixed width file

Hi Experts, I want to Sort the data in fixed width file where i have Header and Footer also in file. I m using below commad to do the sort based on field satarting from 15 position to 17 position , but it is not ignoring the Header and Footer of the file while sorting. In the output i am... (5 Replies)
Discussion started by: sasikari
5 Replies

7. Shell Programming and Scripting

reading comma separated data and reorder

hey guys! i need to read data from a file that are comma separated then reorder them in another file to be generated for example: x,y,z a,b,c l,m,n o,p,q and transform this into: x,a,l,o y,b,m,p z,c,n,q Will appreciate your fast reply Regards! (5 Replies)
Discussion started by: maiooi90
5 Replies

8. Shell Programming and Scripting

Reading comma separated variable into other variables in shell script

Hi, In shell script, I have a variable var = xyz, inn, day, night, calif ....n and I would like to read them in to var1 = xzy, var2 = inn, var3= day, var4 = night....var. probably in a loop. I would like to read the variables until end of the line. Comma is the delimiter and there's no comma at... (3 Replies)
Discussion started by: suryaemlinux
3 Replies

9. Programming

reading reading data from webpage

hi iam reading data from web page using request socket and curl socket. now my problem is some the web page containg data as a image so how can i read the data from a image. thank,inadvance. sree (3 Replies)
Discussion started by: phani_sree
3 Replies

10. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question