Search a text, and Join the next line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a text, and Join the next line.
# 1  
Old 07-31-2011
Question Search a text, and Join the next line.

Hello Everybody,

I am New to Unix Environment, and need some help.

Basically i have got a file named XYZ.txt with following pattern.

Code:
CR Len:102 Typ:1 Nro:71818037 Chk:1 Tim:2011-07-16/08:46:03 oiw:3 oTy:145 oAd:50206 diw:0 dTy:145 dAd:919399951328 SMC:919821900040
iTm:2011-07-16/08:46:03 dat:0 tcl:1 mLn:95 pid:0 srr:62 def:0 sde:LM-UPFJOY 
con:0-0-0 spe:0 dcs:0 ame:1 pri:4 vms:919821000001 ims:N/A                    
CR Len:102 Typ:1 Nro:71818038 Chk:1 Tim:2011-07-16/08:46:03 oiw:3 oTy:145 oAd:50206 diw:0 dTy:145 dAd:919848170499 SMC:919821900040
iTm:2011-07-16/08:46:03 dat:0 tcl:1 mLn:95 pid:0 srr:62 def:0 sde:LM-UPFJOY 
con:0-0-0 spe:0 dcs:0 ame:1 pri:4 vms:919821000001 ims:N/A

Now what i got to do is after the word
LM-UPFJOY "a space" and then Join the next consecutive line.
Is this possible?

Thanks in advance.
Abhi


Last edited by Franklin52; 08-01-2011 at 12:06 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-31-2011
Code:
sed  '/LM-UPFJOY *$/ {N; s/ *\n/ / }' INPUTFILE

# 3  
Old 07-31-2011
awk solution:
Code:
awk '1' RS='LM-UPFJOY *\n' ORS='LM-UPFJOY ' input

# 4  
Old 08-01-2011
Thanks Yazu for your prompt reply,
but i am so sorry to say that i gave an error saying,
Code:
sed: Function /LM-UPFJOY *$/ {N; s/ *\n/ / } cannot be parsed.

Kindly help.

---------- Post updated at 06:24 PM ---------- Previous update was at 06:20 PM ----------

In case of Mirni's solution, it gave some weired results as following.

Code:
CR LM-UPFJOY en:102 Typ:1 Nro:71818037 Chk:1 Tim:2011-07-16/08:46:03 oiw:3 oTy:145 oAd:50206 diw:0 dTy:145 dAd:919399951328 SMC:919821900040
iTm:2011-07-16/08:46:03 dat:0 tcl:1 mLM-UPFJOY n:95 pid:0 srr:62 def:0 sde:LM-UPFJOY M-UPFJOY 
con:0-0-0 spe:0 dcs:0 ame:1 pri:4 vms:919821000001 ims:N/A                    
CR LM-UPFJOY en:102 Typ:1 Nro:71818038 Chk:1 Tim:2011-07-16/08:46:03 oiw:3 oTy:145 oAd:50206 diw:0 dTy:145 dAd:919848170499 SMC:919821900040
iTm:2011-07-16/08:46:03 dat:0 tcl:1 mLM-UPFJOY n:95 pid:0 srr:62 def:0 sde:LM-UPFJOY M-UPFJOY 
con:0-0-0 spe:0 dcs:0 ame:1 pri:4 vms:919821000001 ims:N/A

Which I guess had replaced the L in Len:102 by LM-UPFJOY.
Kindly Help.

Last edited by Franklin52; 08-01-2011 at 12:07 PM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 08-01-2011
You can try:
Code:
sed  '/LM-UPFJOY *$/ {
N
s/ *\n/ / 
}' INPUTFILE

There are too many seds in *nixes.

Last edited by Franklin52; 08-01-2011 at 12:07 PM.. Reason: Code tags
# 6  
Old 08-01-2011
This Worked.. 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 and Replace a text if the line contains a pattern

My text file looks like below . . . abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt $Bad_abcd_REJ=rcrd_op.txt ghijklm $Bad_abcd_TYHS=rcrd_op.txt abcgd abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt (2 Replies)
Discussion started by: machomaddy
2 Replies

2. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

3. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

4. UNIX for Dummies Questions & Answers

VIM search and replace with line breaks in both the target and replacement text

Hi, Ive spent ages trying to find an explanation for how to do this on the web, but now feel like I'm :wall: I would like to change each occurence (there are many within my script) of the following: to in Vim. I know how to search and replace when it is just single lines... (2 Replies)
Discussion started by: blueade7
2 Replies

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

6. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

7. Shell Programming and Scripting

reverse search a text file from a specified line

Hello All, I have a following task that I need to accomplish through a script or program and I am looking for some help as I have exhausted my ideas. 1. given: a text file with thousands of lines 2. find: pattern A in file and get line number ( grep -n works) 3. find: the first occurence of... (14 Replies)
Discussion started by: PacificWonder
14 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

Search and replace multi-line text in files

Hello I need to search for a mult-line text in a file exfile1 and replace that text with another text. The text to search for is in exfile2 and the replacement text is in exfile3. I work with kornshell under AIX and need to do this with a lot of files. (the file type is postscript and they need... (10 Replies)
Discussion started by: marz
10 Replies

10. Shell Programming and Scripting

Search for text and print the next line

Hi, I want to write a small script to search for a text in file and when its found I want to print the next line. I try to write that script but I could not manage it, I just write the following script the find the exact line but I want the next line. $ sed -n -e '/Form not/p' test.txt... (2 Replies)
Discussion started by: alijassim
2 Replies
Login or Register to Ask a Question