how to extract columns from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to extract columns from a text file
# 1  
Old 05-04-2008
how to extract columns from a text file

Hi,
In ksh, I have a file with similar rows as follows:
Department = 1234 G/L Asset Acct No = 12.0000. 2/29/2008
Department = 1234 G/L Asset Acct No = 13.0000. 3/29/2008.

I want to create a new text file that contains only the numbers and date:
1234 12.0000. 2/29/2008
1234 13.0000. 3/29/2008

Should I use "cut" to cut out what I need?
Also, how do I make sure blank lines in original file do not get copied over to the new file?
Thanks in advance.
# 2  
Old 05-04-2008
hi,
try this,

awk '{print $9$10}' test > new_file

where the content of test is ,
Department = 1234 G/L Asset Acct No = 12.0000. 2/29/2008
Department = 1234 G/L Asset Acct No = 13.0000. 3/29/2008
# 3  
Old 05-04-2008
you forget $3
# 4  
Old 05-04-2008
Hammer & Screwdriver

with spaces we can use actual like that.

awk '{print $3 " " $9 " " $10}' afile

1234 12.0000. 2/29/2008
1234 13.0000. 3/29/2008

Regards,
Bash
# 5  
Old 05-04-2008
Hammer & Screwdriver

This can also help.

cut -f 3,9,10 -d ' ' afile

1234 12.0000. 2/29/2008
1234 13.0000. 3/29/2008

Regards,
Bash
# 6  
Old 05-04-2008
you guys are the greatest.

Thanks a million gentlement...You guys are the greatest. You just helped out a deptartment in high tech company in Silicon Valley.
# 7  
Old 05-04-2008
oops - how to use "cut" for multi columns

Oops, as it turns out I have access only to "cut" command in UnixDos. My text file actually has a lot of spaces, so the rows look like these:
Department = 1234 __________G/L Asset Acct No = 12.0000._____2/29/2008
Department = 1234___________G/L Asset Acct No = 13.0000._____3/29/2008
I had to use underscore instead of spaces above because this website removes extra spaces from the rows above.


I tried to use cut -c14-18 -c37-39 textfile but it would not cut multi columns.
My final output file should look like these:
1234 12.0000. 2/29/2008
1234 13.0000. 3/29/2008

Is there a way to cut multiple columns? UnixDos does not have "awk".
Thanks again.

Last edited by ihot; 05-05-2008 at 12:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Columns in one file and extract columns from another file

Kindly help merging information from two files with the following data structure. I want to match for the CHR-SNP in Foo and get the columns that match from CHROM-rsID Fields 1 & 2 of foo may have duplicates, however, a joint key of Fields $1$2$3$4 is unique. Also would be helpful to clean up... (4 Replies)
Discussion started by: genehunter
4 Replies

2. Shell Programming and Scripting

Extract columns into seperate file

I have a comma delimited file as per the one below and I am currently extracting the values in 2 columns (COL1 & COL6) to produce a smaller trimmed down version of the file which only contains the columns we need; COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9... (1 Reply)
Discussion started by: Ads89
1 Replies

3. UNIX for Dummies Questions & Answers

Extract spread columns from large file

Dear all, I want to extract around 300 columns from a very large file with almost 2million columns. There are no headers, but I can find out which column numbers I want. I know I can extract with the function 'cut -f2' for example just the second column but how do I do this for such a large... (1 Reply)
Discussion started by: fndijk
1 Replies

4. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies

5. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

6. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

7. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

8. Shell Programming and Scripting

Extract Columns from file

Hi All, Could you please help me with following: I have to parse a .csv file. For example: If the csv file contains 3 columns, then i have to print the column names. The field separator is (comma). example.csv (contains 2 lines as follows) This is,a test file, for validation... (2 Replies)
Discussion started by: vfrg
2 Replies

9. Shell Programming and Scripting

Help, need to extract columns from file

I have huge fixed width, text file in unix box and I need to extract columns found between the width 105 and 200 and output it to a new file. Can anyone tell me how to extract it? Thanks for your help. (1 Reply)
Discussion started by: kiran2k
1 Replies

10. Shell Programming and Scripting

Extract text in 2 columns of output file.

Hello!! I have the text file with following format... --SubTotal-----------------------------------0--161---------------44---13739369-67--- --SubTotal-----------------------------------0--147---------------0----49643---1----... (2 Replies)
Discussion started by: Danish Shakil
2 Replies
Login or Register to Ask a Question