Help needed in formatting the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in formatting the output
# 1  
Old 08-30-2014
Tools Help needed in formatting the output

Hi All,

Need your help in resolving the below issue.

I've a file called "data.txt" with the below lines:


Code:
TT: <tell://me/sreenivas> 
 <tell://me/100>

<tell://me/500>

TT: <tell://me/sudheer>  
<tell://me/300>  

TT: <tell://me/sreenivas>  
<tell://me/200>  

TT: <tell://me/sudheer>  
<tell://me/400>

I need an output in the below format. Please help me.


Code:
TT: <tell://me/sreenivas> 
<tell://me/100> 
<tell://me/200>

<tell://me/500>

TT: <tell://me/sudheer> 
 <tell://me/300> 
<tell://me/400>

(or)

TT: <tell://me/sreenivas> 
<tell://me/100> 
<tell://me/200>

TT: <tell://me/sudheer>  
<tell://me/300> 
<tell://me/400>

<tell://me/500>

(or)

<tell://me/500>

TT: <tell://me/sreenivas> 
<tell://me/100> 
<tell://me/200>

TT: <tell://me/sudheer> 
 <tell://me/300> 
<tell://me/400>

Explanation of above o/p:
If the pattern between "<tell://me/" and ">" is same on any of the lines that contains "TT" then take only one line from them.
That line should be followed by the lines followed by the actual lines that have the same pattern between "<tell://me/" and ">".

And whenever a line is not preceded with TT line, that line should be taken as it is.

Looking forward to your help as soon as possible. Let me know if any queries.

With Regards,
Sree
# 2  
Old 08-30-2014
Is this minor modification of the script supplied for your last thread sufficient?:
Code:
awk '
NF == 0 { next }
/^TT/ {	if(!((key = $1 FS $2) in out))
		out[key] = $0
	TTf = 1
	next
}
TTf {	out[key] = out[key] "\n" $0
	TTf = 0
	next
}
{	print $0 "\n"
}
END {	for(key in out)
		printf("%s\n\n", out[key])
}' data.txt

which produces the output:
Code:
<tell://me/500>

TT: <tell://me/sudheer>  
<tell://me/300>  
<tell://me/400>

TT: <tell://me/sreenivas> 
 <tell://me/100>
<tell://me/200>

for the input you provided in this thread. It makes the assumption that a line starting with TT: will not be followed by a blank line. If that is a problem for you, please try modifying the script to remove that assumption and let us know how it works for you.

If preserving the order of the 1st occurrence of the TT: line values is important, that can be fixed too. But, since the TT: input line for a given value aren't adjacent in your input file, I assume the order is not important i your output file.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-01-2014
Thanks DON for your help!
# 4  
Old 09-02-2014
Code:
awk -F '\n' '$1 in a {a[$1] = (a[$1] FS $2); next} {a[$1] = $0} END {for(x in a) print a[x]}' RS= ORS='\n\n' file

# 5  
Old 09-02-2014
Quote:
Originally Posted by SriniShoo
Code:
awk -F '\n' '$1 in a {a[$1] = (a[$1] FS $2); next} {a[$1] = $0} END {for(x in a) print a[x]}' RS= ORS='\n\n' file

When I try this with the sample input given in this thread, I get the output:
Code:
TT: <tell://me/sudheer>  
<tell://me/300>  
<tell://me/400>

TT: <tell://me/sreenivas>  
<tell://me/200>  

TT: <tell://me/sreenivas> 
 <tell://me/100>

<tell://me/500>

which doesn't seem to be even close to what was requested. How can taking pairs of input lines (without verifying that the 1st line in a pair starts with TT:) do what was requested?
# 6  
Old 09-02-2014
Quote:
Originally Posted by Don Cragun
When I try this with the sample input given in this thread, I get the output:
Code:
TT: <tell://me/sudheer>  
<tell://me/300>  
<tell://me/400>
 
TT: <tell://me/sreenivas>  
<tell://me/200>  
 
TT: <tell://me/sreenivas> 
 <tell://me/100>
 
<tell://me/500>

which doesn't seem to be even close to what was requested. How can taking pairs of input lines (without verifying that the 1st line in a pair starts with TT:) do what was requested?
Hi Don, there are unwanted spaces at the end of the lines, that is effecting the command.
I would suggest you remove the unwanted spaces and try again
# 7  
Old 09-02-2014
Quote:
Originally Posted by SriniShoo
Hi Don, there are unwanted spaces at the end of the lines, that is effecting the command.
I would suggest you remove the unwanted spaces and try again
Hi SriniShoo,
Maybe your code thinks there are unwanted spaces, but the sample output provided by the submitter seems to want those trailing spaces that were present in the input to be there in the output as well.

