Need Help to sort text lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help to sort text lines
# 1  
Old 01-20-2009
Need Help to sort text lines

I need to sort input file as below to display as below:

input.txt
User: my_id
File: oracle/scripts/ssc/ssc_db_info
User: your_id
File: pkg_files/BWSwsrms/request
User: your_id
File: pkg_files/BWSwsco/checkConfig.sh


OUTPUT:
User: my_id
File: oracle/scripts/ssc/ssc_db_info

User: your_id
File: pkg_files/BWSwsrms/request
File: pkg_files/BWSwsco/checkConfig.sh


Thanks in advance for your help!!!
# 2  
Old 01-20-2009
nawk -f tq.awk input.txt

tq.awk:
Code:
$1 == "User:" {u=$2;next}
{a[u] = (u in a) ? a[u] ORS $0 : $0}
END {
  for(i in a)
    print "User:" i ORS a[i] ORS
}

# 3  
Old 01-21-2009
input:
Code:
User: 123
File: oracle/scripts/ssc/ssc_db_info
User: 456
File: pkg_files/BWSwsrms/request
User: 123
File: pkg_files/BWSwsco/checkConfig.sh
User: 456
File: /usr/bin/perl
User: 456
File: /local/default
User: 123
File: /etc/default

output:
Code:
User: 123
File: oracle/scripts/ssc/ssc_db_info
File: pkg_files/BWSwsco/checkConfig.sh
File: /etc/default

User: 456
File: pkg_files/BWSwsrms/request
File: /usr/bin/perl
File: /local/default

code:
Code:
#!/usr/bin/perl
use strict;
undef $/;
open FH,"<a.txt";
my $str=<FH>;
my @tmp=split("\n",$str);
my $cnt=($#tmp+1)/2;
my %hash;
for (my $i=0;$i<=$cnt-1;$i++){
	$hash{$tmp[2*$i]}.=(exists $hash{$tmp[2*$i]})?"\n".$tmp[2*$i+1]:$tmp[2*$i+1];
}
map {print $_,$hash{$_},"\n\n" } keys %hash;

# 4  
Old 01-21-2009
Thank you guys!!!
Both replied work great!!!
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

Best way to sort file with groups of text of 4-5 lines by the first one

Hi, I have some data I have taken from the internet in the following scheme: name direction webpage phone number open hours menu url book url name ... Of course the only line that is mandatory is the name wich is the one I want to sort by. I have the following sed & awk script that... (3 Replies)
Discussion started by: devmsv
3 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

5. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

6. Shell Programming and Scripting

how to sort lines in the string

Hi guys, I am trying to sort numbers in the string in descending order but for some reason sort fails. n129$ echo "81 240" | sort -r 81 240 n129$ I am not sure what am I doing wrong. Is there a 100% reliable way to make sure that sort will always work. I mean on SUNS and IBM machines. ... (4 Replies)
Discussion started by: aoussenko
4 Replies

7. UNIX for Dummies Questions & Answers

how to add new lines using sort

Hello, I'm trying to sort a file which has many lines with the sort command. I'd like to send the sorted content in the same file. But when I do that, I've the content which is sorted, but all is on one line... I view the content in Notepad++ Could somebody help me to keep the original... (5 Replies)
Discussion started by: tevious
5 Replies

8. UNIX for Dummies Questions & Answers

How to sort lines by strings between ()?

Hello,everyone. I am learning some Info commands.I put all commands and their explanations in a file. This is a part of it: ESC PgUp (scroll-other-window-backward)Scroll the other window backward ESC Right (forward-word) Move forward a word ESC r (move-to-window-line) ESC TAB... (3 Replies)
Discussion started by: vic005
3 Replies

9. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

10. Shell Programming and Scripting

How to sort lines by substring

Dear all There is a file which contains the following formatted files. I need to sort it by substring(strings after dot) in order to process efficiently. Please give me any idea how to sort it. Sample file: 1AAABBBCCC.20080401 1AAABBBCCC.20080402 2AAABBBCCC.20080401... (3 Replies)
Discussion started by: mr_bold
3 Replies
Login or Register to Ask a Question