Get a group of line from different file and put them into one file row by row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get a group of line from different file and put them into one file row by row
# 1  
Old 12-23-2010
Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem.

the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example.

I have different html files and each of them have a consecutive lines group inside that i want to extract.
example:

PHP Code:
file1.html

<tag>
blablabla1
blablabla2
blablabla3
</tag

PHP Code:
file2.html
<tag>
blablabla4
blablabla5
blablabla6
</tag

PHP Code:
file3.html
<tag>
blablabla7
blablabla8
blablabla9
</tag
ok? well i wanna filter the blablabla groups if lines and put them in one file that has the below structure:

PHP Code:
blablabla1;blablabla2;blablabla3
blablabla4
;blablabla5;blablabla6
blablabla7
;blablabla8;blablabla9 
is it possible? THANKS A LOT
# 2  
Old 12-23-2010
Try this:
Code:
awk 'FNR==1{if(p)print p;p=x}!/tag/{p=p (p?";":x)$1}END{print p}' file[123].html

# 3  
Old 12-23-2010
If file1.html has following:
Code:
<tag>
blablabla11
blablabla12
blablabla13
</tag> 
<tag>
blablabla21
blablabla22
blablabla23
</tag>

Then above command gives (having other two files same as above)
Code:
blablabla11;blablabla12;blablabla13;blablabla21;blablabla22;blablabla23
blablabla4;blablabla5;blablabla6
blablabla7;blablabla8;blablabla9

Bit I believe expected output would be:
Code:
blablabla11;blablabla12;blablabla13
blablabla21;blablabla22;blablabla23
blablabla4;blablabla5;blablabla6
blablabla7;blablabla8;blablabla9

Also if file1.html has following(which is possible in a html file):
Code:
fsdgfdsgfsdg
fgdfgdfgdfgdfg
<tag>
blablabla11
blablabla12
blablabla13
</tag> 
fgfgdfg
fgfdgfggfg

Then above command won't give expected output.
Following would be able to handle these input file variations:
Code:
awk '/\<tag\>/,/\<\/tag\>/{if($0=="<tag>")next;if($0=="</tag>"){if(f)print f;f="";next;}f?f=f";"$0:f=$0;}' file[123].html

# 4  
Old 12-23-2010
Hi Anurag, the OP is not expicit about this but I took it to mean the content in a file per line.
# 5  
Old 12-23-2010
That's right. As long as all lines in html files are in only one group, no issues.
# 6  
Old 12-28-2010
sorry but i obtain nothing....i wanna write this results in a file....
# 7  
Old 12-28-2010
try this,

Code:
 paste -s file[123].html | sed 's#</.*>\s*<.*>\s#\n#g;s#</.*>##g;s#<.*>\s*##g'

> nefile

Last edited by pravin27; 12-28-2010 at 12:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

Read line from the file and append it to each row

Hi All, We have a file in the following format: 0.010000 $ ITI 11 LV2 $ 40456211 $ 0.135000 $ ITI 11 LV1 $ 40512211 $ 1.215600 $ ITI 11 ITI3 $ 41406211 $ 24/05/2014 14:05:02 0.030000 $ ITI 11 LV2 $ 40456211 $ ... (3 Replies)
Discussion started by: gauravsinghal79
3 Replies

3. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

4. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

5. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

6. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Shell Programming and Scripting

Replace Second row with First row in a File

Hi, I need to replace first row to second row: Input: A JACK WAS_${HH}_JACK .... Output should be: JACK WAS_A_JACK .... I tried below code.. awk '{TEMP= (NR==1)}; {sub(/${HH}/$TEMP/)}' (2 Replies)
Discussion started by: kmsekhar
2 Replies

8. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

9. Shell Programming and Scripting

Append Second Row to First Row @ end in a file

Hi I want to append line 2n to 2n-1 line where n=1...LastLine in my file. eg: Actual File: Hello Everyone Welcome TO Unix I need Your help Required File: HelloEveryone WelcomeTO Unix I needYour help (5 Replies)
Discussion started by: dashing201
5 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question