Copying lines between two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying lines between two strings
# 1  
Old 12-05-2012
Copying lines between two strings

Hello All,
I want to copy some lines from one file to other with following condition.

Only lines between two specified strings should copy.

Example :-

Code:
"My First String "
Some_Other_String ....
Some_Other_String ....
Some_Other_String ....
"My Second String"

So only lines between
Code:
 "My First String "

and
Code:
"My Second String"

should get copied to other ifle.
# 2  
Old 12-05-2012
Try

Code:
awk '/\"My Second String\"/{a=""}
a{print}
/\"My First String \"/{a=1}' file > new_file

Code:
awk '/\"My Second String\"/{a=""}
a{print > "new_file"}
/\"My First String \"/{a=1}' file

# 3  
Old 12-05-2012
Or with Sed..
Code:
$ uname -rs
SunOS 5.10
$ sed -n '/First/{
> :l
> n
> /Second/{
> q
> }
> p; bl
> }' wer
Some_Other_String ....
Some_Other_String ....
Some_Other_String ....
$

Alternate awk..
Code:
nawk '/First/,/Second/{a=/First|Second/?0:1}a' inputfile


Last edited by michaelrozar17; 12-05-2012 at 03:46 AM..
# 4  
Old 12-05-2012
Dear michaelrozar17
Your solution works absolutely fine on command line but I have to use it in shell script.What are the changes required for that.Also I want little change in its behaviour , it should include both the strings in final output.Is it possible ?
# 5  
Old 12-05-2012
It does not require any change to run in a shell script, just copy paste the sed or awk lines into a file and run. But remember to give full path name of the inputfile, its better coding.
Code:
$ cat print_patterns.ksh
#!/bin/ksh

echo "Printing between Patterns.."
sed '/First/,/Second/!d' /full/path/to/inputfile
$ chmod u+x print_patterns.ksh
$ ./print_patterns.ksh
Printing between Patterns..
"My First String "
Some_Other_String ....
Some_Other_String ....
Some_Other_String ....
"My Second String"
$

This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 12-05-2012
Thank you michaelrozar17.
This solution is working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying few lines from one file to another

Hi all I have a requirement I need to copy only first 2 lines from a file of one location to another file of same location I used to code as cp head -2 abc 123 But i get the following error cp: 0653-437 123 is not a directory. The files used are cat abc ABCD EFDG TYUI (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

2. Shell Programming and Scripting

Help with copying lines from different files.

I am new to Linux and have a challenging task. I have 3 data files, and need to do the following: Go to line 31 of file 1, delete it Read 1 line from file 2 and add in place of deleted line Go to line 97 of file I delete it and then read the line 1 from file 2 and add in place of that... (2 Replies)
Discussion started by: hamnsan
2 Replies

3. Shell Programming and Scripting

Numbering, copying and replacing strings

hello everybody there, I'm a new bash shell programmer and I'm dealing with a problem. To start, I have a file with a number of string lines which may contain a particular string or not. I have to write a code that identifies the line containing one particular string and keeps it, but also writes... (10 Replies)
Discussion started by: leaf
10 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. Shell Programming and Scripting

Copying of multiple columns of one table to another by mapping with particular strings.

Hi, I would like to copy some columns from a particular file by mapping with the string names. i am using the .csv file format. my one file consist of 100 of columns but i want only particular 4 columns such as ( First_name, Middle_name,Last_name & Stlc). but they are listed in many files... (15 Replies)
Discussion started by: dsh007
15 Replies

6. Shell Programming and Scripting

Removing empty lines(space) between two lines containing strings

Hi, Please provide shell script to Remove empty lines(space) between two lines containing strings in a file. Input File : A1/EXT "BAP_BSC6/07B/00" 844 090602 1605 RXOCF-465 PDTR11 1 SITE ON BATTERY A2/EXT... (3 Replies)
Discussion started by: sudhakaryadav
3 Replies

7. Shell Programming and Scripting

Grep and delete lines except the lines with strings

Hi I am writing a script which should read a file and search for certain strings 'approved' or 'removed' and retain only those lines that contain the above strings. Ex: file name 'test' test: approved package waiting for approval package disapproved package removed package approved... (14 Replies)
Discussion started by: vj8436
14 Replies

8. Shell Programming and Scripting

using AWK see the upper lines and lower lines of the strings??

Hi experts, You cool guys already given me the awk script below- awk '/9366109380/,printed==5 { ++printed; print; }' 2008-09-14.0.log Morever, i have one more things- when i awk 9366109380, i can also see the Upper 3 lines as well as below 5 lines of that string. Line 1.... (3 Replies)
Discussion started by: thepurple
3 Replies

9. UNIX for Dummies Questions & Answers

Copying common lines to a new file

How do i copy the common lines between 2 files to another file? for example file1: asdfg sdf gdsf file2: asdfgh asd sdf xcv file3: sdf (3 Replies)
Discussion started by: khestoi
3 Replies

10. Shell Programming and Scripting

copying strings...

hey i want to know the unix command for copying 1 string to another and allso adding a third string to the result. for eg: a b="nbno" c="uioio" i want to copy contents of b to a and the append the contents of c to the contents of a and the result shud be in string a (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question