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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to extract all columns except the last few from a txt file
# 1  
Old 01-26-2013
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 not formatted like the source file. the source file is in the below link. i have removed the www since this is my first post and the file name is INP_psf1301.txt

cms.gov/Medicare/Medicare-Fee-for-Service-Payment/ProspMedicareFeeSvcPmtGen/Downloads/INP_psf1301.zip


i use the following

Code:
cut -c1, 218 INP_psf1301.txt > outputfile.txt

but the resulting file is is not formatted like the original. i want the resulting file to be exactly the same except without the last few columns. i want columns 1 to 218 in the resulting file.

is there a line break or something that i should mention or would AWK command work better?

thanks
# 2  
Old 01-26-2013
Try
Code:
cut -c 1-218 INP_psf1301.txt > outputfile.txt

OR
Code:
awk '{ print substr($0,1,218); } ' INP_psf1301.txt > outputfile.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-27-2013
Thank you. But i still get my target text file with the multiple rows from source file in one row in target file. i checked to see if the word wrap option is disabled and i doesn't make a difference.
# 4  
Old 01-27-2013
I think you have (windows?) control chars (esp. <CR> = carriage return = ^M = 0x0D) in your file, making subsequent lines overwrite earlier lines. Give it a shot after removing those... (tr -d '\r' <file)
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-27-2013
thanks Rudi. i tried your command and it removes the carriage return. i view the file in Notepad++ and there is no carriage return.

but i have a delphi utility that loads the text file and it looks for the carriage return so what i actually need is to remove columns 219 to 236 but keep the [CR][LF]
# 6  
Old 01-27-2013
Then extend bipinajith's script to look like
Code:
awk '{ print substr($0,1,218) "\r"; }' ...

This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-27-2013
thanks rudi. this works for me
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To extract values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 ... (1 Reply)
Discussion started by: jeo_fb
1 Replies

2. 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

3. Shell Programming and Scripting

Extract information from txt file

Hello! I need help :) I have a file like this: AA BC FG RF TT GH DD FF HH (a few number of rows and three columns) and I want to put the letters of each column in a variable step by step in order to give them as input in another script. So I would like to obtain: for the 1° loop:... (11 Replies)
Discussion started by: edekP
11 Replies

4. Shell Programming and Scripting

Script extract text from txt file with grep

All, I require a script that grabs some text from the gitHub API and will grep (or other function) for a string a characters that starts with (") quotes followed by two letters, may contain a pipe |, and ending with ) . What i have so far is below but it's not returning anything. ... (4 Replies)
Discussion started by: ChocoTaco
4 Replies

5. Shell Programming and Scripting

To transpose columns + edit in a txt file

Hi, I have a txt file that looks like log2FoldChange Ontology_term 8.50624450251828 GO:0003700,GO:0003707,GO:0005634,GO:0006355,GO:0043401,GO:0003700,GO:0005634,GO:0006355,GO:0008270,GO:0043565 7.03936870356684 GO:0005515,GO:0008080 6.49606183738682 6.49525073909629 GO:0005515... (4 Replies)
Discussion started by: alisrpp
4 Replies

6. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

7. Shell Programming and Scripting

extract columns from command output

I need to extract information (for example, file owner, directory path, etc). However, the code below does not work? What is wrong? find /usr/local/www/apache22/data/dev/chown_test -ls for i in * do cut -f 1,2 "$i" | echo done (3 Replies)
Discussion started by: montana24
3 Replies

8. Shell Programming and Scripting

Extract from txt file

I have data as follow in the txt file. I want to skip line starting with '#' sign. #command program abc defmt exp refmt ... ... I want to store abc exp .... in a array. I want to store defmt refmt in a array I need command to read each line in the file. I need... (6 Replies)
Discussion started by: ekb
6 Replies

9. UNIX for Dummies Questions & Answers

Adding EMPTY columns to Tab-delimited txt file

Hi I have a txt file with 4 columns where I need to add 4 empty columns in the middle meaning that I need what is currently column 4 to be column 8 in a new file. The idea is that I have to use the file as input in a program that reads the data in column 1 and 8, so the content of the other... (8 Replies)
Discussion started by: Banni
8 Replies

10. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies
Login or Register to Ask a Question