Extract text from file and save as individual file


 
Thread Tools Search this Thread
Top Forums Programming Extract text from file and save as individual file
# 1  
Old 11-26-2012
Extract text from file and save as individual file

Dear All

I am using the following shell script to extract the columns from the file.

Code:
for filename in *.rpt
do
 awk -F"\t" '
    BEGIN {OFS="|"} FNR > 0 {print $1,$2,$3,$5,FILENAME}
    ' $filename >> output.txt
done

However, the script works fine. Instead of saving the single output file, I want to modify the script so that it saves as individual files.

Kindly advice.

Many Thanks
Balaji

Last edited by Corona688; 11-26-2012 at 11:37 AM..
# 2  
Old 11-26-2012
change

Code:
$filename >> output.txt

to

Code:
$filename >> $(echo $filename)-output.txt

Will create "$filename-output.txt" for each $filename.
# 3  
Old 11-26-2012
Quote:
Originally Posted by expl
Code:
$filename >> $(echo $filename)-output.txt

Will create "$filename-output.txt" for each $filename.
The echo command substitution is unnecessary. A simple $filename will do.

Regards,
Alister
# 4  
Old 11-26-2012
Thank you for trying to use code tags. You got it nearly right. Next time use square brackets, [ ] , not triangular brackets, < >.

expl: That is a useless use of backticks. You don't need echo's help to get the value of a variable. You probably need { } to get the variable to expand properly however. ${filename} >> ${filename}-output.txt

This can be made much simpler by feeding the filenames right into awk. No loop necessary. It can even change the filename itself. It will work right with multiple files because FNR counts the line number inside a file, not the total numer of lines (NR).

FNR>0 will always print, incidentally, because FNR will always be a nonzero number. The very first line is 1. If you want it to skip the very first line of each file, that willl be FNR>1

Code:
awk 'BEGIN {OFS="|"} FNR > 0 {print $1,$2,$3,$5,FILENAME > FILENAME"-output.txt"}' *.rpt

# 5  
Old 11-26-2012
Dear All

Thanks for all your suggestion and help.

It works.

Many Thanks
Balaji Smilie

Quote:
Originally Posted by Corona688
Thank you for trying to use code tags. You got it nearly right. Next time use square brackets, [ ] , not triangular brackets, < >.

expl: That is a useless use of backticks. You don't need echo's help to get the value of a variable. You probably need { } to get the variable to expand properly however. ${filename} >> ${filename}-output.txt

This can be made much simpler by feeding the filenames right into awk. No loop necessary. It can even change the filename itself. It will work right with multiple files because FNR counts the line number inside a file, not the total numer of lines (NR).

FNR>0 will always print, incidentally, because FNR will always be a nonzero number. The very first line is 1. If you want it to skip the very first line of each file, that willl be FNR>1

Code:
awk 'BEGIN {OFS="|"} FNR > 0 {print $1,$2,$3,$5,FILENAME > FILENAME"-output.txt"}' *.rpt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract line from file and save as new file.

Hello, I have a tab-file "result.txt "that looks like this CNV.ID Sample Correlation N.comp Start.b End.b CNV.type N.exons BF Reads.expected Reads.observed Reads.ratio Gene 1 S10.Run.variant_ready 0.999411647 7 381 382 duplication 2 ... (9 Replies)
Discussion started by: nans
9 Replies

2. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

3. Shell Programming and Scripting

Use text of one file to save in another

I am trying to use the text of one file as a text file name with and the text of another as the contents of that text file. Is this possible? Thank you :). For example, in the two files attached, target.txt has: 1.txt 2.txt and out_parse.txt has: 13 20763642 20763642 C G... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

How to save the 'nmon' output to a text file with a script?

Hi all, I want to do an Unix Script to save the 'nmon' output on a text file and I don't know how to do it. I need a Script for each monitoring and also one to multiple monitorings. Thanks (6 Replies)
Discussion started by: Javi1990
6 Replies

5. Shell Programming and Scripting

Read a file name from a text file and save it in a variable

i have a text file consists of different file names like: line 1: lib/libIMb.so message broker file line 2: lil/imbdfg.lil message broker file i need to extract libIMb.so and imbdfg.lil files from those lines and save them in a variable. so that i can search for... (9 Replies)
Discussion started by: santosh2626
9 Replies

6. Shell Programming and Scripting

Save result to a text file

Currently I have a perl code to combine two different files. #! /usr/bin/perl -w use strict; open FP1,"A.txt"; open FP2,"B.txt"; my ($l1,$l2); while(1) { $l1=<FP1>; chomp $l1; $l2=<FP2>; chomp $l2; last unless(defined $l1 or defined $l2); print "$l1 $l2\n"; } close FP2;... (4 Replies)
Discussion started by: Tzeronone
4 Replies

7. Shell Programming and Scripting

Open Text file input data and save it.

Hi Guys, I have blank file A.txt I will run the script xyz.sh First i want to open a.txt file... Now i will enter some data like XYZ ABC PQR .. Save it and keep continue my script.... END of my script. Thanks (1 Reply)
Discussion started by: asavaliya
1 Replies

8. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. Shell Programming and Scripting

can I save list of files in memory and not in text file?

Hello all im using allot with the method of getting file list from misc place in unix and copy them into text file and then doing misc action on this list of files using foreach f (`cat file_list.txt`) do something with $f end can I replace this file_list.txt with some place in memory? ... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question