Copy selective lines from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy selective lines from text file
# 1  
Old 05-10-2011
Error Copy selective lines from text file

Hello,

I have a text file which I need to check for presence of certain tags, and then copy a subsequent portion of text into another file. The tag matching canbe done with Grep but I do not know how to copy selective lines from one file to another. Is it possible do that?

I checked up some options. "cp" command copies the entire file, which I do not want !!

Can someone please help me out??
# 2  
Old 05-10-2011
sample input and required output please

regards,
Ahamed
# 3  
Old 05-10-2011
Sample Input and Output

Input : is a tagged dataset.

Code:
.I 1
.T
Preliminary Report-International Algebraic Language
.B
CACM December, 1958
.A
Perlis, A. J.
Samelson,K.
.N
CA581203 JB March 22, 1978  8:28 PM
.I 2
.T
Extraction of Roots by Repeated Subtractions for Digital Computers
.B
CACM December, 1958
.A
Sugai, I.
.N
CA581202 JB March 22, 1978  8:29 PM

and so on..

Output : Based on the .I tags I should store in 2 separate files.. the details of .T and .N tags.
For example,

File 1
Code:
.T
Preliminary Report-International Algebraic Language
.N
CA581203 JB March 22, 1978  8:28 PM

and file 2
Code:
.T
Extraction of Roots by Repeated Subtractions for Digital Computers
.N
CA581202 JB March 22, 1978  8:29 PM

# 4  
Old 05-10-2011
Try this

Code:
awk '/^\.I/ {_2="file"$2".txt"} /^\.T|^\.N/{_1=$0;getline;print "echo -e \""_1"\\n"$0"\">>"_2 }' inputfile | bash

regards,
Ahamed

Last edited by ahamed101; 05-10-2011 at 06:14 PM..
# 5  
Old 05-10-2011
Re : Some error

Hello,
It worked fine but then it threw up an error..

Code:
bash: line 4321: syntax error near unexpected token `('
bash: line 4321: `echo -e ".T\nAn Algorithm for the Blocks and Cutnodes of a Graph (Corrigendum)">>file2161.txt'

Any ideas?

Regards,
Ajay
# 6  
Old 05-10-2011
Piping content into a shell can be dangerous, avoid the pipe to "bash", here's the suggested script with revisions using "awk" to write directly to the output files without the need to pipe to bash ...
Code:
awk '
  /^\.I/ {
    _2="file"$2".txt"
  }
  /^\.T|^\.N/ {
   _1=$0;
   getline;
   print _1  "\n" $0 >> _2
  }
' inputfile


Last edited by Franklin52; 05-11-2011 at 03:45 AM.. Reason: Please use code tags, thank you
# 7  
Old 05-11-2011
Thanks frank.
Ajay try this

Code:
awk '/^\.I/ {_2="file"$2".txt"} /^\.T|^\.N/{_1=$0;getline;print _1"\n"$0 >> _2  }' infile

regards,
Ahamed
This User Gave Thanks to ahamed101 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

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Copy lines from x to y to another file

OS : RHEL 7.2 Shell : bash I have a file which has lines like below I want to copy from 2nd line to the 6th line and copy(redirect) those lines to another file. $ cat patterns.txt hello world hello asia hello europe hello africa hello america hello antartica hello... (9 Replies)
Discussion started by: omega3
9 Replies

3. Shell Programming and Scripting

Bash selective copy folders and content to another location

I'm looking for a bash scrypt to copy some folders and some of the content to another location. I'm a teacher and very noobish with programming language anyway what I'm looking for , I have this director structure Main director "Students" with subfolders "john";"daisy";"work" etc .. and some of... (2 Replies)
Discussion started by: brickleul
2 Replies

4. Shell Programming and Scripting

How to copy selective list of files to a directory?

Hi I have 3 directories indexes_with_ts indexes_without_ts process_indexes in each directories it contains *.sql how do I accomplish this: for all the files found in indexes_without_ts, copy the corresponding file in indexes_with_ts to process_indexes. i.e. for... (2 Replies)
Discussion started by: jediwannabe
2 Replies

5. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Perl: selective printing of lines

Hi, I have a file with lines like this. 2 7 18 ggcgt anna 2 7 18 hhchc sam 3 7 18 hhdjcc ross 4 7 18 hhcjd jenny 0 8 21 jjdhs sam 3 8 21 kkok bush 2 9 24 kosss BrenhamIf the values of the second column are equal, print only those lines with the least first column value. So in... (5 Replies)
Discussion started by: polsum
5 Replies

7. Shell Programming and Scripting

selective replacing text using sed/perl

Hi, I have the following text which I want to search and replace using perl and sed. I would appreciate any help. Please notice the file contains schema name with a single dot and a double dot . &&WEBDIR_SCHEMA. and &&WEBDIR_SCHEMA .. } I would like to change it to the acutal schema... (5 Replies)
Discussion started by: jville
5 Replies

8. Shell Programming and Scripting

sed/awk script selective insert between lines

Hi I have a file in the foll. format *RECORD* *FIELD NO* ....... ....... *FIELD TX* Data *FIELD AV* Data *FIELD RF* *RECORD* *FIELD NO* ....... ....... *FIELD TX* Data *FIELD RF* (4 Replies)
Discussion started by: dunstonrocks
4 Replies

9. Shell Programming and Scripting

selective printing of lines

Hi all , i need to grep for a string in a text file and print the string and the 3rd line above it. As always , Thanks. (4 Replies)
Discussion started by: okiedokie
4 Replies

10. Shell Programming and Scripting

clip selective text and minus

After doing some scripting in a log file, I am left with some lines like this. 10:00:00 Received Message Message ID 1 10:05:00 Published Message Message ID 1 10:10:00 Received Message Message ID 2 10:15:00 Published Message Message ID 2 10:20:00 Received Message Message ID 3 10:26:00... (1 Reply)
Discussion started by: srini123
1 Replies
Login or Register to Ask a Question