If your code depends on modifying the given sample input, maybe you should warn readers that your code only works if the input is modified to meet your additional requirements (or modify your suggested code to massage the sample input provided by the submitter into the format your code requires) and explain that it does not provide the output requested by the submitter.

- Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed in formatting the Output file

Hi All, Need your help in resolving the below issue. I've a file called "data.txt" with the below lines: TT: <tell://me/sreenivas> <tell://me/100> TT: <tell://me/sudheer> <tell://me/300> TT: <tell://me/sreenivas> <tell://me/200> TT: <tell://me/sudheer> <tell://me/400> ... (3 Replies)
Discussion started by: raosr020
3 Replies

2. Shell Programming and Scripting

Formatting output

I have the output like below: DEV#: 9 DEVICE NAME: hdisk9 TYPE: 1750500 ALGORITHM: Load Balance SERIAL: 68173531021 ========================================================================== Path# Adapter/Path Name State Mode Select Errors 0 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

3. AIX

Help Formatting Output

I am using FORTRAN 90 on AIX 5.3 and need to output my data to a tab-delimited file. It must have actual tabs, and I cannot figure out a way to make it work. The resulting file will be imported into another application (quickbooks) as an .iif file....for some reason, it needs the tabs; spaces do... (2 Replies)
Discussion started by: KathyB148
2 Replies

4. Shell Programming and Scripting

Help needed in formatting script files

Hi, Can anyone tell me how i can convert all tab spaces inside a script to 4 spaces through another script. Also i need to find if all the quotes are matching and ended properly. Any idea whould be of great help. Many thanks! (3 Replies)
Discussion started by: justchill
3 Replies

5. Shell Programming and Scripting

Formatting help needed awk or sed maybe

I am executing the following command: sort file1.txt | uniq -c | sort -n > file2.txt The problem is that in file 2, I get leading spaces, Like so: 1 N/A|A8MW11 8 N/A|ufwo1 9 N/A|a8mw11 10 900003|smoketest297688 10 N/A|a9dg4 10 danny|danni 12... (5 Replies)
Discussion started by: ddurden7
5 Replies

6. UNIX for Dummies Questions & Answers

Formatting Help needed

How would i write a script which will add a following content to a file. File Before running script. acpi = 1 apic = 1 builder = 'hvm' device_model = '/usr/lib/xen/bin/qemu-dm' disk = File After running the script. acpi = 1 apic = 1 builder = 'hvm' device_model =... (5 Replies)
Discussion started by: pinga123
5 Replies

7. Shell Programming and Scripting

Formatting Help needed(Sed)

I have a file called abc.txt which has following contents. 10.180.8.231=31608 10.180.8.232=29011 10.180.8.233=31606 10.180.8.234=40501 10.180.8.235=32591 10.180.8.236=31605 10.180.8.237=30561 10.180.8.238=14231 How would i find a ip address having maximum number of ram available. Here... (2 Replies)
Discussion started by: pinga123
2 Replies

8. Shell Programming and Scripting

Little formatting help needed.

I have a following string. "machine=IFLMUD5HP0581&group1=Stop" I have created 2 variables namely machine and action. machine should contain IFLMUD5HP0581 action should contain Stop How do i write a script for the same. (7 Replies)
Discussion started by: pinga123
7 Replies

9. Shell Programming and Scripting

Output formatting help needed.

Hi guys , I have a file which contains following string.(filename tempdisplay) (location 0.0.0.0:5900) i needed to write a script which will extract the string that comes after 0.0.0.0 i.e the string :5900. I have used following method to extract the string :5900 .Is it a preferred way of... (4 Replies)
Discussion started by: pinga123
4 Replies

10. Shell Programming and Scripting

Formatting the output

Hi all, Have the following code(1) producing the results(2 & 3). Would like to know if there is a way to format the two reports created in a similar fashion. IE - The first is formatted nicely as a result of the echo "$xmpbdate $xavgs" >> $xmpbrpt However when I attempt to do the same on... (7 Replies)
Discussion started by: Cameron
7 Replies
Login or Register to Ask a Question