Read columns from file by position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read columns from file by position
# 1  
Old 05-15-2010
Read columns from file by position

Hello ,
i have a fixed-length record file where each column has a specific position. how can retrive two or more column based on their positions in the file ?
Thank you
# 2  
Old 05-15-2010
can you show example of your input file and desired output?
# 3  
Old 05-15-2010
Hello ,

Input file ( each filed has a position and length )
Code:
id ( position 1 , length 5 )  ; Name ( position 6, length 20 ) ; TITLE ( position 26 , length 10 ) ; Type ( position 36 , length 20 ) ; 
Salary ( position 56 , length 5 ) ; DEPT.ID ( position 61, length 5 )

Code:
ID        NAME           TITLE        TYPE         SALARY     DEPT. ID
---       -----------    ---------    ----------   -------    --------
24        JOHN SMITH     MANAGER      FULL TIME    1000       26

Desired Output file :

Code:
JOHN SMITH   MANAGER  FULL TIME

Many thanks

Last edited by Scott; 05-15-2010 at 07:20 AM.. Reason: Please use code tags
# 4  
Old 05-15-2010
Assuming your file having the first two rows as headers ...
Try...
Code:
awk 'NR>2{print substr($0,6,20) substr($0,26,10) substr($0,36,20)}' infile

This User Gave Thanks to malcomex999 For This Post:
# 5  
Old 05-16-2010
Hello ,

How can i separate the fields in the same command with "," or one space ?

Thx
# 6  
Old 05-16-2010
Try...
Code:
awk 'NR>2{print substr($0,6,20)"," substr($0,26,10)"," substr($0,36,20)}' infile

If you need with space, change the comma to space...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete columns with a specific title XXX, where the position change in each file

Goodmorning, I know how to cut a string and a column, and how to find a word. I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title. I can't use cut -f because the position of XXX columns change in each file, and in... (14 Replies)
Discussion started by: echo manolis
14 Replies

2. Shell Programming and Scripting

Merge Two files on the basis of 2 columns at diffrent position

Hello, I am trying to merge two files with multiple records having a common columns but on first file its on 7th column and on 2nd file it on 6th column. First file(file1.txt) - 7th Column is unique H|123|Alex|Ren|W|9856746|2345789|20152202| H|97654|Ray|John||9855678|2345790|20152201|... (6 Replies)
Discussion started by: Mannu2525
6 Replies

3. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

4. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

5. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: injey
7 Replies

6. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

7. Shell Programming and Scripting

use awk to read randomly located columns in an excel file

Hi, I have an excel file that have a random count of columns/fields and what im trying to do is to only retrieve all the rows under 2 specific field headers. I can use the usually command for awk which is awk 'print{ $1 $2}' > output.txt, but the location of the 2 specific field headers is... (9 Replies)
Discussion started by: mdap
9 Replies

8. Shell Programming and Scripting

read file until certain line position

let's say I have this file format and another file i only want to get the lines between "test" and "*", so how do I do that and then assign to a variable with a space in between? (5 Replies)
Discussion started by: finalight
5 Replies

9. Shell Programming and Scripting

Read value from particular position in file.

From unix script i want to read string value in a file from position 2 to 5. which command can be used or any alternatives there?? (2 Replies)
Discussion started by: krishnarao
2 Replies

10. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies
Login or Register to Ask a Question