Help needed in formatting the Output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in formatting the Output file
# 1  
Old 08-28-2014
Linux 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:

Code:
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>

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

Code:
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 ">".

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

With Regards,
SRK

Moderator's Comments:
Mod Comment edit by bakunin: changed PHP- to CODE-tags. Please use these (except for actual PHP-code) to avoid a confused syntax-highlighting effort of a misguided script. Thank you.

Last edited by bakunin; 08-28-2014 at 03:55 PM..
# 2  
Old 08-28-2014
Quote:
Originally Posted by raosr020
Let me know if any queries.
Is your input always guaranteed to consist of groups of 2 lines, one starting with "TT:" and the other with a "<tell..."clause?

If so, create a sort-of table with the 2-line groups brought to one line, like this:

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

You can do this easily with a single sed-line. Sorting this will give you all equal keys following each other. Last step is to do a "control break", which is a basic algorithm in programming. Here is shown how to do this.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 08-28-2014
This is also pretty easy with awk:
Code:
awk '
NF == 0 { next }
/^TT/ {	if(!((key = $1 FS $2) in out))
		out[key] = $0
	next
}
{	out[key] = out[key] "\n" $0 }
END {	for(key in out)
		printf("%s\n\n", out[key])
}' data.txt

This will work even if there are multiple non-blank lines between the lines starting with TT:.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-30-2014
Thanks Don. It helped me 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

Formatting File Decryption Output

Below is the out put of the decrypt command. Decrypt Command: /usr/bin/gpg --yes --batch --output file.xml --decrypt file.xml.gpg Output: gpg: encrypted with 2048-bit RSA key, ID 96301328, created 2014-04-29 "XYZ <xyz@abc.com>" gpg: encrypted with 2048-bit ELG-E key, ID ECB614CF,... (5 Replies)
Discussion started by: Ariean
5 Replies

2. Shell Programming and Scripting

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: 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:... (6 Replies)
Discussion started by: raosr020
6 Replies

3. Shell Programming and Scripting

help needed to get the output from file

Hi All, i have a file with below contents (single line) pasted below http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.grid-pnp.org/2005/11/gpnp-profile gpnp-profile.xsd" ProfileSequence="4" ClusterUId="a412e044b1986f00bfb208e0b89d90bf" ClusterName="lab1-cluster"... (3 Replies)
Discussion started by: kamauv234
3 Replies

4. Shell Programming and Scripting

File output formatting

I have a file that list information about users and want to format the output into columns using Shell scripting. There are about 50 to 75 users. Example current file output fname lname address hiredate dept fname lname address hiredate dept Desired output fname ... (4 Replies)
Discussion started by: daveisme
4 Replies

5. Shell Programming and Scripting

Formatting Output from a file

Hi guys. I'm new to Bash Programming. I've a little problem here. I have a text file, lets say data.txt, with the data: John Tan:Male:20:Singapore Mary:Female:23:Malaysia Abdul Rahman Bin Ali:Male:30:India (: is the delimiter) I want to display it like this: Name Gender Age... (6 Replies)
Discussion started by: andylbh
6 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

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

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

9. Shell Programming and Scripting

Need help with formatting an output file

Hi guys, I need help with formatting the output of a file. Below is the script I use to generate a file called output.xls. DAY=`TZ=MYT+16 date '+%b'` DAY1=`TZ=MYT+16 date '+%e'` ls -ltr /bscswork_bi2/WORK/UMOBILE/IR/IN/ALL/PROCESSED | grep "$DAY $DAY1" | awk '{print $9}' | sort | cut -c3-7... (4 Replies)
Discussion started by: kumaran21
4 Replies

10. Shell Programming and Scripting

Formatting Output file

Hi, I have a file.... File1: Num Name ID Place ADDR City Country 1024|Name1|ID1|Street1|ADDR1|Boston|UK 1025|Name2|ID3|Street2|ADDR2|London|USA The above file is varaiable length file. I have to insert 2 values in every record of the above file. Output: ... (3 Replies)
Discussion started by: manneni prakash
3 Replies
Login or Register to Ask a Question