how to print the certain lines in a file to different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to print the certain lines in a file to different files
# 1  
Old 12-29-2009
how to print the certain lines in a file to different files

Hi All,

File that I have:
Code:
 
<ct>
<name>group
<value>1
<value>2
<value>3
</ct>-->file

The output that I needed is
Code:
 
<ct>
<name>group
<value>1 -->file1
 
<ct>
<name>group
<value>2 -->file2
 
<ct>
<name>group
<value>3 -->file3

I have tried to use this code
Code:
 
awk '/^<ct>/{cnt=3}cnt-->0'

but just can give me the output for file1.
Is there any other method?
Thanks for help!

Last edited by Yogesh Sawant; 12-31-2009 at 02:01 PM..
# 2  
Old 12-29-2009
Code:
cat abc.txt | perl -e 'while (<>){
chomp;
if(m/^\<value\>(.*)$/){
open my $fh , ">" , "file$1"  || die "$!";
print $fh  "$comm_lines"."$_";
close $fh;
next;
}
$comm_lines .= $_."\n";
}'

# 3  
Old 12-29-2009
Try...
Code:
awk -F \> '/<ct>/{c=$0}/<name>/{n=$0}/<value>/{printf("%s\n%s\n%s\n",c,n,$0)>"file" $2}' file

Result...
Code:
$ head file*
==> file <==
<ct>
<name>group
<value>1
<value>2
<value>3
</ct>

==> file1 <==
<ct>
<name>group
<value>1

==> file2 <==
<ct>
<name>group
<value>2

==> file3 <==
<ct>
<name>group
<value>3

# 4  
Old 12-30-2009
Hi Ygor,



Dunno whether I ask ask you futher or not in d forum..how about if my file becomes like this:



Code:
 

<gp>

<wt>a</wt>

<wt>b</wt>

<wt>c</wt>

<ct>

<name>group</name>

<value>1</value>

<value>2</value>

<value>3</value>

</ct>

</gp>-->file



The output that I needed is



Code:
 

<wt>a</wt>

<ct>

<name>group</name>

<value>1 </value>

 </ct>-->file1

 

<wt>b</wt>

<ct>

<name>group</name>

<value>2</value>

</ct>-->file2

 

 

<wt>c</wt>

<ct>

<name>group</name>

<value>3</value>

</ct>-->file3







Thanks!
# 5  
Old 12-30-2009
Try...
Code:
$ awk -F '[<>]' '/<ct>/{c=$0}/<name>/{n=$0}/<value>/{printf("%s\n%s\n%s\n</ct>\n",c,n,$0)>"file" $3}/<wt>/{print $0>"file" ++c}' file
$ head file*
==> file <==
<gp>
<wt>a</wt>
<wt>b</wt>
<wt>c</wt>
<ct>
<name>group</name>
<value>1</value>
<value>2</value>
<value>3</value>
</ct>

==> file1 <==
<wt>a</wt>
<ct>
<name>group</name>
<value>1</value>
</ct>

==> file2 <==
<wt>b</wt>
<ct>
<name>group</name>
<value>2</value>
</ct>

==> file3 <==
<wt>c</wt>
<ct>
<name>group</name>
<value>3</value>
</ct>

# 6  
Old 12-30-2009
Hi Ygor,

Can you please explain the code?

May I know why need to have "$3" when redirect the file?

Besides I have tried the code, but in each file I just have the output of <wt> tag

I just modify abit


Code:
awk  ' BEGIN {FS="[<>]"} 
/<ct>/{c=$0}/<name>/{n=$0}/<value>/{printf("%s\n%s\n%s\n</ct>\n",c,n,$0)>"file" $3}/<wt>/{print $0>"file" ++c}' file

The output that I get:

0
<wt>a</wt>

1
<wt>b</wt>

2
<wt>c</wt>

3
<ct>
<name>group</name>
<value>1</value>
<ct>
<name>group</name>
<value>2</value>
<ct>
<name>group</name>
<value>13</value>

Thanks,

Last edited by natalie23; 12-30-2009 at 04:51 AM..
# 7  
Old 12-30-2009
Code:
my $header;
while(<DATA>){
	if(/<value>([0-9]+)/){
		chomp;
		print $header,$_,"-->file",$1,"\n";
	}
	else{
		$header=$header.$_;
	}
}
__DATA__
<ct>
<name>group
<value>1
<value>20
<value>3
</ct>-->file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

3. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

4. Shell Programming and Scripting

Print first 20 lines from all .log files

RHEL 5.8 In a directory I have more than 200 files with the extension .log Using head command I want to print first 20 lines of each log file. For each .log file, it should print like below Printing first 20 lines of : GRP_error_April29.log Apr 29 04:02:05 raptor03b syslogd 1.4.1:... (4 Replies)
Discussion started by: John K
4 Replies

5. Shell Programming and Scripting

Look up between 2 files and print matching lines

Hi, I have 2 large log files in .gz format file 1 contains abcde 12345 23456 . . . . . . . . 09123 (8 Replies)
Discussion started by: aravindj80
8 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

7. Shell Programming and Scripting

AWK print lines into multiple files

Hi, i have an input text file like this: Student 1 maths science = Student 2 maths science = Student 3 maths science i would like to print each student information into separate files, each student id is separated by "=". (1 Reply)
Discussion started by: saint2006
1 Replies

8. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

9. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

10. Shell Programming and Scripting

print the first 5 lines in which 2 files differs

it is a simple problem, but I am totally new to shell programming There are 2 files. I have to print the 5 five lines which are different. I appreciate any help. 10x. (2 Replies)
Discussion started by: atticus
2 Replies
Login or Register to Ask a Question