Visit The New, Modern Unix Linux Community


Help with order lines from a file based on a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with order lines from a file based on a pattern
# 1  
Help with order lines from a file based on a pattern

Hi

I need to order these lines from a txt file my file looks like this

Code:
IMSI ........................ 1234567890 
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
IMSI ........................ 0123456789 
IMSI ........................ 1234567890 
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM

Is there any way that my file looks like that

Code:
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789 
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM

Thanks
# 2  
Quote:
Originally Posted by alone77
Hi

I need to order these lines from a txt file my file looks like this

Code:
IMSI ........................ 1234567890 
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
IMSI ........................ 0123456789 
IMSI ........................ 1234567890 
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM

Is there any way that my file looks like that

Code:
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789 
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM

Thanks
Hello Alone77,

Welcome to forum, following may help you in same.
Code:
awk '{gsub(/\./,X,$0);} {for(i=1;i<=NF;i++){if($i == "IMSI") {print "\n"$i} else {print $i}}}' ORS=" "   Input_file

Output is as follows.
Code:
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM
IMSI 0123456789
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM

EDIT: Above solution will give a blank line in starting if word IMSI is present in first line, so following will remove this loop whole.

Code:
awk '{gsub(/\./,X,$0);} {for(i=1;i<=NF;i++){if($i == "IMSI" && NR != 1) {print "\n"$i} else {print $i}}}' ORS=" "

EDIT: One more solution on same.
Code:
awk '{gsub(/\./,X,$0);} {for(i=1;i<=NF;i++){$i=$i == "IMSI" && NR != 1?$i="\n"$i:$i;print $i}} END{ORS="";print "\n"}' ORS=" "  Input_file

Output will be as follows.
Code:
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM
IMSI 0123456789
IMSI 1234567890 APN INTERNETCOM APN MMSCOM APN WAPCOM APN BACOM

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-17-2014 at 04:23 AM.. Reason: Added a better solution now+ 1 more solution
# 3  
Code:
[akshay@nio tmp]$ cat file
IMSI ........................ 1234567890 
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM
IMSI ........................ 0123456789 
IMSI ........................ 1234567890 
APN ......................... INTERNET.COM
APN ......................... MMS.COM
APN ......................... WAP.COM
APN ......................... BA.COM

Code:
[akshay@nio tmp]$ awk 's=/IMSI/{printf("%s",(++i==1?"":RS))}{printf("%s%s%s%s",(s?"":OFS),$1,OFS,$3)}END{print ""}' file

Code:
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM

---------- Post updated at 02:33 PM ---------- Previous update was at 02:22 PM ----------

OR like this

Code:
[akshay@nio tmp]$ awk 'function p(){if(length(s)){print s;s=""}}{if(/IMSI/)p();s = (length(s) ? s OFS : "") $1 OFS $NF;}END{p()}' file

# 4  
Try
Code:
awk '/IMSI/ && NR>1 {printf "\n"}; END {printf "\n"} {printf "%s %s ",$1, $NF}' ORS=" " file
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM
IMSI 0123456789
IMSI 1234567890 APN INTERNET.COM APN MMS.COM APN WAP.COM APN BA.COM

# 5  
Perl if interested

Code:
perl -lane 'sub p{print "@_" if @_ }; p(@S) and undef @S if eof or /IMSI/; push(@S,@F[0,$#F]);' file

OR bit lengthy
Code:
perl -lane 'printf("%s",(++$i==1?"":"\n")) if $s=/IMSI/; printf("%s%s%s%s",($s?"":" "),$F[0]," ",$F[$#F]); print "" if eof' file


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #813
Difficulty: Medium
The CSS RGBA alpha parameter defines the opacity as a number between 1.0 (fully transparent) and 0.0 (fully opaque).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

Extract some characters from lines based on pattern

Hi All, i would like to get some help regarding extracting certain characters from a line grepped. blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah... (10 Replies)
Discussion started by: mad man
10 Replies

3. Shell Programming and Scripting

Help with a deleting lines based on a pattern

I have a header-detail file that goes like this: SHP00288820131021110921 ORDER0156605920131021110921INMMMMFN DETAIL0004 4C2Z 10769 AAFC 0000009600000094 4C2Z 10769 AAFC 0000672107 OIL DETAIL0002 ER3Z 14300 E 0000001300000012 ER3Z 14300 E 0000672107 OIL... (3 Replies)
Discussion started by: rbaggio666
3 Replies

4. Shell Programming and Scripting

File w/ many line pairs--how do I order based on 1st lines only?

I have a file in which the data is stored in pairs of lines. The first line (beginining with ">") is a header, the second line is a sequence. I would like to sort the file by species name. Desired output for the example file: I can use sort -t'_' -k2 to alphabetize headers in the... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

6. Shell Programming and Scripting

Need to merge lines based on pattern

Hi, I have a requirement to merge multiple lines based on search pattern. The search criteria is : it will search for CONSTRAINT and when it found CONSTRAINT, it will merge all lines to 1 line till it founds blank line. For Example: CREATE TABLE "AMS_DISTRIBUTOR_XREF" ( "SOURCE"... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

7. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

8. Shell Programming and Scripting

Merge lines in text file based on pattern

Hello, I have searched forum trying to find a solution to my problem, but could not find anything or I did not understand the examples.... I should say, I am very inexperienced with text processing. I have a text file with approx 60k lines in it. I need to merge lines based on the number... (8 Replies)
Discussion started by: Bertik
8 Replies

9. Shell Programming and Scripting

Combine the lines based on particular pattern

Hi, I've a weird problem to be solved. Assume i have a file like this: 1. <timestamp> UID: 12345 <junk> DevID: V123 2. <timestamp>DevID: V123 <junk> DuID: VP 3. ... 4. .... 5. <timestamp> UID: 789 <junk> DevID: S456 6. <timestamp>DevID: S456 <junk> DuID: VP.... 7. ..... Say if i... (3 Replies)
Discussion started by: VenkataPrasad
3 Replies

10. Shell Programming and Scripting

selecting only few lines from many based on a common pattern

Hi, I have a file having some thousand records with the following sort of lines: Error: Failed to get order data Order: PO-BBBTGZE Error: No CLI Error: Failed to get order data Order: PO-SBDJUZA Order: PO-XBBIDEN Error: No CLI Error: Failed to get order data Order: PO-BBDJUTQ Order:... (2 Replies)
Discussion started by: damansingh
2 Replies

Featured Tech Videos