Cut a file in two file selecting a row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut a file in two file selecting a row
# 1  
Old 06-18-2010
Cut a file in two file selecting a row

I all,
i've got this file:
Code:
163.162.126  MGW1
163.162.126  MGW2
163.162.126  MGW3
163.162.126  MGW4
163.162.135 RNC3
163.162.135.RNC23
163.162.126. HLR Node A
163.162.126.HLR Node B
163.162.126.HLR Cluster            
163.162.126.UMSC3 Node A
163.162.126.UMSC3 Node B
163.162.126.UMSC3 Cluster            
163.162.126.UMSC4 Node A
163.162.126.UMSC4 Node B
163.162.126.99   UMSC4 Cluster            
163.162.126.UMSC5 Node 
163.162.126.UMSC5 Node B
163.162.126.UMSC5 Cluster            
163.162.126.APG43
163.162.126.GGSN2 J20
163.162.126.SGSN2
163.162.126.SGSN3
163.162.135.BSC3 Cluster             
163.162.127.CCN SSH                  
163.162.127.CCN HTTPS

How can i create two file in this way:

the first one from the start to a specific row e.g. "163.162.126.UMSC4 Node B"
and the second one for a specific row e.g. "163.162.126.99 UMSC4 Cluster" till the end.


thx in advance!!!

Last edited by Franklin52; 06-18-2010 at 01:51 PM.. Reason: Please use code tags
# 2  
Old 06-18-2010
Something like this:

Code:
 
awk -v f=0 '/163.162.126.UMSC4 Node B/ {f=0} /163.162.126.99 UMSC4 Cluster/ {f=1} { print > "file_"f }'  input_file

# 3  
Old 06-18-2010
a sed way
Code:
S="UMSC4 Node B"
sed -n "1,/$S/p" inputfile > file1
sed  "1,/$S/d" inputfile > file2

# 4  
Old 06-21-2010
Thanks guys, are both perfect!
# 5  
Old 06-21-2010
panyam can u please explain me hows that awk command working.. if u have some time
# 6  
Old 06-21-2010
Sure Smilie


Code:
 
Till awk encounters the line having "163.162.126.UMSC4 Node B" , 
place all the lines in the file_0 ( since f=0 by default) 
 
Once you got the line having : "163.162.126.99 UMSC4 Cluster" set  f to 1 , 
so all the consequent lines will be placed in the file file_1 ( since f is now 1)

# 7  
Old 06-21-2010
thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. 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

4. 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

5. Shell Programming and Scripting

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

6. Shell Programming and Scripting

Selecting lines of a file

Say I wanted to select the 5th line of a file without knowing the context of the file. Would I use grep and pipe it into wc or is there a more simple way of doing this? (3 Replies)
Discussion started by: puttster
3 Replies

7. 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

8. Shell Programming and Scripting

Selecting one file from a list

Hi, I am able to do this by brute force but I am just curious if there is a better way of handling things. Basically the scenario is something like this: There are a number of files in a directory: rib.20071224.1759.gz 24-Dec-2007 17:59 132K rib.20071224.1959.gz 24-Dec-2007... (7 Replies)
Discussion started by: Legend986
7 Replies

9. 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

10. Shell Programming and Scripting

Selecting records from file on criteria.

Can I have 2 files as in input to the awk command? Situation is somewhat below, File A contains number & value delimited by a space. File B contains number as a part of a line. I am not supposed to retrieve more than 1 number from a line. If number from file B matches with number from... (7 Replies)
Discussion started by: videsh77
7 Replies
Login or Register to Ask a Question