Parsing a fixed column text file in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a fixed column text file in sed
# 1  
Old 06-23-2015
Parsing a fixed column text file in sed

I have a text file with records of the form:
Code:
A X1 Y1 X2 Y2 X3 Y3

where A is character length 10, Xi is character length 4 and Yi is numeric length 10.

I want to parse the line, and output records like:
Code:
A X1 Y1
A X2 Y2
A X3 Y3
etc

Can anyone please give me an idea of how to do this. Thanks.

Last edited by Scrutinizer; 06-24-2015 at 12:19 AM.. Reason: code tags
# 2  
Old 06-24-2015
Quote:
Originally Posted by wvdeijk
[..]
Can anyone please give me an idea of how to do this. Thanks.
Is your input space delimited? You can loop over the fields starting on field 2 with increments of 2 and print the first field and the current field plus its right hand neighbor...

If it is fixed width you can call substring functions to specify the fields...

Last edited by Scrutinizer; 06-25-2015 at 07:59 PM.. Reason: with -> width
# 3  
Old 06-25-2015
please check this ,
Code:
$cat a
A X1 Y1 X2 Y2 X3 Y3
$awk '{ i=1 ; while (i< NF) {printf("%s ",$1);i++;printf("%s ",$i);i++;printf("%s\n",$i)} }' a
A X1 Y1
A X2 Y2
A X3 Y3

# 4  
Old 06-25-2015
Quote:
Originally Posted by wvdeijk
I have a text file with records of the form:
Code:
A X1 Y1 X2 Y2 X3 Y3

where A is character length 10, Xi is character length 4 and Yi is numeric length 10.

I want to parse the line, and output records like:
Code:
A X1 Y1
A X2 Y2
A X3 Y3
etc

Can anyone please give me an idea of how to do this. Thanks.
I find it very hard to reconcile your statements (A is character length 10) with ("A" which is length 1); (Xi is character length 4) with ("X1", "X2", and "X3" which are all length 2); and (Yi is numeric length 10) with ("Y1", "Y2", and "Y3" which are all length 2 and none of them are numeric).

Please show us a sample input file that contains real data (or fake data that matches the format of your real input file(s)) and the actual output that you are trying to produce from that sample input file. And, use CODE tags for both the sample input and the desired output.
# 5  
Old 06-25-2015
Hope I understand correctly. From the
input:
Code:
0123456789abcd1234567890ABCD1234567890xyz01234567890ABCD1234567890

Code:
sed -n '
:L
h
s/\(.\{10\}\)\(.\{4\}\)\(.\{10\}\).*/\1 \2 \3/p
g
s/\(.\{10\}\)\(.\{4\}\)\(.\{10\}\)/\1/
tL
' input

produces the output:
Code:
0123456789 abcd 1234567890
0123456789 ABCD 1234567890
0123456789 xyz0 1234567890
0123456789 ABCD 1234567890

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parsing a column of text file - best practices

HI Folks - I hope all is well! I have a business process where I manage a text file of substitution variables and their values for a target system. After updating, I run a script to push the changes to the target system. However, I'm trying to develop a method to be able to automatically... (50 Replies)
Discussion started by: SIMMS7400
50 Replies

2. Shell Programming and Scripting

To replace the value of the column in a fixed width file

I have a fixed with file with header & trailer length having the same length of the detail record file. The details record length of this file is 24, for Header and Trailer the records will be padded with spaces to match the record length of the file Currently I am adding 3 spaces in header... (14 Replies)
Discussion started by: ginrkf
14 Replies

3. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

How to split a fixed width text file into several ones based on a column value?

Hi, I have a fixed width text file without any header row. One of the columns contains a date in YYYYMMDD format. If the original file contains 3 dates, I want my shell script to split the file into 3 small files with data for each date. I am a newbie and need help doing this. (14 Replies)
Discussion started by: bhanja_trinanja
14 Replies

5. Shell Programming and Scripting

Text file -> fixed column -> MySQL DB

Hi ! I have searched in the forum, but i'm not able to combine various solution to help me ! My problem is very simple for a bash scripter or a old linux man, but not for me (novice and inexperienced). I need to store in a mysql database the output of "last" command, but the table where i need... (2 Replies)
Discussion started by: axl936
2 Replies

6. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

7. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

8. Shell Programming and Scripting

print a file with one column having fixed character length

Hi guys, I have tried to find a solution for this problem but couln't. If anyone of you have an Idea do help me. INPUT_FILE with three columns shown to be separated by - sign A5BNK723NVI - 1 - 294 A7QZM0VIT - 251 - 537 A7NU3411V - 245 - 527 I want an output file in which First column... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

9. UNIX Desktop Questions & Answers

Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated. I... (4 Replies)
Discussion started by: sate911
4 Replies

10. Shell Programming and Scripting

(sed) parsing insert statement column that crosses multiple lines

I have a file with a set of insert statements some of which have a single column value that crosses multiple lines causing the statement to fail in sql*plue. Can someone help me with a sed script to replace the new lines with chr(10)? here is an example: insert into mytable(id, field1, field2)... (3 Replies)
Discussion started by: jjordan
3 Replies
Login or Register to Ask a Question