Portion of a file in a new files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Portion of a file in a new files
# 1  
Old 05-07-2009
Portion of a file in a new files

Hi,

I need to devide one file into 3 files based on column numbers and put a string (FILE1, FILE2, FILE3) in the last.....

Input file:
Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10

Output1:
Column1,Column3,Column6,Column4,Column5,FILE1

Output2:
Column3,Column8,Column9,Column1,Column2,FILE2

Output3:
Column1,Column8,Column5,Column6,Column7,FILE3

Thanks.
# 2  
Old 05-07-2009
Code:
awk '{
	print $1,$3,$6,$4,$5,"FILE1" > "output1.file"
	print $3,$8,$9,$1,$2,"FILE2" > "output2.file"
	print $1,$8,$5,$6,$7,"FILE3" > "output3.file"
	}' inputfile

# 3  
Old 05-07-2009
Thanks for your reply but it gives syntax error....also if I run only for one file I get problems if some column has % symbol (column 10 has it).....example:

Input file:
0104100400,C,41.26,43.33,45.39,04/APR/2009,2009,02,MAR,LPF 3: % YTD(Main Exchange),03/APR/2010

Output1 should be (say column 2,10,3,6):

C,LPF 3: % YTD(Main Exchange),41.26,04/APR/2009,FILE1

Output2 should be (say column 2,10,4,11):

C,LPF 3: % YTD(Main Exchange),43.33,03/APR/2010,FILE2
# 4  
Old 05-08-2009
I have tried cut, paste options but that is not what I want to use....If I use the following command
awk '{print $1,$3,$6,$4,$5,"FILE1" > "output1.file"}' inputfile I don't get the desired results.....

Input file:

0104100400,C,41.26,43.33,45.39,04/APR/2009,2009,02,MAR,LPF 3: % YTD(Main Exchange),03/APR/2010

Output (desired):
0104100400,41.26,04/APR/2009,43.33,45.39,FILE1


Output (actual with this awk command):
0104100400,C,41.26,43.33,45.39,04/APR/2009,2009,02,MAR,LPF % YTD(Main Exchange),03/APR/2010 FILE1
# 5  
Old 05-08-2009
Reset the default field separators:

Code:
awk 'BEGIN{FS=OFS=","}{..}' file

# 6  
Old 05-08-2009
I do not see any pattern on what criteria you should select the fields. Can you explain?

But for this one , you should use the field delimeter as , otherwise it is taking the space.

Code:
awk '{print $1,$3,$6,$4,$5,"FILE1" >> "outfile.txt}' OFS=","



cheers,
Devaraj Takhellambam
# 7  
Old 05-08-2009
Thanks Devaraj and Franklin,

I will use the following:

awk 'BEGIN{FS=OFS=","}{print $1,$3,$10,$5,"FILE1"}' Input.txt >> output.txt
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to add portion of text to files in directory using numerical match

In the below bash I am trying to rename eachof the 3 text files in /home/cmccabe/Desktop/percent by matching the numerical portion of each file to lines 3,4, or 5 in /home/cmccabe/Desktop/analysis.txt. There will always be a match between the files. When a match is found each text file in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Perl: batch replace a portion of text in files

Hi all, What I would like to achieve is to batch change the code below in every pdf in a given directory (each pdf is uncompressed so that can be easily edited). An example of the javascript code: if (this.hostContainer) { try { this.hostContainer.postMessage(pp_216', 15259]);... (3 Replies)
Discussion started by: menteith
3 Replies

3. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

4. Shell Programming and Scripting

Renaming portion of multiple files

Hi, I have some files as below. Temp6001_5025.cpp Temp6002_5025.cpp Temp6003_5025.cpp Temp6004_5025.cpp Temp6005_5025.cpp Temp6006_5025.cpp Temp6007_5025.cpp . . Temp6100_5025.cpp i want to replace 5025 to 5026 in all files and the result should be Temp6001_5026.cpp . .... (3 Replies)
Discussion started by: lathigara
3 Replies

5. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

6. Shell Programming and Scripting

Grep certain portion from the file

Dear Friends, Here I am with another difficulty. I have a flat file from which I wanna grep following pattern. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Statement Date : Blah blah Blah blah Blah blah Blah blah... (1 Reply)
Discussion started by: anushree.a
1 Replies

7. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

8. UNIX for Dummies Questions & Answers

Print a portion of file

Hi, I have a little problem. I am having a file with pattern like : asdf;ffgg;dfjfj;djdfjf;nnjj;djd;ssj; I just want to print the portion from last ";" upto the immediate previous ";". There are several ";" in my line. Please help me out... Thnx in advance (8 Replies)
Discussion started by: vanand420
8 Replies

9. Programming

Delete Portion of a file

hi i would like to know whether i can delete a part of a file in C for eg. if my file contained 1234567890 and i want to delete 456 so that it becomes 1237890 is there a way i can do this. well, one way i can achieve this is by creating a new file, copy whatever i want, then delete the... (2 Replies)
Discussion started by: sameersbn
2 Replies

10. Shell Programming and Scripting

remove portion of file

Can anyone tell me how to remove a portion of a large file to smaller ones? What I have is a large file that was created becasue several similar files were joined together. Each individual file starts with MSG_HEAD. I want to take everything from MSG_HEAD up to were it says MSG_HEAD again and... (13 Replies)
Discussion started by: methos
13 Replies
Login or Register to Ask a Question