Finding a column in a flatfile


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding a column in a flatfile
# 1  
Old 01-07-2002
Finding a column in a flatfile

I have a file which is fixed width columns. This is an offset buffer - rather than space or tab delimited. There are upto about 8 columns and I need to get all of the column 5's values into another file.

The problem is that because the delimiter is a space - and some fields are blank - the 5th field is sometimes taken as the 6th - so I lose quite a bit of data.

I want to find a way to output just the values from the 50th - 60th positions on the line into another file - for all lines in a large text file.

Ideally I'd like to get two columns for each line.....but I imagine that should be easy enough if I can figure out how to get the column I want.

I tried " awk -F" " '{print $5 "," $1 }' " - but this gives me the problem...any ideas?Smilie
# 2  
Old 01-07-2002
Try the cut command which will allow a cut from specific positions
cut -c50-60 filename
thehoghunter
# 3  
Old 01-07-2002
Yep - that's exactly what i need - cheers.
# 4  
Old 01-07-2002
For two columns, it would be:

cut -c20-30,50-60 myfile > newfile

but reversing these:

cut -c50-60,20-30 myfile > newfile

will produce same results! man page indicates to use multiple cuts, then paste back together to get the columns rearranged. Now, how easy would it have been for the cut command to just output the data in the desired order?

So if you want column 2 followed by column 1, I would use awk:

awk '{print substr($0,50,11), substr($0,20,11)'} myfile > newfile

That comma will give you a space separator. Omit it to get the two columns concatenated.
Jimbo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding null column value using array

hi, Am trying to find a solution for finding a null column value inside a loop using array. for eg: two three five From the above array myarray,myarray and myarray having null values. But when am trying to check that space using some condition its not working. for (( i=0;... (4 Replies)
Discussion started by: rogerben
4 Replies

2. Shell Programming and Scripting

Finding Nth Column

Please help me how can I display every nth field present in a "|" delimited file. Ex: If a have a file with data as a|b|c|d|e|f|g|h|k|l|m|n I want to display every 3rd feild which means the output should be c f k n Please help me. (1 Reply)
Discussion started by: ngkumar
1 Replies

3. UNIX for Dummies Questions & Answers

finding nearest value in a column

Hi, I have 2 files: file1: 1 ia 2 1 mn 6 1 sd 11 2 ny 3 2 ma 10 3 wa 7 3 ca 8 file2 1 mi 3 1 wi 5 2 pa 4 3 id 6 (2 Replies)
Discussion started by: peanuts48
2 Replies

4. Shell Programming and Scripting

Finding Maximum value in a column

Hello, I am trying to get a script to work which will find the maximum value of the fourth column and assign that value to all rows where the first three columns match. For example: 1111 2222 AAAA 0.3 3333 4444 BBBB 0.7 1111 2222 AAAA 0.9 1111 2222 AAAA 0.5 3333 4444 BBBB 0.4 should... (8 Replies)
Discussion started by: jaysean
8 Replies

5. Shell Programming and Scripting

Finding the second last column value from a text file

Can any one tell me how to get the second last column value from the text file, which has different record size for each record. I know how to get the last column using awk and print statements, but I am unable to get the second last column value from the file. (4 Replies)
Discussion started by: naveen_sangam
4 Replies

6. Shell Programming and Scripting

Finding the last column value from a text file

Hi, I need to find out the last column value from a text file which is delimited by a tab. The issue here is the last column# for each record can be different i.,e, 1st record can have the last column as 15 and the second record can have the last column as "17". I have to search a string... (3 Replies)
Discussion started by: naveen_sangam
3 Replies

7. Shell Programming and Scripting

Help with finding a string and printing value in the next column

Hi, been about 10 years since I've scripted, so very rusty and could use some quick help. I have a file that contains data like such: folder1 jondoe owner janedoe reader joeshmo none folder2 jondoe none janedoe none joeshmo owner folder3 jondoe owner folder4 janedoe owner joeshmo... (7 Replies)
Discussion started by: drewpark
7 Replies

8. Shell Programming and Scripting

Finding a flatfile & deleting first line

I have a small script where I want to see if a file exists & then delete the first line from it. I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile: This is what I have so far just to output if the... (3 Replies)
Discussion started by: fatalxkiss
3 Replies

9. Shell Programming and Scripting

Deleting column from a flatfile with delimiter

I have a set of flatfiles which have columns delimited by #. How can a particular column be deleted in all the flatfiles. All flatfiles have same number of columns. (5 Replies)
Discussion started by: rsprabha
5 Replies

10. Shell Programming and Scripting

Finding the most common entry in a column

Hi, I have a file with 3 columns in it that are comma separated and it has about 5000 lines. What I want to do is find the most common value in column 3 using awk or a shell script or whatever works! I'm totally stuck on how to do this. e.g. value1,value2,bob value1,value2,bob... (12 Replies)
Discussion started by: Donkey25
12 Replies
Login or Register to Ask a Question