search between keywords and make a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search between keywords and make a single line
# 1  
Old 10-30-2012
search between keywords and make a single line

have a very big file where need to format it like below

example file:

abcd today
is
great
day;

search keyword 'abcd' and append to it all words till we reach ; to make it a single line.

output should look like.

abcd today is great day;

There are many occurrence of such paragraphs and text file also contain other lines which we do not want to touch.
# 2  
Old 10-30-2012
Please do not try to oversimplify problems. State them as they stand. Also, provide a representative sample which shows all (or most of) the possible ways in which the input data might occur.

With lots of assumptions, try:
Code:
sed '/abcd/,/;/{
H
/;/{
      s/.*//
      x
      s/^\n//
      s/\n/ /g
      p
 }
d
}' file


Last edited by elixir_sinari; 10-31-2012 at 12:06 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-30-2012
Code:
$
$
$ cat f14
abcd today
is
great
day;
abcd
another one
over
here;
some
text
here;
and some more...
$
$
$
$ perl -ne 'chomp;
            if (/abcd/)         {$x .= $_; $in = 1}
            elsif (/;/ and $in) {print "$x $_\n"; $in = 0; $x = ""}
            elsif ($in)         {$x .= " $_"}
            else                {print $_,"\n"}
           ' f14
abcd today is great day;
abcd another one over here;
some
text
here;
and some more...
$
$
$ perl -0ne 'while (/^(abcd.*?;|.*)$/msg) {$x=$1; $x =~ s/\n/ /g if $x =~ m/abcd/; print "$x\n"}' f14
abcd today is great day;
abcd another one over here;
some
text
here;
and some more...

$
$
$

tyler_durden

Last edited by durden_tyler; 10-31-2012 at 12:09 AM..
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

2. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

3. UNIX for Dummies Questions & Answers

search all file for particular text and make changes to line 3

Hi All, I am sitting on HPUX. I want to change the exit into #exit, which appears into 3red line of code in shell scripting, wondering how shell script to be called up to perform action. I have following code in all files. Now, I need to find the text exit and replace into #exit. #!/sbin/sh... (10 Replies)
Discussion started by: alok.behria
10 Replies

4. Shell Programming and Scripting

Try to make a single line to syslog.

Hi again: Following the thread: https://www.unix.com/shell-programming-scripting/147755-format-lines-file.html I couldn't solve my problem, so I ask you again gurus: I have this code: nohup /usr/sbin/auditstream | /usr/sbin/auditselect -m -e "event== S_ENVIRON_WRITE || event==... (2 Replies)
Discussion started by: iga3725
2 Replies

5. Shell Programming and Scripting

Search a file with keywords

Hi All I have a file of format asdf asf first sec endi asdk rt 123 ferf dfg ijglkp (7 Replies)
Discussion started by: mailabdulbari
7 Replies

6. Shell Programming and Scripting

How to search for keywords in subsequent lines

Hi all, I am looking for a coomand to search for the keywords in susequenct lines. Keyword1 in a line and Keyword2 in the very next line. Once i found the combination ineed to print the lines with patterns and the line above and one below. I am giving an example here: Keywords are :ERROR and... (12 Replies)
Discussion started by: rdhanek
12 Replies

7. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

8. Shell Programming and Scripting

make multiple line containing a pattern into single line

I have the following data file. zz=aa azxc-1234 aa=aa zz=bb azxc-1234 bb=bb zz=cc azxc-1234 cc=cc zz=dd azxc-2345 dd=dd zz=ee azxc-2345 ee=ee zz=ff azxc-3456 ff=ff zz=gg azxc-4567 gg=gg zz=hh azxc-4567 hh=hh zz=ii azxc-4567 ii=ii I want to make 2nd field pattern matching multiple lines... (13 Replies)
Discussion started by: VTAWKVT
13 Replies

9. Shell Programming and Scripting

Search and replace words between two keywords

Hi, I have a file which contains the following : select * from test where test_id=1; select id from test1, test2 where test_id=1 and test_id=2; select * from test1, test2, test3 where test_id=4 and test2_id where in (select test2_id from test2); select id1, id2 from test ... (6 Replies)
Discussion started by: vrrajeeb
6 Replies
Login or Register to Ask a Question