Extract line from file and save as new file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract line from file and save as new file.
# 1  
Old 06-01-2018
Extract line from file and save as new file.

Hello,
I have a tab-file "result.txt "that looks like this
Code:
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    7.61    547    714    1.31    Gene1
2    S10.Run.variant_ready    0.999411647    7    1998    2016    duplication    19    133    14396    18691    1.3    Gene2
3    S11.Run.variant_ready    0.999286215    13    302    302    deletion    1    2.67    188    129    0.686    Gene3
4    S11.Run.variant_ready    0.999286215    13    341    341    deletion    1    4.58    548    386    0.704    Gene4
5    S11.Run.variant_ready    0.999286215    13    383    383    duplication    1    3.61    503    646    1.28    Gene5
6    S12.Run.variant_ready    0.999286215    13    388    388    deletion    1    2.8    45    24    0.533    Gene6

I need to extract each "Sample" (Column 2) and store it as a separate file with the contents. For example, from the above file, I need to extract all lines (ie, row 1&2 in this case) corresponding to "S10.Run39.variant_ready" and store it as S10.txt (ie SAMPLE name).

This is what I have tried so far

Code:
while read ID SAMPLE; do echo "$SAMPLE" > "$SAMPLE"; done < result.txt

I don't end up with a file with contents but rather each row as a file name. Please advise.

thanks

Last edited by RudiC; 06-01-2018 at 12:25 PM..
# 2  
Old 06-01-2018
A crude bash way could be:-
Code:
while read ID SAMPLE DATA
do
   outfile="${SAMPLE%%.*}"              Chop off everything after the first full-stop to decide the file name
   echo "$DATA" >> "$outfile"           Quoted in case the line we have has spaces in.
done < result.txt

The >> means we append to a file so you get all matching records.


This probably won't be very efficient for large files. In that case you would be better with an awk but let us know if you need that or if the above is sufficient.



Kind regards,
Robin
# 3  
Old 06-01-2018
Thank you @rbatte1.

The file is not too big. Not expecting the results.txt to have more than 200 lines.
It does create separate files but the header and the first two columns are missing from the output. [The header or first row ends up being a separate file by itself by the name of "Sample.txt"]

Trying this
Code:
while read ID SAMPLE DATA
do
   outfile="${SAMPLE%%.*}"              
   echo  "$ID\t$SAMPLE\t$DATA" >> "$outfile"           
done < result.txt


but doesn't produce a tab-delimited file with all contents

Last edited by nans; 06-01-2018 at 02:24 PM.. Reason: specified file type
# 4  
Old 06-01-2018
Please - what is a "tab file"?


EDIT: How far would

Code:
awk 'NR > 1 {split ($2, T, "\."); print $0 > (T[1] ".txt")}' file

get you?

Last edited by RudiC; 06-01-2018 at 12:49 PM..
# 5  
Old 06-01-2018
Hello,
I meant tab-delimited file, sorry should be been specific.


The code works by including all the columns, but again the header is missing in all the files.


Also, a very minor modification to the code


Code:
awk 'NR > 1 {split ($2, T, "\\."); print $0 > (T[1] ".txt")}' file


I was getting the following error with the single backlash


Code:
awk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'

# 6  
Old 06-01-2018
That was NOT an error, but a warning.

You did not mention you wanted the header, neither in the written specification nor in the code sample you posted.
Beyond that, do you get a satisfactory result, or not?
# 7  
Old 06-01-2018
True, not an error, but perhaps it is interesting to ponder why exactly the warning was issued here.

The reason that the escape \ does not matter in this case is that a single character, other than space, is not treated as an (extended) regex string, but as a single, literal character, and so is an escape sequence like \. (So "\." is not a regular expression here).

So in short:
  • "." a literal . character (dot).
  • "\." an escape sequence that does not have a meaning here because it does not turn a . character into a special character, hence the warning. So this also gets interpreted as a single literal dot.
  • "\\." a regular expression denoting a literal dot.
So in this case split ($2, T, "\\."), split ($2, T, "\.") and split ($2, T, ".") have the same meaning and produce identical results, while only the second one gives a warning.




--
Not relevant in the above case, but to get a regex that consists of a special . (denoting "any" character) one would need to use a regex constant instead: split ($2, T, /./). On the other hand, to use a single literal dot within a regex constant split ($2, T, /\./) would need to be used.

Last edited by Scrutinizer; 06-01-2018 at 04:08 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Programming

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. 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... (4 Replies)
Discussion started by: bala06
4 Replies

4. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

5. Shell Programming and Scripting

need to remove 1st line of a file and save the file with same old name

Hi friends, I have a doubt, I am not sure whether it is possible ah nu. I am having a file(sample.txt) which contain 5 lines. I want to remove 1st line in the file and save the file with same old name (sample.txt). For removing 1st line i am using sed 1d filename But dono how to... (3 Replies)
Discussion started by: natraj005
3 Replies

6. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

9. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

10. Shell Programming and Scripting

save every line in log file with matched string

i have been doing this script to match every line in a current log file (access_log) with strings that i list from a path (consist of 100 of user's name ex: meggae ).. and then make a directory of every string from the text file (/path/meggae/) --->if it matched.. then print every line from the... (3 Replies)
Discussion started by: meggae
3 Replies
Login or Register to Ask a Question