Grep line to separate column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep line to separate column
# 1  
Old 10-13-2010
Grep line to separate column

Hi friends

I have a file like this
Quote:
=001 abcd002432236
=035 \\$a(abc)32780281
=035 \\$a(xyz)7550102
=001 abcd002432237
=035 \\$a(abc)37909749
=035 \\$a(xyz)8777598
=001 abcd000509975
=035 \\$a(abc)43067318
=035 \\$a(xyz)8749126
=035 \ \\$a(abc)430673199
=001 abcd000509977
=035 \\$a(xyz)8749128
=001 abcd000509977
:
:
number of =035 is not constant
I want output like this probably in excel sheet .
Quote:
=001 abcd002432236 =035 \\$a(abc)32780281
=035 \\$a(xyz)7550102
=001 abcd002432237 =035 \\$a(abc)37909749
=035 \\$a(xyz)8777598
=001 abcd000509975 =035 \\$a(abc)43067318
=035 \\$a(xyz)8749126
=035 \ \\$a(abc)430673199
=001 abcd000509977 =035 \\$a(xyz)8749128

Please help


Waiting for reply

Thanks a lot
# 2  
Old 10-13-2010
Hi,

Here a 'sed' solution, test if it works:
Code:
sed '/^=001/ { N ; /^=001.*\n=035/! {P;D} ; /^=001.*\n=035/ s/\n/ / }' <file>

Regards,
# 3  
Old 10-13-2010
one in awk:

Code:
#  awk '/^=001/{t=t" "$0}!/^=001/{print t,$0;t=""}' infile 
 =001 abcd002432236 =035 \\$a(abc)32780281
 =035 \\$a(xyz)7550102
 =001 abcd002432237 =035 \\$a(abc)37909749
 =035 \\$a(xyz)8777598
 =001 abcd000509975 =035 \\$a(abc)43067318
 =035 \\$a(xyz)8749126
 =035 \ \\$a(abc)430673199

# 4  
Old 10-13-2010
Code:
sed '/^=001/N;s/\n/ /' infile

more efficient:
Code:
sed '/^=001/{N;s/\n/ /}' infile


Last edited by Scrutinizer; 10-14-2010 at 02:17 AM..
# 5  
Old 10-13-2010
Hi,
Thanks a lot , but i think by mistake I have not requested for the correct format . How I wanted is
Code:
=001 abcd002432236 =035 \\$a(abc)32780281 =035 \\$a(xyz)7550102 
=001 abcd002432237 =035 \\$a(abc)37909749 =035 \\$a(xyz)8777598
=001 abcd000509975 =035 \\$a(abc)43067318 =035 \\$a(xyz)8749126

each one in separate or different column like in xls.

Sorry for your time,Please help me

Thanks a lot

Last edited by Scott; 10-16-2010 at 07:44 AM.. Reason: Code tags
# 6  
Old 10-14-2010
It still is a bit ambiguous to me. Do you mean like this (tab separated so excel can split it):
Code:
$ awk '/^=001/{if(NR>1)print p; p=$0; next}{p=p"\t"$0}END{print p}' infile
=001 abcd002432236      =035 \\$a(abc)32780281  =035 \\$a(xyz)7550102
=001 abcd002432237      =035 \\$a(abc)37909749  =035 \\$a(xyz)8777598
=001 abcd000509975      =035 \\$a(abc)43067318  =035 \\$a(xyz)8749126   =035 \ \\$a(abc)430673199
=001 abcd000509977      =035 \\$a(xyz)8749128
=001 abcd000509977

# 7  
Old 10-14-2010
Hi Scrutinizer,

Thanks a lot this is what i was asking for. Can you please explain me the code,very curious to know .
Also one more question can we combine two query
Code:
grep -E -e '=001|(abc)|(xyz) infile > \
outfile and your awk script awk '/^=001/{if(NR>1)print p; p=$0; next}{p=p"\t"$0}END{print p}' infile

But thanks a lot .

Thanks to everyone for spending your valuable time in helping me

Last edited by vbe; 10-14-2010 at 05:27 AM.. Reason: code tags!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep one column of line

Hi all: I have the following input file: function-v such_as-hash-n 6.5328 function-n such_as-head-n 2.5586 function-a such_as-headphone-n 8.0794 function-n such_as-health-n 3.1938 function-v such_as-healthcare-n 5.2845 function-n such_as-hearing-n 9.5367... (9 Replies)
Discussion started by: owwow14
9 Replies

2. UNIX for Dummies Questions & Answers

Want to separate one column

I have one command which provide following output related to file system and disk space utilization Filesystem kbytes used avail %used Mounted on /dev/lvol3 131072 73407 54088 58% / /abc/lvol1 59797 30314 2350300 ... (8 Replies)
Discussion started by: Nakul_sh
8 Replies

3. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

4. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

How to grep an empty line in a specific column of a file?

Suppose i have the following data : cat file.txt 12431,123334,55353,546646,14342234,4646,35234 123123,3535,123434,132535,1234134,13535,123534 123213,545465,23434,45646,2342345,4656,31243 2355425,2134324,53425,342,35235,23434,234535 3423424,234234,65465,,2344,35436,234524,234... (7 Replies)
Discussion started by: Ravi Tej
7 Replies

6. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

7. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

8. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

9. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies

10. Shell Programming and Scripting

separate out the column

I know "awk -F:" will separate out the column by ":" , now if I what to separate out the column by space also , what can I do ? for example : #ps -ef |grep telnet root 10159 702 0 15:45 ? 00:00:00 in.telnetd: 192.168.0.1 how to separate out the column so that the column as below, ... (6 Replies)
Discussion started by: ust
6 Replies
Login or Register to Ask a Question